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

操作单文件计数器,验证 0 和 99 的边界

HTMLWindows,Microsoft Edge 157.0.4341.46,原生 HTML、CSS 和 JavaScript。counter.html
counter.html54 行
<!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; min-height: 100vh; display: grid; place-items: center; background: #17233b; color: #1c293a; font: 1rem "Segoe UI", sans-serif; }
    main { width: min(20rem, calc(100% - 2rem)); box-sizing: border-box; padding: 1.5rem; background: #fff; border-radius: 1rem; text-align: center; }
    h1 { margin: 0; font-size: 1.3rem; }
    output { display: block; margin: 1rem; font: 700 3rem "Segoe UI", sans-serif; font-variant-numeric: tabular-nums; }
    button { padding: .55rem .85rem; margin: .15rem; color: inherit; font: inherit; cursor: pointer; }
    button:disabled { color: #777; cursor: default; }
    button:focus-visible { outline: 3px solid #234fc2; outline-offset: 3px; }
  </style>
</head>
<body>
  <main>
    <h1>计数器验收练习</h1>
    <output id="value" aria-label="当前计数" aria-live="polite">0</output>
    <button id="minus" type="button" aria-label="减一" disabled>−</button>
    <button id="plus" type="button" aria-label="加一">+</button>
    <button id="reset" type="button">重置</button>
  </main>
  <script>
    const value = document.querySelector("#value");
    const minus = document.querySelector("#minus");
    const plus = document.querySelector("#plus");
    let count = 0;
    let feedback;

    function setCount(next) {
      const clamped = Math.max(0, Math.min(99, next));
      if (clamped === count) return;
      count = clamped;
      value.textContent = String(count);
      minus.disabled = count === 0;
      plus.disabled = count === 99;
      feedback?.cancel();
      if (!matchMedia("(prefers-reduced-motion: reduce)").matches) {
        feedback = value.animate(
          [{ transform: "scale(1)" }, { transform: "scale(1.12)" }, { transform: "scale(1)" }],
          { duration: 220, easing: "ease-out" },
        );
      }
    }

    plus.addEventListener("click", () => setCount(count + 1));
    minus.addEventListener("click", () => setCount(count - 1));
    document.querySelector("#reset").addEventListener("click", () => setCount(0));
  </script>
</body>
</html>
本例需在 Windows,Microsoft Edge 157.0.4341.46,原生 HTML、CSS 和 JavaScript。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows,Microsoft Edge 157.0.4341.46,原生 HTML、CSS 和 JavaScript。

入口文件:projects/vibe-coding-mindset/counter.html

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 counter.html,或下载右侧课程工程包。
  2. 确认本地已准备 Windows,Microsoft Edge 157.0.4341.46,原生 HTML、CSS 和 JavaScript。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Vibe Coding

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

下载课程关联工程包:Vibe Coding

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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