时钟倒计时效果

1、思路:输入的时间减去现在的时间就是剩余的时间,也即是倒计时,但不能直接用时间相减。

可使用时间戳==》用户输入时间总的毫秒数减去现在时间的总毫秒数,得到的就是剩余时间

最后再将剩余时间总的毫秒数转换成天、时、分、秒

【转换公式】

d=parseInt(总秒数/60/60/24)     计算天数

h=parseInt(总秒数/60/60%24)     计算小时

m=parseInt(总秒数/60%60)     计算分钟数

s=parseInt(总秒数%60)     计算秒数

function countTime(time) {
    //获取当前时间总的毫秒数
    var nowTime = +new Date();
    //获取用户输入时间总毫秒数
    var inputTime = +new Date(time);
    //获取两者之间相差的秒数
    var times = (inputTime - nowTime) / 1000;
    var d = parseInt(times / 60 / 60 / 24); //天
    d = d < 10 ? "0" + d : d;//当数值小于10的时候,在前面加0
    var h = parseInt((times / 60 / 60) % 24); //时
    h = h < 10 ? "0" + h : h;//同上
    var m = parseInt((times / 60) % 60); //分
    m = m < 10 ? "0" + m : m;//同上
    var s = parseInt(times % 60); //秒
    s = s < 10 ? "0" + s : s;//同上
    return d + "天" + h + "时" + m + "分" + s + "秒";
}
console.log(countTime("2022 /3 / 22 16:10:00"));

 2、获取倒计时

思路:同上,在此增加了两种小的知识点

获取事件戳的方法:可用构造函数

拼接字符串的方法:拼字符串*****  `` 可以 使用 ${变量} --这个整体 最后执行的时候 会被解析变量的值

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

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>

<body>
    <div id="box"></div>
    <script>
        var tDate = new Date("2022-03-25 18:00:00");
        setInterval(function() {
            let now = new Date();
            //获取两个时间戳的差(毫秒)
            // (方法1)var temp=tDate-now;
            //(方法2)var temp = tDate.getTime() - now.getTime();
            // (方法3)Date.now() 静态方法--构造函数直接使用的方法 --- 获取当前时间戳
            var temp = tDate.getTime() - Date.now();

            var totalSeconds = parseInt(temp / 1000); //总的秒数
            var sec = totalSeconds % 60; //封60进位余多少秒

            var totalMins = parseInt(temp / 1000 / 60); //总的分钟数
            var mins = totalMins % 60; //封60进位余多少分钟

            var totalHours = parseInt(temp / 1000 / 60 / 60); //总的小时数
            //数字小于10时在前面补0,效果好看
            if (sec < 10) sec = "0" + sec;
            if (mins < 10) sec = "0" + mins;
            if (totalHours < 10) totalHours = "0" + totalHours;
            //字符串拼接方法1
            var str = totalHours + ":" + mins + ":" + sec;
            console.log(str);
            //字符串拼接方法2(好用)
            var str2 = `离下课还有 ${totalHours} 小时 ${mins}分钟 ${sec}秒`;
            box.innerHTML = str2;
        }, 1000);
    </script>
</body>

</html>

 

好的,你可以使用 HTML、CSS 和 JavaScript 实现时钟翻牌器倒计时效果。以下是一个简单的实现示例: HTML 代码: ```html <div class="flip-clock"> <div class="flip-clock__days flip-clock__part"></div> <div class="flip-clock__hours flip-clock__part"></div> <div class="flip-clock__minutes flip-clock__part"></div> <div class="flip-clock__seconds flip-clock__part"></div> </div> ``` CSS 代码: ```css .flip-clock { display: flex; justify-content: center; align-items: center; height: 100px; } .flip-clock__part { display: flex; flex-direction: row-reverse; } .flip-clock__part > * { position: relative; width: 40px; height: 50px; margin-right: -5px; transform-origin: bottom center; font-size: 36px; font-weight: bold; color: #fff; background-color: #000; } .flip-clock__part > * > * { position: absolute; top: 0; left: 0; right: 0; bottom: 0; display: flex; justify-content: center; align-items: center; font-size: 16px; font-weight: normal; color: #000; background-color: #fff; transform: rotateX(0deg); transition: transform 0.5s; } .flip-clock__part > * > *:last-child { transform: rotateX(-180deg); } ``` JavaScript 代码: ```javascript function updateClock(endtime) { const total = Date.parse(endtime) - Date.parse(new Date()); const seconds = Math.floor((total / 1000) % 60); const minutes = Math.floor((total / 1000 / 60) % 60); const hours = Math.floor((total / (1000 * 60 * 60)) % 24); const days = Math.floor(total / (1000 * 60 * 60 * 24)); document.querySelector('.flip-clock__days').innerHTML = pad(days, 2); document.querySelector('.flip-clock__hours').innerHTML = pad(hours, 2); document.querySelector('.flip-clock__minutes').innerHTML = pad(minutes, 2); document.querySelector('.flip-clock__seconds').innerHTML = pad(seconds, 2); } function pad(num, size) { let s = num + ''; while (s.length < size) s = '0' + s; return s; } const endTime = new Date('2021-12-31T23:59:59'); setInterval(() => updateClock(endTime), 1000); ``` 在这个示例中,我们使用了一个名为 `flip-clock` 的容器来包含四个翻牌器元素,分别表示天、小时、分钟和秒。每个翻牌器元素都由两个子元素组成,一个用于显示当前值,另一个用于显示下一个值。我们使用 CSS 的 3D 转换效果来实现翻牌器的动画效果。在 JavaScript 中,我们使用 `setInterval` 函数来更新翻牌器的值,并将其格式化为两位数字。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值