比赛倒计时软件(界面非常漂亮)基于html开发的源代码真心值得拥有

这篇文章介绍了如何使用HTML、CSS和JavaScript创建一个可交互的倒计时计时器,包括开始、暂停、全屏切换和自定义时间功能。
摘要由CSDN通过智能技术生成

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>倒计时计时器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .header {
            background-color: #88001b;
            color: white;
            height: 180px;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 100px;
        }

        .timer {
            display: flex;
            justify-content: center;
            align-items: center;
            height: calc(100vh - 280px);
            font-size: 30vw;
        }

        .buttons {
            position: fixed;
            bottom: 0;
            width: 100%;
            display: flex;
            justify-content: space-around;
            padding: 10px 0;
        }

        button {
            font-size: 20px;
            padding: 10px 20px;
        }
    </style>
</head>

<body>
    <div class="header">比赛倒计时</div>
    <div class="timer" id="timer">01:00</div>
    <div class="buttons">
        <button id="startTimer">开始计时</button>
        <button id="pauseTimer">暂停计时</button>
        <button id="fullscreenToggle">全屏显示</button>
        <button id="clearData">清空数据</button>
        <button id="customTime">自定义时间</button>
    </div>

    <audio id="startSound" src="start-timer-sound.mp3"></audio>
    <audio id="warningSound" src="warning-timer-sound.mp3"></audio>

    <script>
        let totalSeconds = 60;
        let countdown;
        let warningPlayed = false;
        let isPaused = false;

        function startTimer() {
            document.getElementById('startSound').play();
            if (!countdown && !isPaused) {
                countdown = setInterval(timerFunction, 1000);
            } else if (isPaused) {
                countdown = setInterval(timerFunction, 1000);
                isPaused = false;
            }
        }

        function pauseTimer() {
            clearInterval(countdown);
            countdown = null;
            isPaused = true;
        }

        function timerFunction() {
            if (!isPaused) {
                totalSeconds--;
                const minutes = Math.floor(totalSeconds / 60);
                const seconds = totalSeconds % 60;
                document.getElementById('timer').textContent = `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;

                if (totalSeconds === 10 && !warningPlayed) {
                    document.getElementById('warningSound').play();
                    warningPlayed = true;
                }

                if (totalSeconds <= 0) {
                    clearInterval(countdown);
                    document.getElementById('timer').textContent = "时间到";
                }
            }
        }

        // 按空格键开始或暂停计时
        document.addEventListener('keydown', function (event) {
            if (event.code === 'Space') {
                event.preventDefault();
                if (!countdown || isPaused) {
                    startTimer();
                } else {
                    pauseTimer();
                }
            }
        });

        // 按回车键清空数据
        document.getElementById('clearData').addEventListener('click', function () {
            totalSeconds = 60;
            clearInterval(countdown);
            countdown = null;
            isPaused = false;
            document.getElementById('timer').textContent = "01:00";
        });

        // 弹出自定义时间对话框
        document.getElementById('customTime').addEventListener('click', function () {
            const inputTime = prompt('请输入倒计时时间(单位:秒)');
            const time = parseInt(inputTime);
            if (isNaN(time)) {
                alert('输入的时间无效,请输入一个有效的数字');
            } else {
                totalSeconds = time;
                document.getElementById('timer').textContent = `${String(Math.floor(time / 60)).padStart(2, '0')}:${String(time % 60).padStart(2, '0')}`;
                if (document.fullscreenElement) {
                    document.exitFullscreen();
                }
            }
        });

        document.getElementById('startTimer').addEventListener('click', startTimer);
        document.getElementById('pauseTimer').addEventListener('click', pauseTimer);

        document.getElementById('fullscreenToggle').addEventListener('click', function () {
            if (!document.fullscreenElement) {
                document.documentElement.requestFullscreen().then(() => {
                    this.textContent = '退出全屏模式';
                }).catch(err => {
                    alert(`无法进入全屏模式: ${err.message}`);
                });
            } else {
                if (document.exitFullscreen) {
                    document.exitFullscreen().then(() => {
                        this.textContent = '全屏显示';
                    }).catch(err => {
                        alert(`无法退出全屏模式: ${err.message}`);
                    });
                }
            }
        });
    </script>
</body>

</html>

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大飞哥软件自习室

希望支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值