示例工程 42-custom-elements
42-custom-elements/demo-02-attributes/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>属性驱动演示</title>
<style>
body { margin: 0; padding: 32px; background: #f8fafc; color: #0f172a; font-family: system-ui, sans-serif; line-height: 1.7; }
.box { max-width: 720px; margin-bottom: 16px; padding: 18px; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
h1 { margin: 0 0 16px; font-size: 28px; }
h2 { margin: 0 0 8px; font-size: 17px; }
p { margin: 0 0 10px; color: #334155; }
x-progress { display: block; margin: 10px 0; }
x-progress .row { display: flex; justify-content: space-between; font-size: 13px; color: #475569; }
x-progress .track { height: 12px; margin-top: 6px; border-radius: 999px; background: #e2e8f0; overflow: hidden; }
x-progress .fill { height: 100%; background: #2563eb; }
x-progress[data-done="true"] .fill { background: #16a34a; }
.buttons { display: flex; gap: 8px; margin-top: 10px; }
button { padding: 6px 12px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; }
button + button { border-color: #cbd5e1; background: #fff; color: #0f172a; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, monospace; }
</style>
</head>
<body>
<h1>属性驱动</h1>
<div class="box">
<h2>value 与 max</h2>
<x-progress value="30" max="60" label="下载进度"></x-progress>
<p><code>value</code> 是当前值,<code>max</code> 是上限,百分比由元素自己算。</p>
</div>
<div class="box">
<h2>布尔属性</h2>
<x-progress value="60" max="60" label="构建状态" done></x-progress>
<p>存在 <code>done</code> 属性即完成,不需要 <code>done="true"</code>。</p>
<div class="buttons">
<button type="button" id="inc">value +10</button>
<button type="button" id="reset">重置为 0</button>
</div>
</div>
<script>
class XProgress extends HTMLElement {
static observedAttributes = ["value", "max", "label", "done"];
connectedCallback() {
this.render();
}
attributeChangedCallback() {
if (this.isConnected) this.render();
}
render() {
const max = Math.max(1, Number(this.getAttribute("max")) || 100);
const value = Math.max(0, Math.min(max, Number(this.getAttribute("value")) || 0));
const pct = Math.round((value / max) * 100);
const label = this.getAttribute("label") ?? "进度";
this.dataset.done = String(this.hasAttribute("done"));
this.replaceChildren();
const row = document.createElement("div");
row.className = "row";
const left = document.createElement("span");
left.textContent = label + (this.hasAttribute("done") ? "(已完成)" : "");
const right = document.createElement("span");
right.textContent = value + " / " + max;
row.append(left, right);
const track = document.createElement("div");
track.className = "track";
track.setAttribute("role", "progressbar");
track.setAttribute("aria-label", label);
track.setAttribute("aria-valuemin", "0");
track.setAttribute("aria-valuemax", String(max));
track.setAttribute("aria-valuenow", String(value));
const fill = document.createElement("div");
fill.className = "fill";
fill.style.width = pct + "%";
track.append(fill);
this.append(row, track);
}
}
customElements.define("x-progress", XProgress);
const bar = document.querySelector("x-progress");
document.getElementById("inc").addEventListener("click", () => {
const next = Math.min(60, Number(bar.getAttribute("value")) + 10);
bar.setAttribute("value", String(next));
});
document.getElementById("reset").addEventListener("click", () => {
bar.setAttribute("value", "0");
bar.removeAttribute("done");
});
</script>
</body>
</html>