网页倒计时制作
css:
body{
user-select:none;
}
#hh,#hh1{
font-size: 50px;
text-align: center;
}
内容:
<h1 id="hh"></h1>
<h1 id="hh1"></h1>
JS:
function station(x,y,z,bool){
var now = Date.now(),
end = new Date(x,y-1,z),
ends = end.getTime();
if(bool)
var ss = ends - now;
else
var ss = now - ends;
var s = Math.floor(ss/1000);
var day = Math.floor(s / 60 / 60 / 24);
var hours = Math.floor(s / 60 / 60 % 24);
var min = Math.floor(s / 60 % 60 );
var sec = Math.floor(s % 60 );
if(bool){
var html = "距离元旦还有"+day+"天"+hours+"时"+min+"分"+sec+"秒";
hh.innerHTML = html;
}
else{
var html = "距离暑假回校已有"+day+"天"+hours+"时"+min+"分"+sec+"秒";
hh1.innerHTML = html;
}
}
station(2019,01,01,true);
setInterval(function(){
station(2019,01,01,true);
},1000);
station(2018,9,10,false);
setInterval(function(){
station(2018,9,10,false);
},1000);
结果: