• 最简时分秒倒计时


    代码:单页面

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>倒计时</title>
        <style>
            #currentTime{
                text-align: right;
                font-size: 20px;
                margin-right: 10px;
            }
            .showTime{
                 90%;
                margin: 0 auto;
                font-size: 120px;
                text-align: center;
                margin-top: 10%;
            }
            .setTime{
                 750px;
                margin: 0 auto;
                position: absolute;
                bottom: 80px;
                left: 50%;
                margin-left: -375px;
            }
            .setTime .form{
                display: flex;
                justify-content: left;
            }
            .setTime .form div{
                margin-left: 30px;
            }
        </style>
    </head>
    <body>
        <p id="currentTime">显示实时时间</p>
        <p style="font-size:36px;margin-left: 40px;">您的演讲时间剩余:</p>
        <div class="showTime">
            <span id="showHour">0</span>&nbsp;时
            <span id="showMin">0</span>&nbsp;分
            <span id="showSec">0</span>&nbsp;秒
        </div>
        <div class="setTime">
            <p style="margin-left:30px;">请设置倒计时的时间:</p>
            <div class="form">
                <div>
                    <label for="hour">时:</label>
                    <input type="text" id="hour" value="0">
                </div>
                <div>
                    <label for="min">分:</label>
                    <input type="text" id="min" value="0">
                </div>
                <div>
                    <label for="sec">秒:</label>
                    <input type="text" id="sec" value="0">
                </div>
                <div>
                    <button onclick="startCountDown()" id="start">确定</button>
                </div>
            </div>
        </div>
        <script>
            // 设置当前时间
            var currentTime = setInterval(function(){
                var date = new Date();
                document.getElementById('currentTime').innerHTML = date.getFullYear() + '/' + checkTime((date.getMonth()+1)) + '/' + checkTime(date.getDate()) + ' ' + checkTime(date.getHours()) + ':' + checkTime(date.getMinutes()) + ':' + checkTime(date.getSeconds());
            },1000);
            // 设置月/天/时/分/秒等的个位数前加0 
            function checkTime(num){
                if(num < 10) return ("0" + num);
                return num;
            }
            // 设置倒计时
            var startCountDown = function(){
                document.getElementById('start').disabled = true;
                var endTime = formatTime(document.getElementById('hour').value, document.getElementById('min').value, document.getElementById('sec').value);
                var start = setInterval(function(){
                    endTime--;
                    // console.log(endTime);
                    if(endTime >= 0){
                        document.getElementById('showHour').innerHTML = checkTime(parseInt(endTime/60/60,10)); // 剩余多少小时
                        document.getElementById('showMin').innerHTML = checkTime(parseInt(endTime/60%60,10)); // 剩余多少分钟
                        document.getElementById('showSec').innerHTML = checkTime(parseInt(endTime%60,10)); // 剩余多少秒
                    }else{
                        clearInterval(start);
                    }
                },1000);
            }
            // 格式化设置时间,返回时间以秒为单位
            var formatTime = function(hour, min ,sec){
                document.getElementById('showHour').innerHTML = checkTime(parseInt(hour));
                document.getElementById('showMin').innerHTML = checkTime(parseInt(min));
                document.getElementById('showSec').innerHTML = checkTime(parseInt(sec));
                return (parseInt(hour*60*60) + parseInt(min*60) + parseInt(sec)); // 返回多少秒
            }
        </script>
    </body>
    </html>
  • 相关阅读:
    1210 BBS admin后台管理及侧边栏筛选个人站点
    1209 BBS 登录
    更换 npm 源国内镜像 cnpm
    Linux软件管理
    apt-get / yum 软件安装源(国内)
    修改pip源为国内镜像源(加速下载)
    修改浏览器搜索引擎:网址应该如何填写
    如何根据实际问题选择一个合适的数学模型
    安装向量和矩阵运算库函数
    配置编译器(GCC和GFortran)
  • 原文地址:https://www.cnblogs.com/maoriaty/p/8372178.html
Copyright © 2020-2023  润新知