javascript實(shí)現(xiàn)簡易數(shù)碼時(shí)鐘
本文實(shí)例為大家分享了javascript實(shí)現(xiàn)簡易數(shù)碼時(shí)鐘的具體代碼,供大家參考,具體內(nèi)容如下
通過這個(gè)小例子復(fù)習(xí)一下Date對象的基本使用. 還可以用Date對象做定時(shí)器,計(jì)時(shí)器等等.
效果如圖:
可以自己去找炫一點(diǎn)的圖片來代替文字,原理都是一樣,只是如果用圖片代替文字,則定時(shí)切換圖片即可.
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)換為兩位字符串,補(bǔ)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時(shí)鐘特效點(diǎn)擊查看:JavaScript時(shí)鐘特效專題
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Spring security 自定義過濾器實(shí)現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實(shí)例代碼)2. docker /var/lib/docker/aufs/mnt 目錄清理方法3. JAMon(Java Application Monitor)備忘記4. Python OpenCV去除字母后面的雜線操作5. 在Mac中配置Python虛擬環(huán)境過程解析6. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法7. IntelliJ IDEA設(shè)置背景圖片的方法步驟8. Python TestSuite生成測試報(bào)告過程解析9. Python 的 __str__ 和 __repr__ 方法對比10. Java類加載機(jī)制實(shí)現(xiàn)步驟解析
