JS實現簡易刻度時鐘範例程式碼,js刻度範例程式碼

來源:互聯網
上載者:User

JS實現簡易刻度時鐘範例程式碼,js刻度範例程式碼

,利用JS實現簡易的刻度時鐘;

原理如下:利用60等份的li進行布局,li兩兩之間的間隔為6deg,把基點定在圓心上,使得li圓形分布。然後另外設定三條針線的樣式的位置,基點同樣定在圓心上,然後秒針每秒動6deg,分針每秒動1/60deg,時針每秒動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>

布局代碼裡需要注意的是:每隔四個刻度就有一個刻度比較長,所以我們在設定樣式的時候要特別注意加上:#wrap ul li:nth-child(5n){height: 10px;}。第5n個的長度變長。

JS代碼中主要搞清楚三針之間的度數關係就好做了,代碼如下:

<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>

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.