HTML的自动定义倒计时,这个配色存一下

html 复制代码
<!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>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Arial', sans-serif;
        }

        body {
            background: linear-gradient(to right, #3a1c71, #d76d77, #ffaf7b);
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #fff;
        }

        .container {
            text-align: center;
            background-color: rgba(0, 0, 0, 0.4);
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
        }

        h1 {
            font-size: 2.5rem;
            margin-bottom: 20px;
        }

        .input-group {
            margin-bottom: 20px;
        }

        label {
            font-size: 1.2rem;
        }

        input[type="number"] {
            padding: 10px;
            font-size: 1rem;
            border: none;
            border-radius: 5px;
            margin: 5px;
            width: 60px;
            text-align: center;
        }

        .buttons {
            margin-top: 20px;
        }

        button {
            background-color: #ff7e5f;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            font-size: 1.1rem;
            color: #fff;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #feb47b;
        }

        .countdown-display {
            margin-top: 30px;
            font-size: 3rem;
            font-weight: bold;
        }

        .countdown-finished {
            color: #fffa65;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>倒计时</h1>
        <div class="input-group">
            
            <input type="number" id="hours" min="0" max="23" value="0">
            <label for="hours">小时</label>
            
            <input type="number" id="minutes" min="0" max="59" value="0">
            <label for="minutes">分钟</label>
           
            <input type="number" id="seconds" min="0" max="59" value="0">
             <label for="seconds">秒</label>
        </div>
        <div class="buttons">
            <button onclick="startCountdown()">开始倒计时</button>
            <button onclick="pauseCountdown()">暂停</button>
            <button onclick="resetCountdown()">重置</button>
        </div>
        <div class="countdown-display" id="countdown-display">00:00:00</div>
    </div>

    <script>
        let countdownInterval;
        let totalSeconds;
        let isPaused = false;

        function startCountdown() {
            // 如果正在暂停,直接继续计时
            if (isPaused) {
                isPaused = false;
                countdownInterval = setInterval(updateCountdown, 1000);
                return;
            }

            const hours = parseInt(document.getElementById('hours').value, 10);
            const minutes = parseInt(document.getElementById('minutes').value, 10);
            const seconds = parseInt(document.getElementById('seconds').value, 10);

            totalSeconds = hours * 3600 + minutes * 60 + seconds;

            if (totalSeconds > 0) {
                clearInterval(countdownInterval);
                countdownInterval = setInterval(updateCountdown, 1000);
            }
        }

        function updateCountdown() {
            if (totalSeconds > 0) {
                totalSeconds--;
                const hrs = Math.floor(totalSeconds / 3600);
                const mins = Math.floor((totalSeconds % 3600) / 60);
                const secs = totalSeconds % 60;

                document.getElementById('countdown-display').textContent =
                    `${formatTime(hrs)}:${formatTime(mins)}:${formatTime(secs)}`;
            } else {
                clearInterval(countdownInterval);
                document.getElementById('countdown-display').textContent = "倒计时结束";
                document.getElementById('countdown-display').classList.add('countdown-finished');
            }
        }

        function formatTime(time) {
            return time < 10 ? `0${time}` : time;
        }

        function resetCountdown() {
            clearInterval(countdownInterval);
            document.getElementById('countdown-display').textContent = "00:00:00";
            document.getElementById('hours').value = 0;
            document.getElementById('minutes').value = 0;
            document.getElementById('seconds').value = 0;
            document.getElementById('countdown-display').classList.remove('countdown-finished');
            isPaused = false;  // 重置暂停状态
        }

        function pauseCountdown() {
            if (!isPaused) {
                clearInterval(countdownInterval);
                isPaused = true;
            } else {
                startCountdown();
            }
        }
    </script>
</body>
</html>
相关推荐
不会敲代码14 小时前
从原子CSS到TailwindCSS:现代前端样式解决方案全解析
前端·css·react.js
Wect4 小时前
LeetCode 102. 二叉树的层序遍历:图文拆解+代码详解
前端·算法·typescript
简离4 小时前
VSCode Git Bash 终端:告别内置vi,直接用VSCode编辑交互内容
前端
冴羽5 小时前
2026 年 JavaScript 框架 3 大趋势
前端·javascript·react.js
思茂信息5 小时前
基于CST 3D Combined功能的以太网口RE仿真
开发语言·javascript·单片机·嵌入式硬件·matlab·3d
一枚前端小姐姐5 小时前
Vue3 组合式 API(setup + script setup)实战
前端·vue.js
一拳不是超人5 小时前
从“必选项”到“性能包袱”:为什么现代框架开始“抛弃”虚拟 DOM?
前端·javascript·架构
田里的水稻5 小时前
OE_ubuntu24.04如何安装中文简体拼音输入法
运维·前端·chrome
wordbaby5 小时前
🚀 从零到一实战:基于 Taro 构建纯血鸿蒙 (HarmonyOS NEXT) 应用踩坑全指南
前端
慧一居士5 小时前
ESM 在前端开发中的介绍和使用指导
前端