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>
相关推荐
baiduopenmap7 分钟前
百度世界2024精选公开课:基于地图智能体的导航出行AI应用创新实践
前端·人工智能·百度地图
loooseFish15 分钟前
小程序webview我爱死你了 小程序webview和H5通讯
前端
小牛itbull19 分钟前
ReactPress vs VuePress vs WordPress
开发语言·javascript·reactpress
请叫我欧皇i27 分钟前
html本地离线引入vant和vue2(详细步骤)
开发语言·前端·javascript
533_30 分钟前
[vue] 深拷贝 lodash cloneDeep
前端·javascript·vue.js
guokanglun36 分钟前
空间数据存储格式GeoJSON
前端
GIS瞧葩菜39 分钟前
局部修改3dtiles子模型的位置。
开发语言·javascript·ecmascript
zhang-zan1 小时前
nodejs操作selenium-webdriver
前端·javascript·selenium
ZBY520311 小时前
【Vue】 npm install amap-js-api-loader指南
javascript·vue.js·npm
猫爪笔记1 小时前
前端:HTML (学习笔记)【2】
前端·笔记·学习·html