javascript實現(xiàn)簡易數(shù)碼時鐘
本文實例為大家分享了javascript實現(xiàn)簡易數(shù)碼時鐘的具體代碼,供大家參考,具體內(nèi)容如下
通過這個小例子復(fù)習(xí)一下Date對象的基本使用. 還可以用Date對象做定時器,計時器等等.
效果如圖:

可以自己去找炫一點的圖片來代替文字,原理都是一樣,只是如果用圖片代替文字,則定時切換圖片即可.
HTML代碼:
<div id='clock'> <p></p> <p></p> <p></p></div>
CSS代碼:
*{margin:0;padding:0;} #clock{width:300px;height:150px;position: relative;margin:50px auto;background: #eeeeee;cursor: default;} #clock p{margin-top:5px;width:300px;height: 40px;text-align: center; font:italic bold 36px/40px arial,sans-serif;letter-spacing: 3px;color:blueviolet;}
JS代碼:
window.onload = function () { var oDiv = document.getElementById(’clock’); var aP = oDiv.getElementsByTagName(’p’); setInterval(clock,1000); function clock() { var oDate = new Date(); //創(chuàng)建日期對象 var date = oDate.getFullYear()+’-’+ convert(oDate.getMonth()+1) +’-’+ convert(oDate.getDate()); var time = convert(oDate.getHours()) +’:’+convert(oDate.getMinutes()) + ’:’ +convert(oDate.getSeconds()); aP[0].innerHTML = date; aP[1].innerHTML = time; aP[2].innerHTML = ’星期’ + convertWeek(oDate.getDay()); } clock(); //加載完頁面后立刻執(zhí)行一次,不用等1秒后才顯示 }; //把一位數(shù)字轉(zhuǎn)換為兩位字符串,補0 function convert(num) { return num < 9?’0’+num:’’+num; } //把week轉(zhuǎn)換為中文 function convertWeek(num) { return num==0?’日’:num==1?’一’:num==2?’二’:num==3?’三’:num==4?’四’:num==5?’五’:’六’; }
更多JavaScript時鐘特效點擊查看:JavaScript時鐘特效專題
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:

網(wǎng)公網(wǎng)安備