JQuery仿小米手机抢购页面倒计时效果
1、效果及功能说明
通过对时间的控制来告诉用户一个活动还剩多少时间,精确到秒。
2、实现原理
首先定义活动的截至的时间,要重年份精确到毫秒,在获得当前的年份到秒钟,在用截至时间,减去现在的时间,剩下的还有多少的时间就把还剩下的时间给显示出来就得出了离截止日期还有多久。
主要代码
varstartTime=newDate(); //获得当前的时间 startTime.setFullYear(2016,5,27); //调用设置年份 startTime.setHours(23); //调用设置指定的时间的小时字段 startTime.setMinutes(59); //调用设置指定时间的分钟字段 startTime.setSeconds(59); //调用设置指定时间的秒钟字段 startTime.setMilliseconds(999); //调用置指定时间的毫秒字段 varEndTime=startTime.getTime(); //获得截至的时间 varnMS=EndTime-NowTime.getTime(); //截至时间减去当前时间获得剩余时间 varnD=Math.floor(nMS/(1000*60*60*24)); //定义参数获得天数 varnH=Math.floor(nMS/(1000*60*60))%24; //定义参数获得小时 varnM=Math.floor(nMS/(1000*60))%60; //定义参数获得分钟 varnS=Math.floor(nMS/1000)%60; //定义参数获得秒钟这些就是当前时间
3、运行环境
IE6IE7IE8及以上Firefox和Google
Chrome游览器下都可实现
4、所有图片的压缩包新建一个文件后将包解压放进文件夹图片的压缩包在页面的最下方可以看到并下载下载后无需修改文件夹名因为本身就已经写好了和html5内的路径相吻合
5、将创建html文件保存的时候将编码类型换成(UTF-8有签名)这样可以让部分中文正常的显示出来,将保存类型(T)换成(所有文件(*.*)),将html5和解压后的图片文件夹放在同一个文件夹内效果
6、代码
<!DOCTYPEhtml>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<scripttype="text/javascript"src="http://code.jquery.com/jquery-1.9.1.js"></script>
<styletype="text/css">
*{margin:0;padding:0;list-style-type:none;}
a,img{border:0;}
body{font:12px/180%;background:#fff;}
.timerbg{background:url(images/bg.png)no-repeat;height:60px;width:980px;margin:20pxauto;}
.timerbgdiv{float:right;font-size:16px;margin:24px000;font-weight:800;text-align:left;width:335px;font-family:"微软雅黑","宋体";}
.timerbgdivstrong{font-size:28px;margin:013px;color:#D6000F;font-family:Arial;}
#daoend{margin:24px000;width:324px;color:#D6000F;font-size:22px;}
</style>
</head>
<body>
<divclass="timerbg">
<divid="daoend"style="display:none;">本次活动已结束。</div>
<divid="dao"><strongid="RemainD"></strong>天<strongid="RemainH"></strong>时<strongid="RemainM"></strong>分<strongid="RemainS"></strong>秒</div>
</div>
<scripttype="text/javascript">
varstartTime=newDate();
//定义参数可返回当天的日期和时间
startTime.setFullYear(2016,5,27);
//调用设置年份
startTime.setHours(23);
//调用设置指定的时间的小时字段
startTime.setMinutes(59);
//调用设置指定时间的分钟字段
startTime.setSeconds(59);
//调用设置指定时间的秒钟字段
startTime.setMilliseconds(999);
//调用置指定时间的毫秒字段
varEndTime=startTime.getTime();
//定义参数可返回距1970年1月1日之间的毫秒数
functionGetRTime(){
//定义方法
varNowTime=newDate();
//定义参数可返回当天的日期和时间
varnMS=EndTime-NowTime.getTime();
//定义参数EndTime减去NowTime参数获得返回距1970年1月1日之间的毫秒数
varnD=Math.floor(nMS/(1000*60*60*24));
//定义参数获得天数
varnH=Math.floor(nMS/(1000*60*60))%24;
//定义参数获得小时
varnM=Math.floor(nMS/(1000*60))%60;
//定义参数获得分钟
varnS=Math.floor(nMS/1000)%60;
//定义参数获得秒钟
if(nMS<0){
//如果秒钟大于0
$("#dao").hide();
//获得天数隐藏
$("#daoend").show();
//获得活动截止时间展开
}else{
//否则
$("#dao").show();
//天数展开
$("#daoend").hide();
//活动截止时间隐藏
$("#RemainD").text(nD);
//显示天数
$("#RemainH").text(nH);
//显示小时
$("#RemainM").text(nM);
//显示分钟
$("#RemainS").text(nS);
//显示秒钟
}
}
$(document).ready(function(){
//定义方法
vartimer_rt=window.setInterval("GetRTime()",1000);
//定义参数显示出GetRTime()方法1000毫秒以后启动
});
</script>
</body>
</html>
7、倒计时完成后,设置按钮的hidden属性为false,简单吧~
本代码是从本人项目中截取出来的,小伙伴们可以放心使用,如有疑问,请留言。