国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

用JavaScript實現簡單網頁時鐘

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

利用JavaScript實現網頁時鐘,效果如下圖所示:

用JavaScript實現簡單網頁時鐘

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

<div><img src='http://m.cgvv.com.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代碼部分,使用循環定時器setInterval()每秒調用一次主函數,主函數內使用new Date()創建時間對象,分別使用 .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://m.cgvv.com.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>

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

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 欧美性色网 | 草草影院在线播放 | 国产日韩欧美 | 久久综合狠狠综合狠狠 | 久久99亚洲精品久久 | 成年女人免费观看 | 成人 在线播放 | 色网址在线观看 | 日本高清福利视频在线观看 | 亚洲国产精品久久日 | 自拍国内 | 一区二区三区四区五区六区 | 欧美成一级| 国产亚洲综合在线 | 中文一区| 久久久久久久91精品免费观看 | 在线はじめてのおるすばん | 欧美成人精品一级高清片 | 午夜私人影院免费体验区 | 国产亚洲精 | 亚洲精品国产三级在线观看 | 欧美一级成人免费大片 | 欧美成人二区 | a高清免费毛片久久 | 亚洲欧美国产日韩天堂在线视 | 亚洲精品一区国产二区 | 国产成人亚洲欧美三区综合 | 欧美一级aa毛片禁片 | 亚洲成a人片在线播放 | 日本久久一区二区 | 亚洲精品久久一区二区无卡 | 精品在线免费观看 | 久久亚洲精品中文字幕二区 | 波多野结衣视频在线观看地址免费 | 特级片在线观看 | 免费一级特黄特色黄大任片 | 欧美亚洲国产精品久久 | 中文字幕无线精品乱码一区 | a高清免费毛片久久 | 欧美精品成人一区二区在线观看 | 久久久久亚洲精品中文字幕 |