登录/注册

在线倒计时器

更多

在线倒计时器设计

我将设计一个美观且功能完整的在线倒计时器,用户可以设置时间并开始倒计时,体验流畅的动画效果。

设计思路

下面是完整的实现代码:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>在线倒计时器</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            padding: 20px;
        }

        .container {
            background: rgba(255, 255, 255, 0.15);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
            padding: 40px;
            width: 100%;
            max-width: 500px;
            text-align: center;
            border: 1px solid rgba(255, 255, 255, 0.1);
        }

        h1 {
            color: white;
            margin-bottom: 30px;
            font-size: 2.5rem;
            text-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
        }

        .timer-display {
            font-size: 5rem;
            font-weight: 700;
            color: white;
            margin: 30px 0;
            text-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
            font-variant-numeric: tabular-nums;
            transition: all 0.3s ease;
        }

        .controls {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin: 20px 0;
        }

        button {
            background: rgba(255, 255, 255, 0.2);
            border: none;
            border-radius: 50px;
            color: white;
            cursor: pointer;
            font-size: 1rem;
            font-weight: 600;
            padding: 12px 25px;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        button:hover {
            background: rgba(255, 255, 255, 0.3);
            transform: translateY(-3px);
        }

        button:active {
            transform: translateY(1px);
        }

        #start-btn {
            background: rgba(46, 204, 113, 0.8);
        }

        #pause-btn {
            background: rgba(241, 196, 15, 0.8);
        }

        #reset-btn {
            background: rgba(231, 76, 60, 0.8);
        }

        .time-inputs {
            display: flex;
            justify-content: center;
            gap: 20px;
            margin-bottom: 25px;
        }

        .input-group {
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .input-group label {
            color: rgba(255, 255, 255, 0.9);
            margin-bottom: 8px;
            font-size: 0.9rem;
        }

        .input-group input {
            width: 100px;
            padding: 12px;
            border-radius: 10px;
            border: none;
            background: rgba(255, 255, 255, 0.2);
            color: white;
            font-size: 1.2rem;
            text-align: center;
            font-weight: bold;
        }

        input::-webkit-outer-spin-button,
        input::-webkit-inner-spin-button {
            -webkit-appearance: none;
            margin: 0;
        }

        input[type=number] {
            -moz-appearance: textfield;
        }

        .progress-wrapper {
            height: 10px;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 10px;
            margin: 30px 0;
            overflow: hidden;
        }

        .progress-bar {
            height: 100%;
            background: linear-gradient(90deg, #ff9a9e 0%, #fad0c4 100%);
            width: 100%;
            transition: width 1s linear;
        }

        .message {
            color: white;
            font-size: 1.2rem;
            margin-top: 20px;
            min-height: 30px;
            opacity: 0.9;
        }

        .ended {
            animation: pulse 1s infinite;
        }

        @keyframes pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.05); }
            100% { transform: scale(1); }
        }

        @media (max-width: 500px) {
            .container {
                padding: 30px 15px;
            }

            h1 {
                font-size: 2rem;
            }

            .timer-display {
                font-size: 3.5rem;
            }

            .controls {
                flex-wrap: wrap;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1><i class="fas fa-clock"></i> 在线倒计时器</h1>

        <div class="time-inputs">
            <div class="input-group">
                <label for="minutes">分钟</label>
                <input type="number" id="minutes" min="0" max="99" value="5">
            </div>

            <div class="input-group">
                <label for="seconds">秒钟</label>
                <input type="number" id="seconds" min="0" max="59" value="0">
            </div>
        </div>

        <div class="timer-display" id="display">05:00</div>

        <div class="controls">
            <button id="start-btn"><i class="fas fa-play"></i> 开始</button>
            <button id="pause-btn"><i class="fas fa-pause"></i> 暂停</button>
            <button id="reset-btn"><i class="fas fa-redo"></i> 重置</button>
        </div>

        <div class="progress-wrapper">
            <div class="progress-bar" id="progress"></div>
        </div>

        <div class="message" id="message">设置时间并点击开始</div>

        <audio id="alarm" src="https://assets.mixkit.co/sfx/preview/mixkit-alarm-digital-clock-beep-989.mp3"></audio>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 获取DOM元素
            const minutesInput = document.getElementById('minutes');
            const secondsInput = document.getElementById('seconds');
            const display = document.getElementById('display');
            const startBtn = document.getElementById('start-btn');
            const pauseBtn = document.getElementById('pause-btn');
            const resetBtn = document.getElementById('reset-btn');
            const progressBar = document.getElementById('progress');
            const message = document.getElementById('message');
            const alarm = document.getElementById('alarm');

            let countdown;
            let totalSeconds = 0;
            let remainingTime = 0;
            let isPaused = true;
            let isRunning = false;

            // 格式化时间为MM:SS
            function formatTime(seconds) {
                const mins = Math.floor(seconds / 60);
                const secs = seconds % 60;
                return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
            }

            // 更新显示
            function updateDisplay() {
                display.textContent = formatTime(remainingTime);

                // 更新进度条
                if (totalSeconds > 0) {
                    const percent = (remainingTime / totalSeconds) * 100;
                    progressBar.style.width = `${percent}%`;
                }

                // 最后10秒添加动画效果
                if (remainingTime <= 10 && remainingTime > 0) {
                    display.classList.add('ended');
                    message.textContent = `倒计时即将结束!`;
                } else {
                    display.classList.remove('ended');
                }
            }

            // 开始倒计时
            function startTimer() {
                if (!isRunning) {
                    // 获取输入的时间
                    const minutes = parseInt(minutesInput.value) || 0;
                    const seconds = parseInt(secondsInput.value) || 0;

                    totalSeconds = minutes * 60 + seconds;
                    remainingTime = totalSeconds;

                    if (totalSeconds <= 0) {
                        message.textContent = "请设置有效时间";
                        return;
                    }

                    isRunning = true;
                    isPaused = false;
                    message.textContent = "倒计时进行中...";
                } else if (isPaused) {
                    isPaused = false;
                    message.textContent = "倒计时进行中...";
                }

                startBtn.innerHTML = '<i class="fas fa-play"></i> 继续';

                // 开始计时器
                clearInterval(countdown);
                countdown = setInterval(() => {
                    if (!isPaused) {
                        remainingTime--;
                        updateDisplay();

                        if (remainingTime <= 0) {
                            clearInterval(countdown);
                            timerEnded();
                        }
                    }
                }, 1000);
            }

            // 暂停倒计时
            function pauseTimer() {
                if (!isRunning) return;

                isPaused = !isPaused;
                pauseBtn.innerHTML = isPaused 
                    ? '<i class="fas fa-play"></i> 继续' 
                    : '<i class="fas fa-pause"></i> 暂停';

                message.textContent = isPaused ? "倒计时已暂停" : "倒计时进行中...";
            }

            // 重置倒计时
            function resetTimer() {
                clearInterval(countdown);
                minutesInput.value = 5;
                secondsInput.value = 0;
                remainingTime = 5 * 60;
                totalSeconds = 5 * 60;
                isRunning = false;
                isPaused = true;
                startBtn.innerHTML = '<i class="fas fa-play"></i> 开始';
                pauseBtn.innerHTML = '<i class="fas fa-pause"></i> 暂停';
                message.textContent = "设置时间并点击开始";
                progressBar.style.width = "100%";
                display.classList.remove('ended');
                updateDisplay();
            }

            // 倒计时结束
            function timerEnded() {
                display.classList.add('ended');
                message.textContent = "时间到!倒计时结束!";

                // 播放提示音
                alarm.currentTime = 0;
                alarm.play();

                isRunning = false;
                isPaused = true;
                startBtn.innerHTML = '<i class="fas fa-play"></i> 开始';
            }

            // 事件监听
            startBtn.addEventListener('click', startTimer);
            pauseBtn.addEventListener('click', pauseTimer);
            resetBtn.addEventListener('click', resetTimer);

            // 输入验证
            minutesInput.addEventListener('change', function() {
                let value = parseInt(this.value) || 0;
                if (value < 0) value = 0;
                if (value > 99) value = 99;
                this.value = value;

                if (!isRunning) {
                    remainingTime = value * 60 + (parseInt(secondsInput.value) || 0);
                    updateDisplay();
                }
            });

            secondsInput.addEventListener('change', function() {
                let value = parseInt(this.value) || 0;
                if (value < 0) value = 0;
                if (value > 59) value = 59;
                this.value = value;

                if (!isRunning) {
                    remainingTime = (parseInt(minutesInput.value) || 0) * 60 + value;
                    updateDisplay();
                }
            });

            // 初始化显示
            resetTimer();
        });
    </script>
</body>
</html>

功能特点

  1. 时间设置

    • 可设置分钟(0-99)和秒钟(0-59)
    • 输入验证确保只接收有效数值
  2. 倒计时显示

    • 大字体显示剩余时间(分:秒)
    • 最后10秒添加脉动动画效果
    • 进度条直观展示剩余时间比例
  3. 控制功能

    • 开始/暂停/重置按钮
    • 暂停后可从当前位置继续
    • 开始按钮根据状态变化文本(开始/继续)
  4. 结束提醒

    • 时间到时播放提示音
    • 显示"时间到"消息
    • 数字继续脉动动画直到重置
  5. 响应式设计

    • 在手机和平板等移动设备上自适应
    • 触摸友好的控制按钮

使用方法

  1. 在分钟和秒钟输入框中设置所需时间
  2. 点击"开始"按钮启动倒计时
  3. 点击"暂停"可临时停止倒计时
  4. 点击"继续"可从中断处继续
  5. 点击"重置"可恢复初始设置
  6. 倒计时结束后会自动发出提示音

这个倒计时器采用了现代化的毛玻璃设计风格,有流畅的动画效果,非常适合用于厨房计时、会议计时、运动计时等各种场景。

怎么在倒计时器里应用定时

怎么在倒计时器里应用定时器

2023-10-30 06:02:13

数码管动态显示之倒计时器设计资料分享

1、数码管动态显示之倒计时器设计下面将介绍数码管显示的最后一个实例,该例的实现目标为通过拨码开关输入BCD码设置起始时间(单位为秒,高两位和低两位显示相同的数据,中间用:分开),由数码管显示倒计时

2022-07-27 16:50:45

proteus单片机实现60秒倒计时器

proteus单片机实现60秒倒计时器项目要实现的60s秒表倒计时器,用 AT89C51单片机的定时 / 计数器 T0 产生一秒的定时时间,实现

2021-11-10 08:18:13

红绿灯倒计时API开发文档

百度红绿灯倒计时API开发文档

资料下载 jf_01777102 2023-06-30 11:53:18

基于89C51单片机的实用99分钟倒计时器源程序

基于89C51单片机的实用99分钟倒计时器源程序

资料下载 木头1233 2023-05-15 10:34:35

单片机课设-60秒倒计时器

proteus单片机实现60秒倒计时器项目要实现的60s秒表倒计时器,用 AT89C51单片机的定时 / 计数器 T0 产生一秒的定时时间,实现

资料下载 佚名 2021-11-05 15:06:01

vhdl语言编写的9秒倒计时器资料下载

电子发烧友网为你提供vhdl语言编写的9秒倒计时器资料下载的电子资料下载,更有其他相关的电路图、源代码、课件教程、中文资料、英文资料、参考设计、用户指南、解决方案等资料,希望可以帮助到广大的电子工程师们。

资料下载 佚名 2021-04-25 08:41:49

使用单片机实现实用99分钟倒计时器的C语言实例免费下载

本文档的主要内容详细介绍的是使用单片机实现实用99分钟倒计时器的C语言实例免费下载。

资料下载 佚名 2021-03-29 11:48:28

一种可变信息倒九秒半程倒计时器

本实用新型涉及倒计时器技术领域,具体为一种可变信息倒九秒半程倒计时器。背景技术:倒计时器是由单片机为核心的器件组成的一个

2021-09-02 07:40:07

基于STM32的正倒计时器程序设计

基于STM32的正倒计时器程序设计课程设计要求如下:使用通用定时器定时产生0.01秒基时,进而产生秒、分,在LCD上显示分钟、秒、秒小数各2位;用4个按钮控制正

2021-07-21 06:12:20

如何利用前后台系统架构去实现倒计时器

如何利用前后台系统架构去实现倒计时器?怎样去编写其程序代码?

2021-07-16 10:16:13

智能倒计时器

选择 5 位数码管做显示,实现 5 种倒计时模式,通过控制按键进行选择1、99999s-0s2、9999s-0s3、999s-0s4、99s-0s5、9s-0s6、开始值由人工输入-0s求c程序及仿真图

2021-01-04 12:17:34

双通道两位计数IC 正/倒计时器芯片

LCD界面显示图:一,产品功能概述:该产品型号为ZH151C,采用3A级高端芯片。这是一款正/倒计时器IC。它可以直接驱动1/4duty的4位LCD。最大计时为59:59或99:59。通过绑定选择

2020-04-16 17:00:05

篮球倒计时 案例

功能描述 本工程包含了两个按键和4位数码管显示,共同实现一个篮球24秒倒计时、 并具有暂停和重新计数复位的功能。具体功能如下: 1. 数码管显示秒十位、秒个位、0.1 秒和 0.01 秒。 2. 上

2019-08-31 17:47:20

倒计时计数器数码管不会动

做了一个24秒倒计时的计时器,但是不会倒计时,有没有大佬知道是为啥(试过调电阻了)

2019-06-27 18:55:51
7天热门专题 换一换
相关标签