亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

用JavaScript實現(xiàn)簡單網(wǎng)頁時鐘

瀏覽:76日期:2023-12-02 14:00:40

利用JavaScript實現(xiàn)網(wǎng)頁時鐘,效果如下圖所示:

用JavaScript實現(xiàn)簡單網(wǎng)頁時鐘

首先在body中完成表盤、指針的資源載入:

<div><img src='http://www.aoyou183.cn/image/clockface.jpg' alt=''></div><hr ><hr id='min'><hr id='second'>

設置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)定時器setInterval()每秒調用一次主函數(shù),主函數(shù)內使用new Date()創(chuàng)建時間對象,分別使用 .getHours();.getMinutes();.getSeconds()獲得當前的時分秒,然后利用CSS自帶動畫-旋轉改變指針的角度:

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)'; }

目前存在的問題是,時分秒指針由于使用的是hr標簽表示,所以存在兩端一樣長的問題。

完整代碼如下:

<!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='http://www.aoyou183.cn/image/clockface.jpg' alt=''></div><hr ><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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 美女国内精品自产拍在线播放 | 性 色 黄 一级 | 国产亚洲女在线精品 | 欧美日韩无线码免费播放 | 亚洲综合综合在线 | 国产一级毛片视频 | 国产免费私拍一区二区三区 | 欧美一级毛片欧美大尺度一级毛片 | 国产亚洲视频在线 | 亚洲国产精品免费 | 国产精品女人在线观看 | 日本色图在线 | 4444亚洲国产成人精品 | 黄色三级视频在线观看 | 国产一区成人 | 亚洲一区二区三区久久久久 | 国产精品久久久久久 | 国产亚洲高清不卡在线观看 | 精品美女视频在线观看2023 | 小馒头刚发育在线播放free | 亚洲精品国产男人的天堂 | 中文字幕永久在线视频 | 国产人成精品香港三级古代 | 成人精品一区二区久久久 | 成人一二| www.啪| 欧美一级毛片不卡免费观看 | 婷婷影院在线综合免费视频 | 免费一级欧美在线观看视频片 | 黄色毛片在线播放 | 亚洲精品午夜一区二区 | 亚色综合 | 俄罗斯一级毛片免费视频 | 国产一级视频免费 | 日韩在线精品视频 | 午夜视频成人 | 亚洲精品久久久久福利网站 | 中文国产成人精品久久无广告 | 亚洲国产一区在线精选 | 最新大黄网站免费 | 日韩一级黄|