html+css3實現網頁時鐘

來源:互聯網
上載者:User

標籤:

在網頁上實現時鐘功能,效果如右圖所示:

 

運用到的關鍵技術有:css3中的旋轉、旋轉基點設定

旋轉:transform:rotate(*deg)

旋轉基點:transform-origin: x軸位移 y軸位移

 

樣式代碼:

<style type="text/css" id="style">        #clock{            width:200px;            height: 200px;            border: 1px solid #000000;            margin: 100px auto;            border-radius: 50%;            position: relative;        }        #ul{            margin: 0;            padding: 0;            list-style: none;            position: relative;        }        #ul li{            width:2px;            height: 6px;            background:#000;            position: absolute;            top:0;            left:98px;            -webkit-transform-origin:center 100px;  /*錶盤上刻度的旋轉中心點位於錶盤中央,x軸中間,y軸距離頂部100的位置*/        }                #ul li:nth-of-type(5n+1){height:10px; }        #clock div{            position: absolute;            -webkit-transform-origin:center bottom;        }        #hour{            width:8px;            height:45px;            background: #0c0c0c;            left: 96px;            top:55px;        }         #min{            width:6px;            height:60px;            background:#c0c0c0;            left: 97px;            top:40px;        }         #sec{            width:4px;            height:80px;            background: red;            left: 98px;            top:20px;        }        #ico{            width:20px;            height: 20px;            background: #000000;            border-radius: 50%;            left: 90px;            top: 90px;        }        #time{            text-align: center;        }    </style>
View Code


javascript代碼:

<script type="text/javascript">        window.onload = function(){            var oUl = document.getElementById(‘ul‘);            var oHour = document.getElementById(‘hour‘);            var oMin = document.getElementById(‘min‘);            var oSec = document.getElementById(‘sec‘);            var oStyle = document.getElementById(‘style‘);            var oTime = document.getElementById(‘time‘);            var iLi = ‘‘;            var iStyle =‘‘;            for(var i=0;i<60;i++)            {                iStyle+=‘#ul li:nth-of-type(‘+(i+1)+‘){-webkit-transform:rotate(‘+(i*6)+‘deg)}‘;                iLi+=‘<li></li>‘;            }            oUl.innerHTML = iLi ;            oStyle.innerHTML+=iStyle;             function toMove(){                var iHour = ‘‘;                var iMin = ‘‘;                var iSec = ‘‘;                var oDate = new Date();                iSec = oDate.getSeconds();                iMin = oDate.getMinutes()+iSec/60;                iHour = oDate.getHours()+iMin/60;                oSec.style.webkitTransform=‘rotate(‘+(iSec*6)+‘deg)‘;                oMin.style.webkitTransform=‘rotate(‘+(iMin*6)+‘deg)‘;/**/                oHour.style.webkitTransform=‘rotate(‘+(iHour*30)+‘deg)‘;/*錶盤12小時,共360度,每小時走30度*/                oTime.innerHTML=‘當前的時間是:‘+ Math.floor(iHour)+‘時‘+Math.floor(iMin)+‘分‘+iSec+‘秒‘;            }            setInterval(toMove,1000);        };    </script>
View Code


網頁布局代碼:

 <div id="clock">        <ul id="ul">            <li></li>            <li></li>            <li></li>            <li></li>            <li></li>            <li></li>            <li></li>        </ul>        <div id="hour"></div>        <div id="min"></div>        <div id="sec"></div>        <div id="ico"></div>    </div>    <div id="time"></div>
View Code

 

html+css3實現網頁時鐘

聯繫我們

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