我們先來(lái)看下效果圖(不考慮顏色搭配):
(學(xué)習(xí)視頻分享:html5視頻教程)
我們首先要理解如何去實(shí)現(xiàn)這個(gè)時(shí)鐘,暫時(shí)不要考慮動(dòng)畫(huà),學(xué)著將問(wèn)題進(jìn)行拆解,一步一步實(shí)現(xiàn)。
首先我們需要畫(huà)個(gè)方形,有個(gè)邊框,給一個(gè)圓角就可以實(shí)現(xiàn)最外邊的圓環(huán)再通過(guò)一個(gè)長(zhǎng)的矩形旋轉(zhuǎn)多個(gè)就可以實(shí)現(xiàn)刻度
只要再畫(huà)一個(gè)白色圓面去覆蓋就可以實(shí)現(xiàn)標(biāo)準(zhǔn)的刻度
最后再加上三個(gè)矩形和中間的小圓面就可以實(shí)現(xiàn)時(shí)鐘的初始狀態(tài)了
代碼實(shí)現(xiàn)
以上過(guò)程理解了之后,代碼實(shí)現(xiàn)就簡(jiǎn)單多了,唯一需要考慮的就是代碼的優(yōu)化問(wèn)題,以下為了簡(jiǎn)單明了每一步是如何實(shí)現(xiàn),存在很多重復(fù)的代碼。
關(guān)于動(dòng)畫(huà),我們只需要設(shè)置旋轉(zhuǎn)動(dòng)畫(huà)就可以了,時(shí)分秒針的動(dòng)畫(huà)只需要改變不同的時(shí)間就可以了。
具體細(xì)節(jié)注意見(jiàn)代碼:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>時(shí)鐘</title> <style> *{ padding: 0; margin: 0; } .clock{ width: 300px; height: 300px; border: 10px solid #ccc; /*百分比參照的是實(shí)際寬高*/ border-radius: 50%; margin: 20px auto; position: relative; } .line{ width: 8px; height: 300px; background-color: #ccc; position: absolute; /*實(shí)現(xiàn)居中*/ /*參照父元素的寬*/ left: 50%; top: 0; /*參照元素本身*/ transform: translate(-50%,0); /*保留,否則會(huì)被覆蓋*/ } .line2{ transform: translate(-50%,0) rotate(30deg); } .line3{ transform: translate(-50%,0) rotate(60deg); } .line4{ transform: translate(-50%,0) rotate(90deg); } .line5{ transform: translate(-50%,0) rotate(120deg); } .line6{ transform: translate(-50%,0) rotate(150deg); } .cover{ width: 250px; height: 250px; border-radius: 50%; background-color: #fff; position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); } .hour{ width: 6px; height: 80px; background-color: red; position: absolute; left: 50%; top: 50%; transform: translate(-50%,-100%); /*設(shè)置軸心*/ transform-origin: center bottom; /*動(dòng)畫(huà)*/ -webkit-animation: move 43200s linear infinite; } .minute{ width: 4px; height: 90px; background-color: green; position: absolute; left: 50%; top: 50%; transform: translate(-50%,-100%); /*設(shè)置軸心*/ transform-origin: center bottom; /*動(dòng)畫(huà)*/ -webkit-animation: move 3600s linear infinite; } .second{ width: 2px; height: 100px; background-color: blue; position: absolute; left: 50%; top: 50%; transform: translate(-50%,-100%); /*設(shè)置軸心*/ transform-origin: center bottom; /*動(dòng)畫(huà)*/ -webkit-animation: move 60s infinite steps(60); /*linear與step(60)重復(fù)*/ } .center{ width:20px; height:20px; background-color: #ccc; border-radius: 50%; position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); } /*創(chuàng)建移動(dòng)動(dòng)畫(huà)*/ @keyframes move{ 0%{ transform: translate(-50%,-100%) rotate(0deg); } 100%{ transform: translate(-50%,-100%) rotate(360deg); } } </style> </head> <body> <div> <div class="line line1"></div> <div class="line line2"></div> <div class="line line3"></div> <div class="line line4"></div> <div class="line line5"></div> <div class="line line6"></div> <div></div> <div></div> <div></div> <div></div> <div></div> </div> </body> </html>
相關(guān)推薦:html5教程