基于Javascript倒計時效果
更新時間:2016年12月22日 11:46:47 作者:逆世風(fēng)靈
這篇文章主要為大家詳細(xì)介紹了基于Javascript倒計時效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js倒計時效果的具體代碼,供大家參考,具體內(nèi)容如下
Index.html
<!DOCTYPE html > <html> <head> <title>倒計時</title> <style type="text/css"> .colockbox { width:500px; height:20px; color:#000000; } .colockbox span { float:left;display:block; width:20px; height:20px; line-height:20px; font-size:18px; font-weight:bold; text-align:center; color:#ffffff; text-indent:3px; } .square { float:left; width:26px; height:20px; background-color:#222222; border-radius:3px; padding:0px; margin-right:5px; } </style> <script type="text/javascript" src="jquery-1.11.3.js"></script> <script type="text/javascript"> $(function(){ countDown("2016/12/25 23:00:00","#colockbox1"); }); function countDown(time,id){ var day_elem = $(id).find('.day'); var hour_elem = $(id).find('.hour'); var minute_elem = $(id).find('.minute'); var second_elem = $(id).find('.second'); var end_time = new Date(time).getTime(),//月份是實際月份-1 sys_second = (end_time-new Date().getTime())/1000; var timer = setInterval(function(){ if (sys_second > 1) { sys_second -= 1; var day = Math.floor((sys_second / 3600) / 24); var hour = Math.floor((sys_second / 3600) % 24); var minute = Math.floor((sys_second / 60) % 60); var second = Math.floor(sys_second % 60); day_elem && $(day_elem).text(day);//計算天 $(hour_elem).text(hour<10?"0"+hour:hour);//計算小時 $(minute_elem).text(minute<10?"0"+minute:minute);//計算分鐘 $(second_elem).text(second<10?"0"+second:second);//計算秒殺 } else { clearInterval(timer); } }, 1000); } </script> </head> <body> <div class="colockbox" id="colockbox1"> <div class="square"> <span class="day">00</span> </div> <span style="color:gray;font-size:15px; float:left;">天</span> <div class="square"> <span class="hour">00</span> </div> <span style="color:gray;font-size:15px; float:left;">時</span> <div class="square"> <span class="minute">00</span> </div> <span style="color:gray;font-size:15px; float:left;">分</span> <div class="square"> <span class="second">00</span> </div> <span style="color:gray;font-size:15px; float:left;">秒</span> </div> </body> </html>
運行結(jié)果如圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript幾種常見循環(huán)遍歷使用和區(qū)別
這篇文章主要介紹了JavaScript幾種常見循環(huán)遍歷使用和區(qū)別,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09uniapp存儲數(shù)據(jù)到本地文件及讀取本地文件數(shù)據(jù)代碼示例
這篇文章主要給大家介紹了關(guān)于uniapp存儲數(shù)據(jù)到本地文件及讀取本地文件數(shù)據(jù)的相關(guān)資料,在開發(fā)uniapp應(yīng)用時,我們常常需要使用本地存儲來保存一些數(shù)據(jù),比如用戶登錄信息、設(shè)置項等,本文通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09