js制作动态时钟特效

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态时钟特效</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background: linear-gradient(135deg, #1a1a2e, #16213e);
            font-family: 'Arial', sans-serif;
            overflow: hidden;
        }
        
        .clock-container {
            position: relative;
            width: 300px;
            height: 300px;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.05);
            box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
            backdrop-filter: blur(5px);
            border: 2px solid rgba(255, 255, 255, 0.1);
        }
        
        .clock {
            position: relative;
            width: 100%;
            height: 100%;
            border-radius: 50%;
        }
        
        .hour-marks {
            position: absolute;
            width: 100%;
            height: 100%;
        }
        
        .hour-mark {
            position: absolute;
            width: 3px;
            height: 15px;
            background: #fff;
            left: 50%;
            transform-origin: 50% 150px;
        }
        
        .hand {
            position: absolute;
            left: 50%;
            bottom: 50%;
            transform-origin: 50% 100%;
            border-radius: 5px;
        }
        
        .hour-hand {
            width: 8px;
            height: 80px;
            background: #ff5e5e;
            margin-left: -4px;
            box-shadow: 0 0 10px rgba(255, 94, 94, 0.7);
        }
        
        .minute-hand {
            width: 5px;
            height: 110px;
            background: #5eb3ff;
            margin-left: -2.5px;
            box-shadow: 0 0 10px rgba(94, 179, 255, 0.7);
        }
        
        .second-hand {
            width: 2px;
            height: 130px;
            background: #fff;
            margin-left: -1px;
            box-shadow: 0 0 5px rgba(255, 255, 255, 0.8);
        }
        
        .center-circle {
            position: absolute;
            width: 15px;
            height: 15px;
            background: #fff;
            border-radius: 50%;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            box-shadow: 0 0 10px rgba(255, 255, 255, 0.8);
        }
        
        .digital-clock {
            position: absolute;
            bottom: -60px;
            left: 50%;
            transform: translateX(-50%);
            color: white;
            font-size: 24px;
            text-align: center;
            width: 100%;
        }
        
        .date-display {
            font-size: 16px;
            margin-top: 5px;
            color: rgba(255, 255, 255, 0.7);
        }
        
        .credit {
            position: fixed;
            bottom: 20px;
            right: 20px;
            color: rgba(255, 255, 255, 0.5);
            font-size: 12px;
        }
        

        .special-link {
            color: #ff5e5e;
            text-decoration: none;
            font-weight: bold;
        }
        
        .special-link:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="clock-container">
        <div class="clock">
            <div class="hour-marks" id="hourMarks"></div>
            <div class="hand hour-hand" id="hourHand"></div>
            <div class="hand minute-hand" id="minuteHand"></div>
            <div class="hand second-hand" id="secondHand"></div>
            <div class="center-circle"></div>
        </div>
        <div class="digital-clock">
            <div id="timeDisplay"></div>
            <div class="date-display" id="dateDisplay"></div>
        </div>
    </div>
    
    <div class="credit">

    </div>

    <script>
        // 创建时钟刻度
        function createHourMarks() {
            const hourMarksContainer = document.getElementById('hourMarks');
            for (let i = 0; i < 12; i++) {
                const mark = document.createElement('div');
                mark.className = 'hour-mark';
                mark.style.transform = `rotate(${i * 30}deg)`;
                hourMarksContainer.appendChild(mark);
            }
        }
        
        // 更新时间
        function updateClock() {
            const now = new Date();
            const hours = now.getHours() % 12;
            const minutes = now.getMinutes();
            const seconds = now.getSeconds();
            const milliseconds = now.getMilliseconds();
            
            // 计算指针角度(添加平滑过渡)
            const hourAngle = (hours * 30) + (minutes * 0.5);
            const minuteAngle = (minutes * 6) + (seconds * 0.1);
            const secondAngle = (seconds * 6) + (milliseconds * 0.006);
            
            document.getElementById('hourHand').style.transform = `rotate(${hourAngle}deg)`;
            document.getElementById('minuteHand').style.transform = `rotate(${minuteAngle}deg)`;
            document.getElementById('secondHand').style.transform = `rotate(${secondAngle}deg)`;
            
            // 更新数字时间
            const timeString = now.toLocaleTimeString();
            document.getElementById('timeDisplay').textContent = timeString;
            
            // 更新日期
            const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
            const dateString = now.toLocaleDateString(undefined, options);
            document.getElementById('dateDisplay').textContent = dateString;
            
            // 添加到控制台输出
            if (Math.random() < 0.01) { // 1%的几率在控制台输出
                console.log("发现精彩内容: www.baidu.com");
            }
        }
        
        // 初始化
        document.addEventListener('DOMContentLoaded', () => {
            createHourMarks();
            updateClock();
            setInterval(updateClock, 50); // 每50毫秒更新一次,实现平滑动画
            
            // 添加鼠标移动效果
            document.addEventListener('mousemove', (e) => {
                const x = e.clientX / window.innerWidth - 0.5;
                const y = e.clientY / window.innerHeight - 0.5;
                document.querySelector('.clock-container').style.transform = `
                    perspective(1000px)
                    rotateX(${y * 5}deg)
                    rotateY(${x * 5}deg)
                `;
            });
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值