"""Offline Chromium lab for jQuery UI Ch16: Examples overview (Interactions, Widgets, Effects, Utilities)."""
import json
from playwright.sync_api import sync_playwright
HTML = """<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery UI Examples Overview Lab</title>
<style>
body { margin: 0; font-family: sans-serif; }
#drag-card { position: absolute; left: 20px; top: 20px; width: 100px; height: 50px; background: #ddeeff; border: 1px solid #336699; }
#sel-list { position: absolute; left: 20px; top: 100px; list-style: none; padding: 0; margin: 0; }
#sel-list li { width: 100px; height: 24px; border: 1px solid #aaa; margin-bottom: 2px; }
#sel-list li.ui-selected { background: #f39814; color: #fff; }
#tabs { position: absolute; left: 160px; top: 20px; width: 220px; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }
#pb { position: absolute; left: 160px; top: 110px; width: 200px; height: 20px; border: 1px solid #666; }
#pb-bar { height: 100%; width: 25%; background: #4caf50; }
#fx-box { position: absolute; left: 400px; top: 20px; width: 80px; height: 40px; }
.theme-a { background: #ccc; }
.theme-b { background: #2196f3; color: #fff; }
#anchor { position: absolute; left: 400px; top: 100px; width: 100px; height: 40px; border: 1px solid #333; }
#pos-target { position: absolute; left: 0px; top: 0px; width: 60px; height: 24px; background: #ffeb3b; }
#wf-badge { position: absolute; left: 400px; top: 180px; }
</style>
</head>
<body>
<div id="drag-card">Drag</div>
<ul id="sel-list"><li id="item-1">Item 1</li><li id="item-2">Item 2</li></ul>
<div id="tabs">
<button id="btn-tab-1">Tab 1</button><button id="btn-tab-2">Tab 2</button>
<div id="tab-1" class="tab-panel active">Panel 1</div>
<div id="tab-2" class="tab-panel">Panel 2</div>
</div>
<div id="pb"><div id="pb-bar"></div></div>
<div id="fx-box" class="theme-a">FX</div>
<div id="anchor">Anchor</div>
<div id="pos-target">Target</div>
<div id="wf-badge"></div>
<script>
// 1. Draggable + Selectable interactions
(() => {
const card = document.getElementById("drag-card");
let dragging = false, sx = 0, sy = 0, ox = 20, oy = 20;
card.addEventListener("mousedown", e => {
dragging = true; sx = e.clientX; sy = e.clientY;
ox = parseFloat(getComputedStyle(card).left);
oy = parseFloat(getComputedStyle(card).top);
});
window.addEventListener("mousemove", e => {
if (!dragging) return;
card.style.left = (ox + e.clientX - sx) + "px";
card.style.top = (oy + e.clientY - sy) + "px";
});
window.addEventListener("mouseup", () => { dragging = false; });
document.querySelectorAll("#sel-list li").forEach(li => {
li.addEventListener("click", () => {
document.querySelectorAll("#sel-list li").forEach(x => x.classList.remove("ui-selected"));
li.classList.add("ui-selected");
});
});
})();
// 2. Tabs + Progressbar widgets
document.getElementById("btn-tab-1").addEventListener("click", () => {
document.getElementById("tab-1").classList.add("active");
document.getElementById("tab-2").classList.remove("active");
});
document.getElementById("btn-tab-2").addEventListener("click", () => {
document.getElementById("tab-2").classList.add("active");
document.getElementById("tab-1").classList.remove("active");
});
window.setProgress = function(val) {
const bar = document.getElementById("pb-bar");
bar.style.width = val + "%";
bar.dataset.value = String(val);
};
// 3. Effects: visibility + class animation helpers
window.fxOps = function() {
const box = document.getElementById("fx-box");
box.style.display = "none";
const hiddenDisplay = getComputedStyle(box).display;
box.style.display = "block";
const shownDisplay = getComputedStyle(box).display;
box.classList.remove("theme-a");
box.classList.add("theme-b");
return {
hiddenDisplay,
shownDisplay,
hasThemeA: box.classList.contains("theme-a"),
hasThemeB: box.classList.contains("theme-b")
};
};
// 4. Utilities: Position + Widget Factory
window.runUtilities = function() {
const anchor = document.getElementById("anchor").getBoundingClientRect();
const target = document.getElementById("pos-target");
// my: "left top", at: "right bottom", of: "#anchor"
target.style.left = anchor.right + "px";
target.style.top = anchor.bottom + "px";
const tr = target.getBoundingClientRect();
// Minimal widget factory instance
const badge = document.getElementById("wf-badge");
const widget = { options: { label: "ready" }, _create() { badge.textContent = this.options.label; } };
widget._create();
return {
targetLeft: Math.round(tr.left),
targetTop: Math.round(tr.top),
anchorRight: Math.round(anchor.right),
anchorBottom: Math.round(anchor.bottom),
badgeText: badge.textContent
};
};
</script>
</body>
</html>
"""
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)
try:
page = browser.new_page(viewport={"width": 800, "height": 600})
page.set_content(HTML)
# Case 1: Interactions (drag + select)
before = page.locator("#drag-card").bounding_box()
cx = before["x"] + before["width"] / 2
cy = before["y"] + before["height"] / 2
page.mouse.move(cx, cy)
page.mouse.down()
page.mouse.move(cx + 80, cy + 40, steps=4)
page.mouse.up()
after = page.locator("#drag-card").bounding_box()
page.click("#item-2")
r1 = {
"dx": round(after["x"] - before["x"]),
"dy": round(after["y"] - before["y"]),
"selectedId": page.evaluate("() => document.querySelector('#sel-list li.ui-selected')?.id"),
}
exp1 = {"dx": 80, "dy": 40, "selectedId": "item-2"}
cases.append(dict(name="interactions_drag_and_select_demo", expected=exp1, actual=r1, passed=(r1 == exp1)))
# Case 2: Widgets (tabs + progressbar)
page.click("#btn-tab-2")
page.evaluate("() => window.setProgress(75)")
r2 = page.evaluate("""() => ({
tab1Display: getComputedStyle(document.getElementById('tab-1')).display,
tab2Display: getComputedStyle(document.getElementById('tab-2')).display,
progressWidth: document.getElementById('pb-bar').style.width
})""")
exp2 = {"tab1Display": "none", "tab2Display": "block", "progressWidth": "75%"}
cases.append(dict(name="widgets_tabs_and_progressbar_demo", expected=exp2, actual=r2, passed=(r2 == exp2)))
# Case 3: Effects (visibility + switchClass)
r3 = page.evaluate("() => window.fxOps()")
exp3 = {"hiddenDisplay": "none", "shownDisplay": "block", "hasThemeA": False, "hasThemeB": True}
cases.append(dict(name="effects_class_and_visibility_demo", expected=exp3, actual=r3, passed=(r3 == exp3)))
# Case 4: Utilities (Position + Widget Factory)
r4 = page.evaluate("() => window.runUtilities()")
exp4 = {"targetLeft": 502, "targetTop": 142, "anchorRight": 502, "anchorBottom": 142, "badgeText": "ready"}
cases.append(dict(name="utilities_position_and_widget_factory_demo", expected=exp4, actual=r4, passed=(r4 == exp4)))
finally:
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/jqueryui-examples-lab/explore-examples.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-examples.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a946.zip
课程关联工程包(登记关联43章) · 含当前章节或实例 · mbskycom202612a916.zip