本文實例講述了JS+CSS3實現的簡易鐘表效果。分享給大家供大家參考,具體如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>www.jb51.net js+css3簡易鐘表</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#wrap{
width: 300px;
height: 300px;
background-color: aliceblue;
margin: 200px auto;
position: relative;
box-sizing: border-box;
}
#wrap > div{
position: absolute;
top: 50%;
left: 50%;
}
#hour{
width: 6px;
height: 60px;
background-color: #000000;
margin: -60px -3px;
transform-origin: 3px 60px;
}
#min{
width: 4px;
height: 80px;
background-color: #000000;
margin: -80px -2px;
transform-origin: 2px 80px;
}
#sec{
width: 2px;
height: 100px;
background-color: red;
margin: -100px -1px;
transform-origin: 1px 100px;
}
#point{
width: 20px;
height: 20px;
background-color: burlywood;
margin: -10px -10px;
border-radius: 50%;
}
#circle{
width: 300px;
height: 300px;
position: relative;
}
#circle li{
list-style: none;
width: 2px;
height: 6px;
background-color: #000000;
position: absolute;
transform-origin: 1px 150px;
left: 149px;
top: 0px;
}
</style>
</head>
<body>
<div id="wrap">
<div id="hour"></div>
<div id="min"></div>
<div id="sec"></div>
<div id="point"></div>
<ul id="circle"></ul>
</div>
</body>
<script type="text/javascript">
var hourDom=document.getElementById('hour');
var minDom=document.getElementById('min');
var secDom=document.getElementById('sec');
var cricle=document.getElementById('circle');
//創建表盤,ul寬高為wrap寬高,以wrap中心點為變換基點,動態分配6°的li
for (var i=0;i<60;i++) {
var li=document.createElement('li');
cricle.appendChild(li);
li.style.transform='rotate('+i*6+'deg)';
}
//延時函數,確保每一秒更新一次最新時間。并且計算時間準確值。
setInterval(function(){
var date=new Date();
var hour=date.getHours();
var min=date.getMinutes();
var sec=date.getSeconds();
min+=sec/60;
hour+=min/60;
//當前時間*每個單位時間走的角度=指針指向
hourDom.style.transform='rotate('+hour*30+'deg)';
minDom.style.transform='rotate('+min*6+'deg)';
secDom.style.transform='rotate('+sec*6+'deg)';
},1000)
</script>
</html>
運行效果:

感興趣的朋友可使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試一下運行效果。
PS:這里再為大家推薦幾款時間及日期相關工具供大家參考使用:
在線日期/天數計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線日期計算器/相差天數計算器:
http://tools.jb51.net/jisuanqi/datecalc
在線日期天數差計算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
Unix時間戳(timestamp)轉換工具:
http://tools.jb51.net/code/unixtime
更多關于JavaScript相關內容感興趣的讀者可查看本站專題:《JavaScript時間與日期操作技巧總結》、《JavaScript查找算法技巧總結》、《JavaScript錯誤與調試技巧總結》、《JavaScript數據結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數學運算用法總結》
希望本文所述對大家JavaScript程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。