手表时钟

七月 24, 2024 / Mr.x / 1阅读 / 0评论/ 分类: 创意
手表时钟 — 荣小站

手表时钟

使用 CSS 和 JavaScript 实现的复古手表风格时钟,支持自定义背景图片和响应式设计。

▼ 效果展示

这是一个精美的手表时钟效果,使用纯 CSS 绘制手表外观,包括表带、表盘和边框阴影等细节。支持自定义背景图片,可以将你喜欢的图片设置为表盘背景。时钟显示当前时间、星期和日期,采用艺术字体显示。

核心特性

  • 纯 CSS 绘制:使用 CSS 边框、阴影、伪元素打造手表外观
  • 自定义背景:支持设置任意图片作为表盘背景
  • 艺术字体:使用 Dancing Script 字体显示时间
  • 响应式设计:适配桌面端、平板和移动端
  • 实时更新:JavaScript 实时更新时间

完整代码

以下是完整的代码,点击下方查看:

📄 完整代码 (HTML + CSS + JS)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>手表</title> <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Dancing+Script:wght@700&family=Roboto:wght@400&display=swap"> <style> .watch { --watch-color: white; --screen-color: black; font-size: 4.5vmin; font-family: 'Roboto', sans-serif; color: #fff; padding: 5em 2.5em; border: 2px solid var(--watch-color); border-radius: 2em; box-shadow: inset 0 0 2px black, inset 0 0 1rem 1em hsl(0 0% 100% / 0.25); background-color: var(--screen-color); background-image: url('你想要设置的背景图地址'); background-size: cover; background-position: center; position: relative; display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; width: 400px; height: 400px; box-sizing: border-box; } .time { font-family: 'Dancing Script', cursive; font-size: 2.5em; margin: 0; line-height: 1.2; } .date { font-family: 'Dancing Script', cursive; font-size: 0.8em; margin: 0; line-height: 1.2; } .watch::before, .watch::after { content: ''; position: absolute; background-color: var(--watch-color); z-index: -1; } .watch::after { inset: -0.5em 20%; border-radius: 0.5em; background-image: linear-gradient( #fff0, hsl(0 0% 0% / 0.5) 0.5em calc(100% - 0.5em), #fff0 ); } .watch::before { inset: -50vh 25%; background-image: repeating-linear-gradient( #fff0 0 0.3em, hsl(0 0% 0% / 0.125) 0.3em 0.5em, #fff0 0.5em 0.8em ), radial-gradient(circle, #fff0, hsl(0 0% 0% / 0.25) 50%); } * { box-sizing: border-box; } body { margin: 0; padding: 1em; min-height: 100vh; background-color: #1d1e22; display: grid; place-items: center; overflow: hidden; } /* 媒体查询:平板和移动设备 */ @media (max-width: 768px) { .watch { font-size: 6vw; padding: 3em 1.5em; width: auto; height: auto; } .time { font-size: 6vw; } .date { font-size: 3.5vw; } } /* 媒体查询:超小屏设备 */ @media (max-width: 480px) { .watch { font-size: 8vw; padding: 2em 1em; } .time { font-size: 8vw; } .date { font-size: 3.5vw; } } </style> </head> <body> <div class="watch"> <div class="time"></div> <div class="date"></div> </div> <script> function clock() { const date = new Date(); const hours = date.getHours(); const minutes = date.getMinutes(); const seconds = date.getSeconds(); const formattedTime = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`; const dayOfWeek = ["日", "一", "二", "三", "四", "五", "六"][date.getDay()]; const romanMonths = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]; const month = romanMonths[date.getMonth()]; const dayNum = date.getDate(); const formattedDate = `星期${dayOfWeek}, ${month}月${dayNum}日`; document.querySelector('.watch .time').innerHTML = formattedTime; document.querySelector('.watch .date').innerHTML = formattedDate; requestAnimationFrame(clock); } clock(); </script> </body> </html>

配置参数

🎨 CSS 变量

--watch-color 手表边框颜色,默认 white
--screen-color 表盘背景色,默认 black

🖼️ 背景图片设置

background-image 设置表盘背景图 URL
background-size cover 覆盖整个表盘
background-position center 居中显示

使用说明

  1. 将完整代码复制到你的 HTML 文件中
  2. 修改 background-image 的 URL 为你想要的背景图
  3. 可以调整 widthheight 来改变手表大小
  4. 修改 --watch-color 调整边框颜色
  5. 页面加载后时钟会自动运行

#手表时钟(1)

评论