用JavaScript實(shí)現(xiàn)簡單網(wǎng)頁時(shí)鐘
利用JavaScript實(shí)現(xiàn)網(wǎng)頁時(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)畫-旋轉(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)";
}
目前存在的問題是,時(shí)分秒指針由于使用的是hr標(biāo)簽表示,所以存在兩端一樣長的問題。
完整代碼如下:
<!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>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS實(shí)現(xiàn)瀏覽器狀態(tài)欄文字閃爍效果的方法
這篇文章主要介紹了JS實(shí)現(xiàn)瀏覽器狀態(tài)欄文字閃爍效果的方法,通過時(shí)間函數(shù)定時(shí)觸發(fā)遞歸調(diào)用實(shí)現(xiàn)狀態(tài)欄文字閃爍效果,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
如何使用uniapp開發(fā)微信小程序獲取當(dāng)前位置詳解
uni-app小程序項(xiàng)目無法直接獲取到地理位置,只能通過獲取到的經(jīng)緯度,調(diào)用第三方地圖Api獲取,下面這篇文章主要給大家介紹了關(guān)于如何使用uniapp開發(fā)微信小程序獲取當(dāng)前位置的相關(guān)資料,需要的朋友可以參考下2022-10-10
JS響應(yīng)鼠標(biāo)點(diǎn)擊實(shí)現(xiàn)兩個(gè)滑塊區(qū)間拖動(dòng)效果
這篇文章主要介紹了JS實(shí)現(xiàn)的兩個(gè)滑塊區(qū)間拖動(dòng)效果代碼,涉及JavaScript響應(yīng)鼠標(biāo)事件針對(duì)頁面元素的動(dòng)態(tài)操作技巧,需要的朋友可以參考下2015-10-10
獲取JAVASCRIPT時(shí)間戳函數(shù)的5種方法
JavaScript獲得時(shí)間戳的方法有五種,后四種都是通過實(shí)例化時(shí)間對(duì)象new?Date()?來進(jìn)一步獲取當(dāng)前的時(shí)間戳,JavaScript處理時(shí)間主要使用時(shí)間對(duì)象Date,本文對(duì)js時(shí)間戳函數(shù)獲取方法給大家介紹的非常詳細(xì),需要的朋友參考下吧2024-01-01

