JS實(shí)現(xiàn)簡易刻度時(shí)鐘示例代碼
如圖所示,利用JS實(shí)現(xiàn)簡易的刻度時(shí)鐘;

原理如下:利用60等份的li進(jìn)行布局,li兩兩之間的間隔為6deg,把基點(diǎn)定在圓心上,使得li圓形分布。然后另外設(shè)置三條針線的樣式的位置,基點(diǎn)同樣定在圓心上,然后秒針每秒動(dòng)6deg,分針每秒動(dòng)1/60deg,時(shí)針每秒動(dòng)1/3600deg。
布局代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css" id="sty">
*{
margin: 0;
padding: 0;
list-style: none;
}
#wrap{
width: 200px;
height: 200px;
border: 1px solid #000;
border-radius: 50%;
margin: 20px auto;
position: relative;
}
#wrap ul{
position: relative;
}
#wrap ul li{
width: 2px;
height: 6px;
background: #000;
position: absolute;
left: 99px;
top: 0;
-moz-transform-origin: center 100px;
}
#wrap ul li:nth-child(5n){
height: 10px;
}
#con{
width: 10px;
height: 10px;
background: #000;
border-radius: 50%;
position: absolute;
left: 95px;
top: 95px;
}
#hour{
width: 5px;
height: 70px;
background: red;
border-radius: 50%;
position: absolute;
left: 98px;
top: 35px;
-moz-transform-origin: center 65px;
}
#min{
width: 3px;
height: 85px;
background: #000;
border-radius: 50%;
position: absolute;
left: 98.5px;
top: 20px;
-moz-transform-origin: center 80px;
}
#sec{
width: 2px;
height: 100px;
background: gray;
border-radius: 50%;
position: absolute;
left: 98.5px;
top: 20px;
-moz-transform-origin: center 80px;
}
</style>
</head>
<body>
<div id="wrap">
<ul id="list">
</ul>
<div id="hour"></div>
<div id="min"></div>
<div id="sec"></div>
<div id="con"></div>
</div>
</body>
</html>
布局代碼里需要注意的是:每隔四個(gè)刻度就有一個(gè)刻度比較長,所以我們?cè)谠O(shè)置樣式的時(shí)候要特別注意加上:#wrap ul li:nth-child(5n){height: 10px;}。第5n個(gè)的長度變長。
JS代碼中主要搞清楚三針之間的度數(shù)關(guān)系就好做了,代碼如下:
<script type="text/javascript">
window.onload=function(){
var oWrap=document.getElementById('wrap');
var oList=document.getElementById('list');
var oSty=document.getElementById('sty');
var tump='';
for(var i=0;i<60;i++){
var aLi=document.createElement('li');
oList.appendChild(aLi);
tump+='#wrap ul li:nth-child('+(i+1)+'){transform: rotate('+(i+1)*6+'deg);}';
oSty.innerHTML+=tump;
}
var oSec=document.getElementById('sec');
var oMin=document.getElementById('min');
var oHour=document.getElementById('hour');
function time(){
var date=new Date();
var s=date.getSeconds();
var m=date.getMinutes()+(s/60);
var h=date.getHours()+(m/60);
oSec.style.transform='rotate('+s*6+'deg)';
oMin.style.transform='rotate('+m*6+'deg)';
oHour.style.transform='rotate('+h*30+'deg)';
}
time();
setInterval(time,1000);
}
</script>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 利用css+原生js制作簡單的鐘表
- 詳解JavaScript的Date對(duì)象(制作簡易鐘表)
- html5 canvas js(數(shù)字時(shí)鐘)實(shí)例代碼
- js實(shí)現(xiàn)一個(gè)簡單的數(shù)字時(shí)鐘效果
- JS實(shí)現(xiàn)的網(wǎng)頁倒計(jì)時(shí)數(shù)字時(shí)鐘效果
- 超級(jí)可愛純js網(wǎng)頁時(shí)鐘
- js實(shí)現(xiàn)簡單秒表走動(dòng)的時(shí)鐘特效
- js時(shí)鐘翻牌效果實(shí)現(xiàn)代碼分享
- JS 實(shí)現(xiàn)倒計(jì)時(shí)數(shù)字時(shí)鐘效果【附實(shí)例代碼】
- 純js代碼制作的網(wǎng)頁時(shí)鐘特效【附實(shí)例】
- 五步輕松實(shí)現(xiàn)JavaScript HTML時(shí)鐘效果
- 原生JS實(shí)現(xiàn)的簡單小鐘表功能示例
相關(guān)文章
arcgis for js 修改infowindow樣式的方法
下面小編就為大家?guī)硪黄猘rcgis for js 修改infowindow樣式的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
javascript實(shí)現(xiàn)文本框標(biāo)簽驗(yàn)證的實(shí)例代碼
這篇文章主要介紹了javascript實(shí)現(xiàn)文本框標(biāo)簽驗(yàn)證的實(shí)例代碼,需要的朋友可以參考下2018-10-10
swiper移動(dòng)端輪播插件(觸碰圖片之后停止輪播)
這篇文章主要為大家詳細(xì)介紹了swiper移動(dòng)端輪播插件,觸碰圖片之后停止輪播,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
JS重寫Date函數(shù)以及兼容IOS系統(tǒng)
這篇文章主要介紹了JS重寫Date函數(shù)以及兼容IOS系統(tǒng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

