css3 利用transform打造走動(dòng)的2D時(shí)鐘

學(xué)完了transform
這個(gè)課程,還是來(lái)一個(gè)案例吧,利用transform
的旋轉(zhuǎn)rotate
打造一個(gè)時(shí)鐘,再結(jié)合JavaScript的定時(shí)器讓它走起來(lái)。
截一個(gè)動(dòng)圖:
案例知識(shí)點(diǎn)分析:
1、利用定位完成時(shí)鐘的繪制。
2、背景使用了放射性漸變。
3、利用JavaScript完成刻度和時(shí)間數(shù)字的旋轉(zhuǎn)。
4、利用Date()對(duì)象獲取系統(tǒng)時(shí)間,并讓時(shí)針指向?qū)?yīng)的刻度。
5、利用定時(shí)器不斷更新時(shí)間,完成時(shí)針的運(yùn)動(dòng)。
一、HTML源代碼
<div id="clock-wrap"> <div id="clock"> <ul id="list"> </ul> </div> <div id="num"> <ul> <li>12</li> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> <li>11</li> </ul> </div> <div id="hour"></div> <div id="min"></div> <div id="sec"></div> <div id="circle"></div> </div>
二、CSS樣式
<style id="css">/*注意這里為style標(biāo)簽添加了一個(gè)id,在JavaScript里面有獲取,并往里面添加css樣式。*/ body,ul{ margin:0; padding:0;} body{ font:1em "microsoft Yahei"; color:#666; background-color:#333;} h1{ text-align:center; color:#eee; font-size:3rem;} li{ list-style:none;} p{ text-align:center; color:#ddd; position:relative; top:100px; } a{ color:#999; text-decoration:none; transition:0.2s;} a:hover{ color:#ddd;} #clock-wrap{ width:400px; height:400px; border:10px solid #fff; border-radius:50%; margin:80px auto 0; position:relative; box-shadow:0 0 40px rgba(0,0,0,1)} #clock ul{ width:400px; height:400px; position:relative; border-radius:50%; background:radial-gradient(circle at center,#667eea,#764ba2); box-shadow:0 0 50px rgba(0,0,0,0.5) inset; /*設(shè)置內(nèi)陰影*/ } #clock ul li{ position:absolute; left:50%; margin-left:-2px; top:0; width:4px; height:10px; background:rgba(255,255,255,.5); transform-origin:center 200px; /*li的旋轉(zhuǎn)中心點(diǎn)在圓形中間。*/ } #clock li:nth-child(5n+1){ /*5個(gè)刻度為一組,每一組的第一個(gè)刻度要長(zhǎng)一點(diǎn)。*/ height:18px; } #num{ position:absolute; width:360px; height:360px; left:0; right:0; top:0; bottom:0; margin:auto; } #num li{ position:absolute; left:50%; margin-left:-10px; top:0; color:rgba(255,255,255,.5); font:2em Arial, Helvetica, sans-serif; transform-origin:center 180px;} #hour,#min,#sec{ background:#fff; position:absolute; left:50%; top:50%; transform-origin:bottom; /*時(shí)針的旋轉(zhuǎn)點(diǎn)在自己的底部。*/ box-shadow:0 0 6px rgba(0,0,0,.5) } #hour{ width:14px; height:100px; margin-left:-7px; margin-top:-100px; border-radius:3px; } #min{ width:10px; height:150px; margin-left:-5px; margin-top:-150px; border-radius:2px; } #sec{ width:4px; height:180px; margin-left:-2px; margin-top:-180px; border-radius:1px; } #circle{ width:40px; height:40px; border-radius:50%; background:#fff; position:absolute; left:50%; margin-left:-20px; top:50%; margin-top:-20px; box-shadow:0 0 20px rgba(0,0,0,.4)} </style>
三、JavaScript代碼
<script> window.onload=function(){ var oList=document.getElementById("list"); var oCSS=document.getElementById("css"); //style標(biāo)簽也可以加id屬性。 var aNums=document.getElementById("num").getElementsByTagName("li"); var oHour=document.getElementById("hour"); var oMin=document.getElementById("min"); var oSec=document.getElementById("sec"); var aLi=""; var sCSS=""; for(var i=0;i<60;i++){ //循環(huán)60次,產(chǎn)生刻度值和每一個(gè)刻度旋轉(zhuǎn)的度數(shù)。 aLi+="<li></li>"; sCSS+="#clock li:nth-child("+(i+1)+"){transform:rotate("+i*6+"deg);}" } for(var i=0;i<12;i++){ sCSS+="#num li:nth-child("+(i+1)+"){transform:rotate("+i*30+"deg);}" } oList.innerHTML=aLi; oCSS.innerHTML+=sCSS; //css需要追加到原來(lái)的css文檔中。 myTime(); //初始化函數(shù)。 setInterval(myTime,1000); //設(shè)置定時(shí)器,每隔1秒執(zhí)行一次函數(shù)。 function myTime(){ var oDate=new Date(); var iSec=oDate.getSeconds(); //精確到秒數(shù)的分鐘數(shù)。 var iMin=oDate.getMinutes()+iSec/60; //精確到分秒的小時(shí)數(shù)??梢栽谛D(zhuǎn)的時(shí)候更精確。 var iHour=oDate.getHours()+iMin/60; oSec.style.transform="rotate("+iSec*6+"deg)" ; oMin.style.transform="rotate("+iMin*6+"deg)"; oHour.style.transform="rotate("+iHour*30+"deg)"; //時(shí)針的指向需要把分鐘和秒數(shù)算進(jìn)去才精確。 } } </script>
以上就是css3 利用transform打造走動(dòng)的2D時(shí)鐘的詳細(xì)內(nèi)容,更多關(guān)于CSS3 transform的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
CSS3的常見(jiàn)transformation圖形變化用法小結(jié)
這篇文章主要介紹了CSS3的常見(jiàn)transformation圖形變化用法小結(jié),共整理了旋轉(zhuǎn)、縮放、平移、傾斜以及矩陣的使用方法,需要的朋友可以參考下2016-05-13css3中transform屬性實(shí)現(xiàn)的4種功能
在CSS3中,可以利用transform功能實(shí)現(xiàn)文字或圖像的旋轉(zhuǎn)、縮放、傾斜、移動(dòng)這4中類型的變形處理。本文就詳細(xì)的介紹了這4種實(shí)現(xiàn),感興趣的可以了解一下2021-08-05css3 利用transform-origin 實(shí)現(xiàn)圓點(diǎn)分布在大圓上布局及旋轉(zhuǎn)特效
這篇文章主要介紹了css3 利用transform-origin 實(shí)現(xiàn)圓點(diǎn)分布在大圓上布局及旋轉(zhuǎn)特效,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可2021-04-29css3利用transform變形結(jié)合事件完成扇形導(dǎo)航
這篇文章主要介紹了css3利用transform變形結(jié)合事件完成扇形導(dǎo)航,幫助大家更好的制作CSS3特效,美化自身網(wǎng)頁(yè),感興趣的朋友可以了解下2020-10-26css3 transform過(guò)渡抖動(dòng)問(wèn)題解決
這篇文章主要介紹了css3 transform過(guò)渡抖動(dòng)問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)2020-10-23css3中2D轉(zhuǎn)換之有趣的transform形變效果
在CSS3中,transform屬性應(yīng)用于元素的2D或3D轉(zhuǎn)換,可以利用transform功能實(shí)現(xiàn)文字或圖像的旋轉(zhuǎn)、縮放、傾斜、移動(dòng)這4中類型的形變處理,本文給大家介紹css3中2D轉(zhuǎn)換之有趣2022-02-18