HTML+CSS科技感时钟(附源码!!!)

预览效果

源码(直接复制使用)

html 复制代码
<!DOCTYPE html>
<html lang="zh-Hans">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>科技感时钟</title>
    <style>
        :root {
            --color2: #f67280;
            --color4: #c06c84;
            --color6: #6c5b7b;
        }

        * {
            margin: 0;
            padding: 0;
        }

        html {
            font-size: 14px;
        }

        body {
            width: 100vw;
            height: 100vh;
            background-color: #000;
            display: flex;
            justify-content: center;
            align-items: center;
            font-family: 'Montserrat', sans-serif, Arial, 'Microsoft Yahei';
        }

        .clock-container {
            position: relative;
            width: 380px;
            height: 80px;
            border-radius: 10px;
            background-image: linear-gradient(135deg, var(--color2), var(--color4), var(--color6));
            display: flex;
            justify-content: center;
            align-items: center;
            animation: animate 4s linear infinite;
        }

        .clock-container::after,
        .clock-container::before {
            position: absolute;
            content: "";
            background-image: inherit;
            width: 100%;
            height: 100%;
            z-index: -1;
            border-radius: 10px;
            filter: blur(15px);
        }

        .clock-container::before {
            filter: blur(100px);
        }

        .clock-display {
            position: absolute;
            left: 10px;
            right: 10px;
            top: 10px;
            bottom: 10px;
            background-color: #2b2a2a;
            border-radius: 8px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .clock-display p {
            font-size: 50px;
            font-weight: bold;
            color: transparent;
            letter-spacing: 2px;
            background-image: linear-gradient(135deg, var(--color2), var(--color4), var(--color6));
            background-clip: text;
            -webkit-background-clip: text;
        }

        @keyframes animate {
            100% {
                filter: hue-rotate(360deg);
            }
        }
    </style>
</head>

<body>
    <div class="clock-container">
        <div class="clock-display">
            <p id="time"></p>
        </div>
    </div>
    <script>
        const timeEl = document.querySelector("#time");

        function updateTime(timeEl) {
            const date = new Date();
            let h = date.getHours();
            let m = date.getMinutes();
            let s = date.getSeconds();
            const dayNight = h >= 12 ? "PM" : "AM";

            h = h % 12 || 12;

            h = h < 10 ? "0" + h : h;
            m = m < 10 ? "0" + m : m;
            s = s < 10 ? "0" + s : s;

            timeEl.textContent = `${h}:${m}:${s} ${dayNight}`;
        }

        setInterval(() => {
            updateTime(timeEl);
        }, 1000);
    </script>
</body>

</html>

JavaScript逻辑代码的解释

  • const timeEl = document.querySelector("#time"); 获取显示时间的 p标签元素。

  • function updateTime(timeEl) 定义更新时间的函数。

    • const date = new Date(); 创建一个新的 Date 对象。

    • let h = date.getHours(); 获取当前小时。

    • let m = date.getMinutes(); 获取当前分钟。

    • let s = date.getSeconds(); 获取当前秒。

    • const dayNight = h >= 12 ? "PM" : "AM"; 判断是上午还是下午。

    • h = h % 12 || 12; 将小时转换为 12 小时制。

    • h = h < 10 ? "0" + h : h; 如果小时小于 10,前面补零。

    • m = m < 10 ? "0" + m : m; 如果分钟小于 10,前面补零。

    • s = s < 10 ? "0" + s : s; 如果秒小于 10,前面补零。

    • timeEl.textContent = h : {h}: h:{m}😒{s} ${dayNight}; 更新时间显示。

    • timeEl.textContent = h : {h}: h:{m}😒{s} ${dayNight}; 更新时间显示。

    • setInterval(() => { updateTime(timeEl); }, 1000); 每秒调用一次 updateTime 函数,更新时间。

还有什么不懂的欢迎大家留言!

欢迎小白新手加群交流

相关推荐
江城开朗的豌豆1 分钟前
Redux 与 React-Redux:从“全局变量”到“响应式状态”的优雅之道
前端·javascript·react.js
枫叶丹42 分钟前
【Qt开发】常用控件(七)-> styleSheet
开发语言·css·qt·qss
excel4 分钟前
JavaScript 错误处理与调试全攻略:从 try/catch 到全局兜底
前端
江城开朗的豌豆8 分钟前
点击弹窗外部自动关闭?一个useRef Hook就搞定!
前端·javascript·react.js
JarvanMo24 分钟前
跨平台开发的隐性成本
前端·后端
qq_4152162530 分钟前
html pc和移动端共用一个页面,移动端通过缩放达到适配页面,滚动飘窗
前端·html
前端小巷子1 小时前
watch 与 computed:Vue3响应式的抉择
前端·vue.js·面试
ss2731 小时前
手写MyBatis第36弹:MyBatis执行流程中SQL命令类型解析
前端·javascript·html
IT_陈寒1 小时前
Python数据处理太慢?这5个Pandas优化技巧让速度提升300%!
前端·人工智能·后端
花落文心2 小时前
使用 html2canvas + jspdf 实现页面元素下载为pdf文件
前端·javascript·pdf