简单通过js实现倒计时功能。
代码贴上:
function changeTime(surplusTime) { //倒计时-时分秒格式 var hours = parseInt(surplusTime / (1000 * 60 * 60)); var minutes = parseInt((surplusTime % (1000 * 60 * 60)) / (1000 * 60)); var seconds = (surplusTime % (1000 * 60)) / 1000; var text = '<span>'+hours+'</span>时<span>'+minutes+'</span>分<span>'+seconds+'</span>秒'; if (surplusTime != 0) { surplusTime -= 1000; setTimeout(function(){ changeTime(surplusTime) }, 1000); } return text; } function changeDateTime(surplusTime){ //倒计时-天数+时分秒格式 var day = parseInt((surplusTime / (1000 * 60 * 60 * 24))); var hours = parseInt((surplusTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = parseInt((surplusTime % (1000 * 60 * 60)) / (1000 * 60)); var seconds = (surplusTime % (1000 * 60)) / 1000; var text = '<span>'+day+'</span>天<span>'+hours+'</span>时<span>'+minutes+'</span>分<span>'+seconds+'</span>秒'; if (surplusTime != 0) { surplusTime -= 1000; setTimeout(function(){ changeTime(surplusTime) }, 1000); } return text; }
样式及显示方式可根据自身喜好调整
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com