蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 Bootstrap 5 范围滑块默认值、步长吸附与上下界钳位实验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-form-range.py
explore-form-range.py105 行
"""Offline Playwright Chromium verification for Bootstrap 5 Form Range."""
import json
from playwright.sync_api import sync_playwright

CSS = """
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; font-size: 16px; }
.wrap { width: 600px; }
.form-label { display: inline-block; margin-bottom: 0.5rem; }
.form-range {
  width: 100%; height: 1.5rem; padding: 0;
  background-color: transparent; appearance: none;
}
.form-range:disabled { pointer-events: none; }
"""


def launch_browser(p):
    try:
        return p.chromium.launch(headless=True)
    except Exception:
        return p.chromium.launch(headless=True, channel="chrome")


def main():
    cases = []
    with sync_playwright() as p:
        browser = launch_browser(p)
        page = browser.new_page(viewport={"width": 800, "height": 500})

        # Case 1: default form-range styles & step=1 default
        page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div class="wrap">
          <label for="r_def" class="form-label">Custom range</label>
          <input type="range" class="form-range" id="r_def">
        </div></body></html>""")
        act1 = page.evaluate("""() => {
          const el = document.getElementById('r_def');
          const cs = getComputedStyle(el);
          const init = el.value;
          el.stepUp();
          return {
            width: Math.round(el.getBoundingClientRect().width),
            appearance: cs.appearance,
            bg: cs.backgroundColor,
            initial_val: init,
            after_step_up: el.value,
          };
        }""")
        exp1 = {
            "width": 600,
            "appearance": "none",
            "bg": "rgba(0, 0, 0, 0)",
            "initial_val": "50",
            "after_step_up": "51",
        }
        cases.append({"name": "range_default_styles_and_value", "expected": exp1, "actual": act1, "passed": act1 == exp1})

        # Case 2: step="10" quantization
        page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div class="wrap">
          <input type="range" class="form-range" id="r_step" step="10">
        </div></body></html>""")
        act2 = page.evaluate("""() => {
          const el = document.getElementById('r_step');
          const init = el.value;
          el.stepUp();
          const up = el.value;
          el.stepDown();
          const down = el.value;
          el.value = "63";
          const snapped = el.value;
          return { init, up, down, snapped };
        }""")
        exp2 = {"init": "50", "up": "60", "down": "50", "snapped": "60"}
        cases.append({"name": "range_step_quantization", "expected": exp2, "actual": act2, "passed": act2 == exp2})

        # Case 3: min="0" max="4" clamping and input event
        page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div class="wrap">
          <input type="range" class="form-range" id="r_mm" min="0" max="4">
        </div></body></html>""")
        act3 = page.evaluate("""() => {
          const el = document.getElementById('r_mm');
          let events = 0;
          el.addEventListener('input', () => { events += 1; });
          const init = el.value;
          el.value = "10";
          el.dispatchEvent(new Event('input'));
          const clampedHigh = el.value;
          el.value = "-3";
          el.dispatchEvent(new Event('input'));
          const clampedLow = el.value;
          return { init, clampedHigh, clampedLow, events };
        }""")
        exp3 = {"init": "2", "clampedHigh": "4", "clampedLow": "0", "events": 2}
        cases.append({"name": "range_min_max_clamping", "expected": exp3, "actual": act3, "passed": act3 == exp3})

        browser.close()
    print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/bootstrap5-form-range-lab/explore-form-range.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-form-range.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Bootstrap5

课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a700.zip

下载课程关联工程包:Bootstrap5

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a714.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习