javascript實現簡易數碼時鐘
阿新 • • 發佈:2020-04-02
本文例項為大家分享了javascript實現簡易數碼時鐘的具體程式碼,供大家參考,具體內容如下
通過這個小例子複習一下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(); //建立日期物件 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(); //載入完頁面後立刻執行一次,不用等1秒後才顯示 }; //把一位數字轉換為兩位字串,補0 function convert(num) { return num < 9?'0'+num:''+num; } //把week轉換為中文 function convertWeek(num) { return num==0?'日':num==1?'一':num==2?'二':num==3?'三':num==4?'四':num==5?'五':'六'; }
更多JavaScript時鐘特效點選檢視:JavaScript時鐘特效專題
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。