色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

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

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

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

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

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

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 手机在线观看a | 欧美成人小视频 | 国产精品观看在线亚洲人成网 | 国产高清一区二区三区视频 | 欧美黄视频网站 | 亚洲午夜久久久久影院 | 手机在线观看亚洲国产精品 | 国产视频一二三 | 亚洲天堂在线观看视频 | 中文字幕成人免费高清在线 | 国产亚洲欧美成人久久片 | 韩国日本一级毛片免费视频 | 精品久久久久久久久久久 | 日韩高清在线播放不卡 | 一级做a爱片特黄在线观看免费看 | 亚洲国产99 | 日韩专区亚洲国产精品 | 成人影院欧美大片免费看 | 国产精品永久免费视频观看 | 美女va| 2022国产精品网站在线播放 | 欧美日韩免费一区二区在线观看 | 欧美一区二三区 | 一区二区视频在线 | 国产精品国产精品国产三级普 | 国产成人一区二区三区在线播放 | 日日干日日操日日射 | 男女生性毛片免费观看 | 午夜性爽快免费视频播放 | 国内精品一区二区2021在线 | 国产三级网站在线观看 | 亚洲欧美视频在线播放 | 高清国产美女一级a毛片录 高清国产亚洲va精品 | 一区二区在线欧美日韩中文 | 亚洲福利视频精选在线视频 | 欧美精品 日韩 | 青青青免费手机版视频在线观看 | 欧美精品一区二区三区免费播放 | 亚洲 欧美 日韩 丝袜 另类 | 亚洲人妖女同在线播放 | 国产精品热久久毛片 |