JavaScript圓形鐘錶效果
分享一段程式碼例項,它實現了利用js實現了自動讀取本地時間的圓形鐘錶效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style type="text/css"> #outbox { position: relative; border-radius: 50%; width: 400px; height: 400px; margin: 150px auto; background: linear-gradient(#D1E6ED, #767775); } #innerbox { position: absolute; width: 360px; height: 360px; border-radius: 50%; margin: 20px; background-color: white; box-shadow: 0 0 10px 5px gray inset; } #innerbox > div { width: 350px; height: 32px; font-size: 1.5em; line-height: 32px; text-align: center; position: absolute; top: 164px; left: 5px; } #innerbox > div > span:nth-child(1) { float: left; } #innerbox > div > span:nth-child(2) { float: right; } #second_line { height: 175px; width: 4px; background-color: red; top: 25px; left: 198px; position: absolute; z-index: 13; } #minute_line { height: 150px; width: 6px; background-color: blue; top: 50px; left: 197px; position: absolute; z-index: 12; } #hour_line { height: 130px; width: 8px; background-color: black; top: 70px; left: 196px; position: absolute; z-index: 11; } </style> </head> <body> <div id="outbox"> <div id="innerbox"> <div> <span>1</span><span>7</span> </div> <div> <span>2</span><span>8</span> </div> <div> <span>3</span><span>9</span> </div> <div> <span>4</span><span>10</span> </div> <div> <span>5</span><span>11</span> </div> <div> <span>6</span><span>12</span> </div> </div> <div id="second_line"> </div> <div id="minute_line"> </div> <div id="hour_line"> </div> </div> </body> <script> var hours, minutes, seconds; //獲取所有元素 var arrDiv = document.querySelectorAll("#innerbox>div"); var arrSpan = document.querySelectorAll("#innerbox>div>span"); //佈局 for (var i = 0; i < arrDiv.length; i++) { arrDiv[i].style.transform = "rotateZ(-" + (240 - 30 * i) + "deg)"; } for (var i = 0; i < arrSpan.length; i += 2) { arrSpan[i].style.transform = "rotateZ(" + (240 - 15 * i) + "deg)"; arrSpan[i + 1].style.transform = "rotateZ(" + (240 - 15 * i) + "deg)"; } //選擇旋轉點 document.getElementById("second_line").style.transformOrigin = "bottom"; document.getElementById("minute_line").style.transformOrigin = "bottom"; document.getElementById("hour_line").style.transformOrigin = "bottom"; //獲取秒數 seconds = new Date().getSeconds(); //獲取分鐘數,如果seconds=30,相當於分針轉了(30/60)分鐘 minutes = new Date().getMinutes() + seconds / 60; //獲取小時數,同理可得 hours = new Date().getHours() + minutes / 60; //計時器 setInterval(init, 1000); //初始化 function init() { hours = hours + 1 / 3600; minutes = minutes + 1 / 60; seconds++; document.getElementById("second_line").style.transform = "rotate(" + 6 * seconds + "deg)"; document.getElementById("minute_line").style.transform = "rotate(" + 6 * minutes + "deg)"; document.getElementById("hour_line").style.transform = "rotate(" + 30 * hours + "deg)"; } init(); </script> </html>
相關文章
- SVG圓形鐘錶效果SVG
- JavaScript動態圓形鐘錶效果詳解JavaScript
- JavaScript 動態圓形鐘錶JavaScript
- javascript圓形電子鐘錶效果程式碼例項JavaScript
- css3動態圓形鐘錶效果CSSS3
- javascript圓形鐘錶程式碼例項JavaScript
- jquery和css實現的圓形鐘錶效果jQueryCSS
- canvas實現的圓形走動鐘錶效果Canvas
- js和css3實現的圓形鐘錶效果JSCSSS3
- canvas實現的圓形鐘錶效果程式碼例項Canvas
- canvas圓形時鐘效果Canvas
- jquery自動走動的圓形鐘錶效果程式碼例項jQuery
- canvas繪製圓盤走動鐘錶效果Canvas
- css3和js實現的圓形鐘錶效果程式碼例項CSSS3JS
- canvas實現的圓形時鐘效果Canvas
- canvas繪製圓形鐘錶程式碼例項Canvas
- CSS3圓形時鐘效果程式碼CSSS3
- PyQt實現鐘錶效果QT
- JavaScript 秒錶效果JavaScript
- CSS圓形圖片效果CSS
- 圓形放大的hover效果
- canvas圓形隨機漂浮效果Canvas隨機
- js鐘錶效果程式碼例項JS
- 13 - CSS3 - 邊框圓角 - 鐘錶CSSS3
- JavaScript 秒錶效果詳解JavaScript
- css實現圓形、橢圓和半圓效果程式碼例項CSS
- canvas原型鐘錶效果程式碼例項Canvas原型
- CSS3圓形進度條效果CSSS3
- canvas繪製鐘錶效果程式碼例項Canvas
- 蘋果專利曝光 圓形錶盤Apple Watch來襲蘋果APP
- 【譯】使用CSS Transitions實現圓形懸停效果CSS
- canvas繪製圓形框效果不填充內部Canvas
- CSS3標懸浮圓形縮放效果CSSS3
- CSS3圓形旋轉變大動畫效果CSSS3動畫
- javascript圓形區域碰撞檢測程式碼JavaScript
- 新專利暗示Apple Watch或將配備圓形錶盤APP
- Flutter 圓形/圓角頭像Flutter
- css3和javascript實現的時鐘效果CSSS3JavaScript