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

运行 Bootstrap 5 浮动标签聚焦切换、缺失 placeholder 陷阱、文本框与下拉框实验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-form-floating-labels.py
explore-form-floating-labels.py138 行
"""Offline Playwright Chromium verification for Bootstrap 5 Floating Labels."""
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; line-height: 1.5; }
.wrap { width: 600px; }
.form-floating { position: relative; }
.form-floating > .form-control, .form-floating > .form-select {
  display: block; width: 100%; height: calc(3.5rem + 2px); line-height: 1.25;
  padding: 1rem 0.75rem; font-size: 1rem; border: 1px solid #ced4da; border-radius: 0.375rem;
}
.form-floating > textarea.form-control { height: 100px; }
.form-floating > label {
  position: absolute; top: 0; left: 0; height: 100%; padding: 1rem 0.75rem;
  pointer-events: none; border: 1px solid transparent; transform-origin: 0 0;
  opacity: 1; transform: none;
}
.form-floating > .form-control::placeholder { color: transparent; }
.form-floating > .form-control:focus,
.form-floating > .form-control:not(:placeholder-shown) {
  padding-top: 1.625rem; padding-bottom: 0.625rem;
}
.form-floating > .form-select { padding-top: 1.625rem; padding-bottom: 0.625rem; }
.form-floating > .form-control:focus ~ label,
.form-floating > .form-control:not(:placeholder-shown) ~ label,
.form-floating > .form-select ~ label {
  opacity: 0.65;
  transform: scale(0.85) translateY(-0.5rem) translateX(0.15rem);
}
"""


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": 600})

        # Case 1: input focus and non-empty floating label transform
        page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div class="wrap">
          <div class="form-floating">
            <input type="text" class="form-control" id="email" placeholder="Enter email">
            <label id="lbl_email" for="email">Email</label>
          </div>
          <button id="other" type="button">BlurTarget</button>
        </div></body></html>""")
        idle = page.evaluate("""() => {
          const cs = getComputedStyle(document.getElementById('lbl_email'));
          return { opacity: cs.opacity, transform: cs.transform };
        }""")
        page.focus("#email")
        focused = page.evaluate("""() => {
          const cs = getComputedStyle(document.getElementById('lbl_email'));
          return { opacity: cs.opacity, floated: cs.transform !== 'none', paddingTop: getComputedStyle(document.getElementById('email')).paddingTop };
        }""")
        page.fill("#email", "dev@example.com")
        page.click("#other")
        filled_blurred = page.evaluate("""() => {
          const cs = getComputedStyle(document.getElementById('lbl_email'));
          return { opacity: cs.opacity, floated: cs.transform !== 'none' };
        }""")
        act1 = {"idle": idle, "focused": focused, "filled_blurred": filled_blurred}
        exp1 = {
            "idle": {"opacity": "1", "transform": "none"},
            "focused": {"opacity": "0.65", "floated": True, "paddingTop": "26px"},
            "filled_blurred": {"opacity": "0.65", "floated": True},
        }
        cases.append({"name": "floating_input_focus_and_value", "expected": exp1, "actual": act1, "passed": act1 == exp1})

        # Case 2: missing placeholder pitfall (:placeholder-shown does not match)
        page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div class="wrap">
          <div class="form-floating">
            <input type="text" class="form-control" id="no_ph">
            <label id="lbl_noph" for="no_ph">Missing Placeholder</label>
          </div>
        </div></body></html>""")
        act2 = page.evaluate("""() => {
          const cs = getComputedStyle(document.getElementById('lbl_noph'));
          return { empty_unfocused_opacity: cs.opacity, stuck_floated: cs.transform !== 'none' };
        }""")
        exp2 = {"empty_unfocused_opacity": "0.65", "stuck_floated": True}
        cases.append({"name": "floating_missing_placeholder_pitfall", "expected": exp2, "actual": act2, "passed": act2 == exp2})

        # Case 3: floating textarea
        page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div class="wrap">
          <div class="form-floating">
            <textarea class="form-control" id="comment" placeholder="Comment goes here"></textarea>
            <label id="lbl_ta" for="comment">Comments</label>
          </div>
          <button id="other2" type="button">BlurTarget</button>
        </div></body></html>""")
        ta_idle = page.evaluate("() => ({ opacity: getComputedStyle(document.getElementById('lbl_ta')).opacity, transform: getComputedStyle(document.getElementById('lbl_ta')).transform })")
        page.fill("#comment", "Great layout!")
        page.click("#other2")
        ta_filled = page.evaluate("() => ({ opacity: getComputedStyle(document.getElementById('lbl_ta')).opacity, floated: getComputedStyle(document.getElementById('lbl_ta')).transform !== 'none' })")
        act3 = {"idle": ta_idle, "filled_blurred": ta_filled}
        exp3 = {"idle": {"opacity": "1", "transform": "none"}, "filled_blurred": {"opacity": "0.65", "floated": True}}
        cases.append({"name": "floating_textarea_behavior", "expected": exp3, "actual": act3, "passed": act3 == exp3})

        # Case 4: floating select (always floated at top-left)
        page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div class="wrap">
          <div class="form-floating">
            <select class="form-select" id="sel1">
              <option value="1">1</option><option value="2">2</option><option value="3">3</option>
            </select>
            <label id="lbl_sel" for="sel1">Select list:</label>
          </div>
        </div></body></html>""")
        sel_init = page.evaluate("() => ({ opacity: getComputedStyle(document.getElementById('lbl_sel')).opacity, floated: getComputedStyle(document.getElementById('lbl_sel')).transform !== 'none' })")
        page.select_option("#sel1", "3")
        sel_after = page.evaluate("() => ({ value: document.getElementById('sel1').value, opacity: getComputedStyle(document.getElementById('lbl_sel')).opacity, floated: getComputedStyle(document.getElementById('lbl_sel')).transform !== 'none' })")
        act4 = {"initial": sel_init, "after_change": sel_after}
        exp4 = {
            "initial": {"opacity": "0.65", "floated": True},
            "after_change": {"value": "3", "opacity": "0.65", "floated": True},
        }
        cases.append({"name": "floating_select_always_floated", "expected": exp4, "actual": act4, "passed": act4 == exp4})

        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-floating-labels-lab/explore-form-floating-labels.py

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

下载课程关联工程包:Bootstrap5

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

下载课程关联工程包:Bootstrap5

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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