{"id":21882492,"url":"https://github.com/suiyun39/css-timer","last_synced_at":"2026-05-18T11:33:14.394Z","repository":{"id":208230089,"uuid":"721084207","full_name":"suiyun39/css-timer","owner":"suiyun39","description":"纯 CSS 实现的计时器","archived":false,"fork":false,"pushed_at":"2023-12-30T01:14:52.000Z","size":14,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-26T19:14:40.289Z","etag":null,"topics":["css","css3","timer","timer-clock"],"latest_commit_sha":null,"homepage":"https://css-timer.suiyun.io/","language":"CSS","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/suiyun39.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-11-20T10:22:52.000Z","updated_at":"2024-02-08T06:48:06.000Z","dependencies_parsed_at":"2023-12-05T03:29:35.500Z","dependency_job_id":"f610fdd2-4482-425b-9215-6eaf397870b5","html_url":"https://github.com/suiyun39/css-timer","commit_stats":null,"previous_names":["suiyun39/css-timer"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suiyun39%2Fcss-timer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suiyun39%2Fcss-timer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suiyun39%2Fcss-timer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suiyun39%2Fcss-timer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/suiyun39","download_url":"https://codeload.github.com/suiyun39/css-timer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244890098,"owners_count":20527035,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["css","css3","timer","timer-clock"],"created_at":"2024-11-28T09:31:29.083Z","updated_at":"2026-05-18T11:33:09.345Z","avatar_url":"https://github.com/suiyun39.png","language":"CSS","funding_links":[],"categories":[],"sub_categories":[],"readme":"# css-timer\n\n纯 CSS 实现的计时器\n\n## 原理解析\n\n要实现这个需求，我们需要解决一个问题：当我们拿掉 JavaScript 之后，我们应该如何存储和更新计时器的状态？\n\n通常情况下，我们不会将 HTML 和 CSS 视为真正的编程语言，但它们的一些特性组合起来却可以表现出一定的计算能力。在 Stack\nOverflow 上就有人使用 HTML 和 CSS 实现了\n[Rule 110 自动机](https://en.wikipedia.org/wiki/Rule_110) 以证明其图灵完备性。虽然这个实现存在一些争议，但是它的确证明了\nHTML 和 CSS 能够进行一定程度的计算。\n\n### 时间状态\n\n对于计时功能，我们会很自然的想到使用 CSS Animation 来实现。然而，在 CSS 中存储状态则超出了日常开发的知识范畴。在这里，我们需要认识一个全新的概念：\n[CSS Houdini](https://developer.mozilla.org/zh-CN/docs/Web/Guide/Houdini)。\n\n这是一组底层 API，公开了 CSS\n引擎的各个部分。其中的 [@property](https://developer.mozilla.org/zh-CN/docs/Web/CSS/@property) 提供了自定义属性和变量的能力。\n我们可以利用它来存储计时器的时间计数。以秒数为例，我们可以创建一个自定义属性：\n\n```css\n@property --second {\n  syntax: \"\u003cnumber\u003e\";\n  inherits: false;\n  initial-value: 0;\n}\n```\n\n接下来，我们可以利用 CSS Animation 来更新这个自定义属性。同时，利用 **执行时间**\n和 [缓动函数](https://developer.mozilla.org/zh-CN/docs/Web/CSS/easing-function)\n控制更新频率，从而模拟时间的流逝。\n\n```css\n@keyframes update-second {\n  from {\n    --second: 0;\n  }\n\n  to {\n    --second: 59;\n  }\n}\n\n.clock {\n  animation: update-second 60s steps(60) infinite;\n}\n```\n\n最后，我们使用 [CSS 计数器](https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_counter_styles/Using_CSS_counters) 和\n**伪元素** 将其显示到页面上。\n\n```css\n.clock {\n  counter-reset: second var(--second);\n\n  \u0026::before {\n    content: counter(second, decimal-leading-zero);\n  }\n}\n```\n\n\u003e 这里使用了 counter()\n\u003e 函数来优化显示，相关知识可参见 [counter()](https://developer.mozilla.org/zh-CN/docs/Web/CSS/counter)\n\n### 控制状态\n\nHTML 拥有一个天然的状态存储器，那就是复选框 (checkbox)\n。我们可以利用它的选中与否来表示计时器的运行状态。同时，通过使用 `:checked` 伪类，\n我们可以在 CSS 中捕获到这个状态，并通过兄弟选择器将这个状态传递到其他元素上。\n\n```html\n\u003cdiv\u003e\n  \u003cinput type=\"checkbox\" id=\"Running\" /\u003e\n  \u003clabel for=\"Running\" class=\"btn-start\"\u003e\u003c/label\u003e\n\u003c/div\u003e\n```\n\n```css\n.btn-start::before {\n  content: \"开始\";\n}\n\n#Running:checked ~ .btn-start::before {\n  content: \"暂停\";\n}\n```\n\n同样的，我们可以利用这个方法来影响 CSS Animation，从而控制计时逻辑的运行。\n\n```css\n.clock {\n  animation-play-state: paused;\n}\n\n#Running:checked ~ .clock {\n  animation-play-state: running;\n}\n```\n\n最后，我们利用 `:active` 伪类来捕获重置按钮的点击，并将计时器的动画状态重置。\n\n```css\n.btn-reset:active ~ .clock {\n  animation: none;\n}\n```\n\n## 参考与感谢\n\n- [牛！竟然只用 CSS 就可以实现功能完整的计时器功能～](https://www.bilibili.com/video/BV1bN411u7nZ/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuiyun39%2Fcss-timer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsuiyun39%2Fcss-timer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuiyun39%2Fcss-timer/lists"}