簡單的JS鐘錶計時

來源:互聯網
上載者:User

標籤:alt   cond   ora   padding   style   效果   使用   onload   count()   

思路:先寫出簡單的數字計時,根據時分秒的數值轉換成度數,使用CSS3的transform進行div傾斜。

知識點:transform可以對div進行傾斜或旋轉等效果。但是根據瀏覽器不同代碼也不同,本代碼只能根據chorme瀏覽器或safari使用

div{transform:rotate(7deg);-ms-transform:rotate(7deg); /* IE 9 */-moz-transform:rotate(7deg); /* Firefox */-webkit-transform:rotate(7deg); /* Safari 和 Chrome */-o-transform:rotate(7deg); /* Opera */}

代碼部分:

<!doctype html>
<html>
<head>
<meta charset="GBK">
<title>H5+jquery實現網頁時鐘</title>
<script src="jquery.min.js"></script>
<style>
.body{
margin:0;
padding:0;
border:0;
}
div{
border:0;
}
.biaopan{
width:400px;
height:400px;
background-color:#E0FFFF;
border:1px solid black;
-webkit-border-radius:200px;
}
.h{
position:absolute;
left:200px;
top:70px;
height:260px;
width:10px;
}
.m{
position:absolute;
left:200px;
top:50px;
height:300px;
width:6px;
}
.s{
position:absolute;
left:200px;
top:10px;
height:380px;
width:4px;
}
</style>
</head>
<body onload="timecount()">
<div class="biaopan"></div>
<div class="h" id="h">
<div style="background-color:red;height:130px;
width:10px;"></div>
</div>
<div class="m" id="m">
<div style="background-color:green;height:150px;
width:6px;"></div>
</div>
<div class="s" id="s">
<div style="background-color:blue;height:190px;
width:4px;"></div>
</div>
<div style="margin-top:30px;height:60px;font-weight:bold;color:orange;font-size:30px;" id="timecount"></div>
<div>
<ul>
<li style="color:red;font-weight:bold;font-size:30px;">紅色時針</li><li style="font-weight:bold;font-size:30px;color:green;">綠色分針</li><li style="font-weight:bold;font-size:30px;color:blue;">藍色秒針</li>
</ul>
</div>
</body>
</html>
<script>
function timecount(){
var hours = new Date().getHours();
var minutes = new Date().getMinutes();
if(minutes<10){
minutes = "0" + minutes;
}
var second = new Date().getSeconds();
if(second<10){
second = "0" + second;
}
document.getElementById("timecount").innerHTML = "目前時間為:"+hours+":"+minutes+":"+second;
//設定三個指標的傾斜度數
var h = Number(hours)*30;
var m = Number(minutes)*6;
var s = Number(second)*6;
$("#h").css("-webkit-transform","rotate("+h+"deg)");
$("#m").css("-webkit-transform","rotate("+m+"deg)");
$("#s").css("-webkit-transform","rotate("+s+"deg)");
setTimeout("timecount()",1000);
}


</script>

網頁效果:

 

簡單的JS鐘錶計時

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.