用JavaScript實(shí)現(xiàn)簡(jiǎn)單網(wǎng)頁(yè)時(shí)鐘
利用JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)時(shí)鐘,效果如下圖所示:
首先在body中完成表盤、指針的資源載入:
<div><img src="../../image/clockface.jpg" alt=""></div> <hr id="hour" > <hr id="min"> <hr id="second">
設(shè)置CSS樣式:
<style> body{ margin: 0; } div{ margin: 0 auto; width: 600px; height: 600px; } #hour{ background-color: black; width: 130px; height: 10px; position: fixed; top: 295px; left: 50%; margin-left: -65px; } #min{ background-color: red; width: 200px; height: 8px; position: fixed; top: 296px; left: 50%; margin-left: -100px; } #second{ background-color: yellow; width: 270px; height: 5px; position: fixed; top: 297.5px; left: 50%; margin-left: -135px; } </style>
最后是JS代碼部分,使用循環(huán)定時(shí)器setInterval()每秒調(diào)用一次主函數(shù),主函數(shù)內(nèi)使用new Date()創(chuàng)建時(shí)間對(duì)象,分別使用 .getHours();.getMinutes();.getSeconds()獲得當(dāng)前的時(shí)分秒,然后利用CSS自帶動(dòng)畫(huà)-旋轉(zhuǎn)改變指針的角度:
setInterval(watch,1000); var anjleSeconds=0,anjleMin=0,anjleHours=0; function watch() { var Time= new Date(); anjleSeconds=Time.getSeconds()/60*360+90; anjleMin=Time.getMinutes()/60*360+90; anjleHours=nowHours/12*360+90; document.getElementById("second").style.transform="rotate("+anjleSeconds+"deg)"; document.getElementById("min").style.transform="rotate("+anjleMin+"deg)"; document.getElementById("hour").style.transform="rotate("+anjleHours+"deg)"; }
目前存在的問(wèn)題是,時(shí)分秒指針由于使用的是hr標(biāo)簽表示,所以存在兩端一樣長(zhǎng)的問(wèn)題。
完整代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> body{ margin: 0; } div{ margin: 0 auto; width: 600px; height: 600px; } #hour{ background-color: black; width: 130px; height: 10px; position: fixed; top: 295px; left: 50%; margin-left: -65px; } #min{ background-color: red; width: 200px; height: 8px; position: fixed; top: 296px; left: 50%; margin-left: -100px; } #second{ background-color: yellow; width: 270px; height: 5px; position: fixed; top: 297.5px; left: 50%; margin-left: -135px; } </style> </head> <body> <div><img src="../../image/clockface.jpg" alt=""></div> <hr id="hour" > <hr id="min"> <hr id="second"> <script> setInterval(watch,1000); var anjleSeconds=0,anjleMin=0,anjleHours=0; function watch() { var Time= new Date(); anjleSeconds=Time.getSeconds()/60*360+90; anjleMin=Time.getMinutes()/60*360+90; anjleHours=Time.getHours()/12*360+90; document.getElementById("second").style.transform="rotate("+anjleSeconds+"deg)"; document.getElementById("min").style.transform="rotate("+anjleMin+"deg)"; document.getElementById("hour").style.transform="rotate("+anjleHours+"deg)"; } </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript數(shù)組push方法使用注意事項(xiàng)
push() 方法可向數(shù)組的末尾添加一個(gè)或多個(gè)元素,并返回新的長(zhǎng)度。這篇文章主要介紹了JavaScript數(shù)組push方法使用注意,需要的朋友可以參考下2017-10-10JavaScript必知必會(huì)(三) String .的方法來(lái)自何方
這篇文章主要介紹了JavaScript必知必會(huì)(三) String .的方法來(lái)自何方的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06javascript 一個(gè)自定義長(zhǎng)度的文本自動(dòng)換行的函數(shù)
javascript 一個(gè)自定義長(zhǎng)度的文本自動(dòng)換行的函數(shù)...2007-08-08javascript中巧用“閉包”實(shí)現(xiàn)程序的暫停執(zhí)行功能
javascript中巧用“閉包”實(shí)現(xiàn)程序的暫停執(zhí)行功能...2007-04-04JavaScript中click和onclick本質(zhì)區(qū)別與用法分析
這篇文章主要介紹了JavaScript中click和onclick本質(zhì)區(qū)別與用法,結(jié)合實(shí)例形式分析了JavaScript中click和onclick的具體概念、功能、使用場(chǎng)景及相關(guān)操作技巧,需要的朋友可以參考下2018-06-06JavaScript?ES6中class定義類實(shí)例方法
ES6提供了更接近面向?qū)ο?注意:javascript本質(zhì)上是基于對(duì)象的語(yǔ)言)語(yǔ)言的寫(xiě)法,引入了Class(類)這個(gè)概念,作為對(duì)象的模板,下面這篇文章主要給大家介紹了關(guān)于JavaScript?ES6中class定義類的相關(guān)資料,需要的朋友可以參考下2022-07-07window.parent調(diào)用父框架時(shí) ie跟火狐不兼容問(wèn)題
window.parent調(diào)用父框架時(shí),ie跟火狐不兼容問(wèn)題!2009-07-07JavaScript Generator函數(shù)使用分析
生成器Generator是JavaScript ES6引入的特性,它讓我們可以分段執(zhí)行一個(gè)函數(shù)。但是在談?wù)撋善鳎℅enerator)之前,我們要先了解迭代器Iterator2022-10-10