本文主要分享了原生js实现节日时间倒计时功能的示例代码。感兴趣的朋友一起来看吧。
知识要点
1.实现原理:
用结束时间-当前时间=时间差
当前时间每过1秒时间差自然也就少了1秒
所以要1秒更新一次当前时间就达到了倒计时的效果
2.需要注意的就是时间之间的转换和对得出数值的处理
3.用到的方法:
obj.gettime() //换算成毫秒
math.floor() //把小数点向下舍入结果取一个整数
50%24 // 这是对数值求余的方法,意思是 假如有50个小时,一天24个小时 这个得出的结果就是 余数是2
具体的数值的运算处理完整代码里有详细的注释
完整代码
注:代码附带显示当前时间的标准格式以及倒计时天数
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>demo</title>
<style>
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;}
h1,h2,h3,h4,h5,h6{font-size:100%;}
address,cite,dfn,em,var{font-style:normal;}
code,kbd,pre,samp{font-family:courier new,courier,monospace;}
ul,ol{list-style:none;}
a{text-decoration:none;}
a:hover{text-decoration:none;}
sup{vertical-align:text-top;}
sub{vertical-align:text-bottom;}
legend{color:#000;}
fieldset,img{border:0;}
button,input,select,textarea{font-size:100%;}
table{border-collapse:collapse;border-spacing:0;}
.clear{clear: both;float: none;height: 0;overflow: hidden;}
</style>
</head>
<body>
<p id="time"></p>
<br/>
<p id="day"></p>
<br/>
<p id="tm"></p>
<script type="text/javascript">
//在页面加载完后立即执行多个函数完美方案。
function addloadevent(func){
var oldonload=window.onload;
if(typeof window.onload !="function"){
window.onload=func;
}
else{
window.onload=function(){
if(oldonload){
oldonload();
}
func();
}
}
}
addloadevent(showtime);
addloadevent(day);
addloadevent(tb);
//在页面加载完后立即执行多个函数完美方案over。
//天时秒分倒计时
function tb(){
var mydate=new date();//获取当前时间
var endtime=new date("2018,1,1");//获取结束时间
//换算成秒 小数点向下舍入取整
var ltime=math.floor((endtime.gettime()-mydate.gettime())/1000);
//console.log(ltime)
//换算成天 小数点向下舍入取整
var d=math.floor(ltime/(24*60*60));
//换算成小时取去掉天数的余数(也就是小时) 小数点向下舍入取整
var h=math.floor(ltime/(60*60)%24);
//换算成分钟取去掉小时的余数(也就是分钟) 小数点向下舍入取整
var m=math.floor(ltime/60%60);
//换算成分钟取去掉分钟的余数(也就是秒) 小数点向下舍入取整
var s=math.floor(ltime%60);
document.getelementbyid("tm").innerhtml="距2018年元旦还有:"+d+"天"+h+"小时"+m+"分钟"+s+"秒";
if(ltime<=0){
document.getelementbyid("tm").innerhtml="元旦快乐!";
cleartimeout(tb);
}
settimeout(tb,1000);
}
//当秒数为个位数时前面+字符串0
function checktime(i){
return i<10? "0"+i:""+i;
}
//当前时间标准格式
function showtime(){
var mydate=new date();//获取当前时间
var year=mydate.getfullyear();//获取年份
var month=mydate.getmonth()+1;//获取月份是0-11的数字所以+1
var date=mydate.getdate();//日
var day=mydate.getday();//
var h=mydate.gethours();//小时
var m=mydate.getminutes();//分钟
var s=mydate.getseconds();//秒
checktime(m);
checktime(s);
//console.log(day)
var week="日一二三四五六".charat(day);
document.getelementbyid("time").innerhtml=year+"年"+month+"月"+date+"日"+"星期"+week+h+":"+m+":"+s;
settimeout(showtime,1000);
}
//倒计时天数计算
function day(){
var mydate=new date();//获取当前时间
var endtime=new date("2018,1,1");//获取结束时间
//先换算成毫秒再相减就是时间差,再除以一天的毫秒数结果是带有小数点的,用math方法进一位取整
var ltime=math.ceil((endtime.gettime()-mydate.gettime())/(24*60*60*1000));
document.getelementbyid("day").innerhtml="距2018年元旦还有:"+ltime+"天";
}
</script>
</body>
</html>
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
js中事件委托使用详解
源生js做出抽奖页面
p5.js实现黄金螺旋动画
以上就是原生js实现节日时间倒计时功能(附上代码)的详细内容。
