JS 京东倒计时案例

简介: JS 京东倒计时案例

我们利用学过的定时器来实现一下京东倒计时的案例:

效果如下:

JS区域实现:

<script>document.addEventListener('DOMContentLoaded',function(){
varhour=document.querySelector('.left');
varminute=document.querySelector('.center');
varsecond=document.querySelector('.right');
timer();
varclock=window.setInterval(function(){
timer();
            },1000)
functiontimer(){
varnowtime=+newDate();
varaimtime=+newDate("2022-3-8 19:30:00");
time=(aimtime-nowtime)/1000;
varh=parseInt(time/60/60%24);
h=h<10?'0'+h:h;
hour.innerHTML=h;
varm=parseInt(time/60%60);
m=m<10?'0'+m:m;
minute.innerHTML=m;
vars=parseInt(time%60);
s=s<10?'0'+s:s;
second.innerHTML=s;
            }
        })
</script>

完整代码实现:

<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{
margin: 0px;
padding: 0px;
        }
        .box{
box-sizing: border-box;
margin: 100pxauto;
width: 500px;
height: 600px;
background-color: rgb(216, 0, 0);
        }
        .top{
box-sizing: border-box;
width: 500px;
height: 350px;
padding: 5px;
padding-top: 35px;
text-align: center;
line-height: 130px;
font-size: 70px;
color: rgb(255, 255, 255);
font-weight: bold;
        }
         .bottom{
padding: 10px;
         }
        .left{
box-sizing: border-box;
margin: 10px;
float: left;
width: 140px;
height: 140px;
background-color: rgb(0, 0, 0);
color: azure;
padding: 19px;
font-size: 80px;
font-weight: bold;
        }
        .center{
box-sizing: border-box;
margin: 10px;
float: left;
width: 140px;
height: 140px;
background-color: rgb(0, 0, 0);
color: azure;
padding: 19px;
font-size: 80px;
font-weight: bold;
        }
        .right{
box-sizing: border-box;
margin: 10px;
float: left;
width: 140px;
height: 140px;
background-color: rgb(0, 0, 0);
color: azure;
padding: 19px;
font-size: 80px;
font-weight: bold;
        }
</style></head><body><script>document.addEventListener('DOMContentLoaded',function(){
varhour=document.querySelector('.left');
varminute=document.querySelector('.center');
varsecond=document.querySelector('.right');
timer();
varclock=window.setInterval(function(){
timer();
            },1000)
functiontimer(){
varnowtime=+newDate();
varaimtime=+newDate("2022-3-8 19:30:00");
time=(aimtime-nowtime)/1000;
varh=parseInt(time/60/60%24);
h=h<10?'0'+h:h;
hour.innerHTML=h;
varm=parseInt(time/60%60);
m=m<10?'0'+m:m;
minute.innerHTML=m;
vars=parseInt(time%60);
s=s<10?'0'+s:s;
second.innerHTML=s;
            }
        })
</script><divclass="box"><divclass="top">11.11<br>秒杀倒计时</div><divclass="bottom"><divclass="left"></div><divclass="center"></div><divclass="right"></div></div>   </div></body></html>
相关文章
|
18天前
|
JavaScript 前端开发
js变量的作用域、作用域链、数据类型和转换应用案例
【4月更文挑战第27天】JavaScript 中变量有全局和局部作用域,全局变量在所有地方可访问,局部变量只限其定义的代码块。作用域链允许变量在当前块未定义时向上搜索父级作用域。语言支持多种数据类型,如字符串、数字、布尔值,可通过 `typeof` 检查类型。转换数据类型用 `parseInt` 或 `parseFloat`,将字符串转为数值。
18 1
|
3天前
|
JavaScript 前端开发
JavaScript解决生日倒计时的问题
JavaScript解决生日倒计时的问题
|
3天前
|
JavaScript 前端开发
JavaScript编写一个倒计时
JavaScript编写一个倒计时
|
9天前
|
JavaScript 前端开发
JavaScript-jQuery的使用 + JS的案例
JavaScript-jQuery的使用 + JS的案例
17 0
|
15天前
|
XML 前端开发 JavaScript
jQuery与javascript的区别+案例 锋芒毕露
jQuery与javascript的区别+案例 锋芒毕露
|
18天前
|
编解码 JavaScript 安全
JS逆向过程中中文编解码的小案例详解
JS逆向过程中中文编解码的小案例详解
16 1
|
18天前
|
JavaScript 前端开发
js的let、const、var的区别以及应用案例
【4月更文挑战第27天】ES6 中,`let` 和 `const` 是新增的变量声明关键字,与 `var` 存在显著差异。`let` 允许重新赋值,而 `const` 不可,且两者都具有块级作用域。`var` 拥有函数级作用域,并可在函数内任意位置访问。`let` 和 `const` 声明时必须初始化,而 `var` 不需。根据需求选择使用:局部作用域用 `let`/`const`,全局或函数范围用 `var`,不可变值用 `const`。
24 2
|
18天前
android-agent-web中js-bridge案例
android-agent-web中js-bridge案例
24 2
|
18天前
|
开发框架 前端开发 JavaScript
采用C#.Net +JavaScript 开发的云LIS系统源码 二级医院应用案例有演示
技术架构:Asp.NET CORE 3.1 MVC + SQLserver + Redis等 开发语言:C# 6.0、JavaScript 前端框架:JQuery、EasyUI、Bootstrap 后端框架:MVC、SQLSugar等 数 据 库:SQLserver 2012
27 0