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

离线 Chromium 实测悬停/聚焦/按压/禁用变体、变换过渡、关键帧及降动效偏好

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-animations.py
explore-animations.py197 行
"""Offline Chromium verification for Tailwind CSS animations and interaction states chapter."""
import json
from playwright.sync_api import sync_playwright

CSS = """
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes ping { 75%, 100% { transform: scale(2); opacity: 0; } }
@keyframes pulse { 50% { opacity: 0.5; } }
@keyframes bounce {
  0%, 100% { transform: translateY(-25%); animation-timing-function: cubic-bezier(0.8, 0, 1, 1); }
  50% { transform: none; animation-timing-function: cubic-bezier(0, 0, 0.2, 1); }
}
@keyframes wiggle {
  0%, 100% { transform: rotate(-3deg); }
  50% { transform: rotate(3deg); }
}
.bg-blue-500 { background-color: rgb(59 130 246); }
.hover\\:bg-blue-700:hover { background-color: rgb(29 78 216); }
.bg-green-500 { background-color: rgb(34 197 94); }
.hover\\:bg-green-600:hover { background-color: rgb(22 163 74); }
.hover\\:shadow-md:hover { box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1); }
.bg-red-500 { background-color: rgb(239 68 68); }
.active\\:bg-red-700:active { background-color: rgb(185 28 28); }
.active\\:scale-95:active { transform: scale(0.95); }
.hover\\:scale-105:hover { transform: scale(1.05); }
.hover\\:-translate-y-1:hover { transform: translateY(-0.25rem); }
.border { border: 1px solid rgb(229 231 235); }
.outline-none { outline: 2px solid transparent; outline-offset: 2px; }
.focus\\:border-blue-500:focus { border-color: rgb(59 130 246); }
.focus\\:ring-2:focus { box-shadow: 0 0 0 2px rgb(191 219 254); }
.cursor-not-allowed { cursor: not-allowed; }
.w-20 { width: 5rem; }
.h-20 { height: 5rem; }
.hover\\:w-40:hover { width: 10rem; }
.transition-all { transition-property: all; transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); transition-duration: 150ms; }
.duration-300 { transition-duration: 300ms; }
.ease-in-out { transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); }
.animate-spin { animation: spin 1s linear infinite; }
.animate-ping { animation: ping 1s cubic-bezier(0, 0, 0.2, 1) infinite; }
.animate-pulse { animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite; }
.animate-bounce { animation: bounce 1s infinite; }
.animate-spin-slow { animation: spin 3s linear infinite; }
.animate-wiggle { animation: wiggle 1s ease-in-out infinite; }
.will-change-transform { will-change: transform; }
@media (prefers-reduced-motion: reduce) {
  .motion-reduce\\:animate-none { animation: none; }
}
"""


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


def main():
    cases = []
    with sync_playwright() as pw:
        browser = launch_browser(pw)
        page = browser.new_page(viewport={"width": 800, "height": 600})
        page.set_content(f"""<!doctype html><html><head><style>{CSS}</style></head><body>
            <button id="b-hover" class="bg-green-500 hover:bg-green-600 hover:shadow-md" style="width:120px;height:40px;">Hover</button>
            <input id="inp-focus" class="border focus:border-blue-500 focus:ring-2 outline-none" />
            <button id="b-active" class="bg-red-500 active:bg-red-700 active:scale-95" style="width:120px;height:40px;">Active</button>
            <button id="b-dis" class="cursor-not-allowed" disabled>Disabled</button>

            <div id="t-box" class="w-20 h-20 hover:w-40 transition-all duration-300 ease-in-out"></div>
            <button id="t-scale" class="bg-blue-500 hover:scale-105" style="width:100px;height:40px;">Scale</button>
            <div id="t-card" class="hover:-translate-y-1" style="width:160px;height:80px;">Card</div>

            <div id="a-spin" class="animate-spin">Spin</div>
            <div id="a-ping" class="animate-ping">Ping</div>
            <div id="a-pulse" class="animate-pulse">Pulse</div>
            <div id="a-bounce" class="animate-bounce">Bounce</div>
            <div id="a-slow" class="animate-spin-slow">Slow</div>
            <div id="a-wiggle" class="animate-wiggle">Wiggle</div>

            <div id="wc-box" class="will-change-transform">WC</div>
            <div id="rm-box" class="animate-bounce motion-reduce:animate-none">RM</div>
        </body></html>""")

        # Case 1: interactive states (hover, focus, active, disabled)
        hover_before = page.eval_on_selector("#b-hover", "el => getComputedStyle(el).backgroundColor")
        page.hover("#b-hover")
        hover_after = page.eval_on_selector("#b-hover", "el => ({bg: getComputedStyle(el).backgroundColor, shadow: getComputedStyle(el).boxShadow !== 'none'})")

        page.focus("#inp-focus")
        focus_state = page.eval_on_selector("#inp-focus", "el => ({border: getComputedStyle(el).borderTopColor, ring: getComputedStyle(el).boxShadow, outline: getComputedStyle(el).outlineStyle})")

        box = page.locator("#b-active").bounding_box()
        page.mouse.move(box["x"] + box["width"] / 2, box["y"] + box["height"] / 2)
        page.mouse.down()
        page.wait_for_function(
            "() => getComputedStyle(document.querySelector('#b-active')).backgroundColor === 'rgb(185, 28, 28)'"
        )
        active_state = page.eval_on_selector("#b-active", "el => ({bg: getComputedStyle(el).backgroundColor, tf: getComputedStyle(el).transform})")
        page.mouse.up()

        dis_state = page.eval_on_selector("#b-dis", "el => ({disabled: el.disabled, cursor: getComputedStyle(el).cursor})")

        c1_act = {
            "hover_before": hover_before,
            "hover_after_bg": hover_after["bg"],
            "hover_has_shadow": hover_after["shadow"],
            "focus_border": focus_state["border"],
            "focus_ring": focus_state["ring"],
            "active_bg": active_state["bg"],
            "active_scale": active_state["tf"],
            "disabled_attr": dis_state["disabled"],
            "disabled_cursor": dis_state["cursor"],
        }
        c1_exp = {
            "hover_before": "rgb(34, 197, 94)",
            "hover_after_bg": "rgb(22, 163, 74)",
            "hover_has_shadow": True,
            "focus_border": "rgb(59, 130, 246)",
            "focus_ring": "rgb(191, 219, 254) 0px 0px 0px 2px",
            "active_bg": "rgb(185, 28, 28)",
            "active_scale": "matrix(0.95, 0, 0, 0.95, 0, 0)",
            "disabled_attr": True,
            "disabled_cursor": "not-allowed",
        }
        cases.append(dict(name="interactive_state_variants", expected=c1_exp, actual=c1_act, passed=(c1_act == c1_exp)))

        # Case 2: transitions & transforms
        t_meta = page.eval_on_selector("#t-box", "el => ({w: getComputedStyle(el).width, prop: getComputedStyle(el).transitionProperty, dur: getComputedStyle(el).transitionDuration})")
        page.hover("#t-scale")
        scale_tf = page.eval_on_selector("#t-scale", "el => getComputedStyle(el).transform")
        page.hover("#t-card")
        card_tf = page.eval_on_selector("#t-card", "el => getComputedStyle(el).transform")
        c2_act = {
            "initial_width": t_meta["w"],
            "transition_prop": t_meta["prop"],
            "transition_dur": t_meta["dur"],
            "hover_scale_105": scale_tf,
            "hover_translate_y_neg4": card_tf,
        }
        c2_exp = {
            "initial_width": "80px",
            "transition_prop": "all",
            "transition_dur": "0.3s",
            "hover_scale_105": "matrix(1.05, 0, 0, 1.05, 0, 0)",
            "hover_translate_y_neg4": "matrix(1, 0, 0, 1, 0, -4)",
        }
        cases.append(dict(name="transitions_and_transforms", expected=c2_exp, actual=c2_act, passed=(c2_act == c2_exp)))

        # Case 3: preset & custom keyframe animations
        c3_act = page.evaluate("""() => {
            const anim = id => {
                const s = getComputedStyle(document.getElementById(id));
                return [s.animationName, s.animationDuration, s.animationIterationCount];
            };
            return {
                spin: anim('a-spin'),
                ping: anim('a-ping'),
                pulse: anim('a-pulse'),
                bounce: anim('a-bounce'),
                spin_slow: anim('a-slow'),
                wiggle: anim('a-wiggle'),
            };
        }""")
        c3_exp = {
            "spin": ["spin", "1s", "infinite"],
            "ping": ["ping", "1s", "infinite"],
            "pulse": ["pulse", "2s", "infinite"],
            "bounce": ["bounce", "1s", "infinite"],
            "spin_slow": ["spin", "3s", "infinite"],
            "wiggle": ["wiggle", "1s", "infinite"],
        }
        cases.append(dict(name="preset_and_custom_keyframes", expected=c3_exp, actual=c3_act, passed=(c3_act == c3_exp)))

        # Case 4: will-change & prefers-reduced-motion
        wc_val = page.eval_on_selector("#wc-box", "el => getComputedStyle(el).willChange")
        rm_default = page.eval_on_selector("#rm-box", "el => getComputedStyle(el).animationName")
        page.emulate_media(reduced_motion="reduce")
        rm_reduced = page.eval_on_selector("#rm-box", "el => getComputedStyle(el).animationName")
        c4_act = {
            "will_change": wc_val,
            "default_motion_anim": rm_default,
            "reduced_motion_anim": rm_reduced,
        }
        c4_exp = {
            "will_change": "transform",
            "default_motion_anim": "bounce",
            "reduced_motion_anim": "none",
        }
        cases.append(dict(name="reduced_motion_and_will_change", expected=c4_exp, actual=c4_act, passed=(c4_act == c4_exp)))
        browser.close()

    print(json.dumps({"cases": cases}, ensure_ascii=False))


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

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

入口文件:projects/tailwindcss-animations-lab/explore-animations.py

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

下载课程关联工程包:Tailwind CSS

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

下载课程关联工程包:Tailwind CSS

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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