css3和js實現的圓形鐘錶效果程式碼例項

admin發表於2017-04-15

分享一段程式碼例項,它利用css和js實現了圓形鐘錶效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style id="css">
#box{
  width:200px;
  height:200px;
  border:4px solid #000; 
  margin:30px auto; 
  border-radius:50%; 
  position:relative;
}
#list{ 
  margin:0;
  padding:0; 
  list-style:none;
  width:200px;
  height:200px; 
  position:relative;
}
#list li{ 
  width:2px;
  height:6px;
  background:#000; 
  position:absolute; 
  top:0; 
  left:99px; 
  transform-origin:center 100px;
}
#list li:nth-of-type(5n+1){ 
  height:15px;
}
#hours{
  width:8px;
  height:45px;
  background:#000; 
  position:absolute;
  left:96px;
  top:55px;
  transform-origin:bottom;
}
#min{
  width:4px;
  height:60px;
  background:#066; 
  position:absolute;
  left:98px;
  top:40px;
  transform-origin:bottom;
}
#sec{
  width:2px;
  height:80px;
  background:red; 
  position:absolute;
  left:99px;
  top:20px;
  transform-origin:bottom;
}
.ico{
  width:20px;
  height:20px;
  background:#000; 
  border-radius:50%; 
  position:absolute;
  left:90px;
  top:90px;
}
</style>
<script>
window.onload = function() {
  var oList = document.getElementById("list");
  var oCss = document.getElementById("css");
  var sHtml = "",
    sCss = "";
  var oHours = document.getElementById("hours");
  var oMin = document.getElementById("min");
  var oSec = document.getElementById("sec");
  for (var i = 0; i < 60; i++) {
    sHtml += "<li></li>";
    sCss += "#list li:nth-of-type(" + (i + 1) + "){ transform:rotate(" + (i * 6) + "deg);}";
  }
  oList.innerHTML = sHtml;
  oCss.innerHTML += sCss;
  toDate();
  setInterval(toDate, 1000);
 
  function toDate() {
    var oDate = new Date();
    var iSec = oDate.getSeconds();
    var iMin = oDate.getMinutes() + iSec / 60;
    var iHours = oDate.getHours() + iMin / 60;
    oSec.style.transform = "rotate(" + iSec * 6 + "deg)";
    oMin.style.transform = "rotate(" + iMin * 6 + "deg)";
    oHours.style.transform = "rotate(" + iHours * 30 + "deg)";
  }
};
</script>
</head>
<body>
<div id="box">
  <ul id="list"></ul>
  <div id="hours"></div>
  <div id="min"></div>
  <div id="sec"></div>
  <div class="ico"></div>
</div>
</body>
</html>

相關文章