js實(shí)現(xiàn)手表表盤(pán)時(shí)鐘與圓周運(yùn)動(dòng)
蘋(píng)果手表表盤(pán)時(shí)鐘與js圓周運(yùn)動(dòng)
實(shí)現(xiàn)結(jié)果
需求分析:
1、時(shí)鐘時(shí)間按照北京時(shí)間進(jìn)行顯示;
2、時(shí)針、分針、秒針按照時(shí)鐘運(yùn)轉(zhuǎn)標(biāo)準(zhǔn)進(jìn)行運(yùn)轉(zhuǎn);
3、小球跟隨秒表圍繞表盤(pán)進(jìn)行圓周運(yùn)動(dòng)。
代碼分析
1、html結(jié)構(gòu):時(shí)針、分針、秒針、小球分別用一個(gè)div,將他們一起放到一個(gè)大的div中;
2、css樣式:表盤(pán)布局多使用相對(duì)定位與絕對(duì)定位,將表針與各時(shí)刻標(biāo)刻移動(dòng)到特定的位置;
3、js行為:為了實(shí)現(xiàn)動(dòng)態(tài)獲取時(shí)間,可以使用var now=new Date(),再利用定時(shí)器setInterval,實(shí)現(xiàn)每經(jīng)過(guò)1s重新獲取當(dāng)前時(shí)間。
核心函數(shù)
時(shí)鐘運(yùn)動(dòng)
function getTimeDeg(){ //獲取當(dāng)前時(shí)間,計(jì)算在時(shí)鐘上,時(shí)、分、秒對(duì)應(yīng)轉(zhuǎn)動(dòng)角度 var now=new Date(); var s=now.getSeconds(); var sDeg=s/60*360; var m=now.getMinutes(); var mDeg=(m*60+s)/3600*360; var h=now.getHours(); var hDeg=(h*3600+m*60+s)/(3600*12)*360; divH.style.transform=`rotate(${hDeg}deg)`; divM.style.transform=`rotate(${mDeg}deg)`; divS.style.transform=`rotate(${sDeg}deg)`; }
圓周運(yùn)動(dòng)
function circleMotion(){ var now=new Date(); var sball=now.getSeconds(); var saDeg=sball/60*360-90; var r = 250; var radian = saDeg*Math.PI/180; var top = Math.cos(radian)*r; var left = Math.sin(radian)*r; ball.style.left= top+'px'; ball.style.top = left+'px'; }
完整源代碼(復(fù)制可用)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>表盤(pán)時(shí)鐘</title> <style> #circle{ width: 500px;height: 500px;border-radius: 255px; position: relative;top: 50px;left: 500px; background: radial-gradient(black,grey); border:#f7f7f7 10px solid; box-shadow: 0px 0px 0px 2px #a3a4a6,0px 0px 0px 32px #dedfe1; } #ball{ width: 30px;height: 30px;border-radius: 15px; background-color: red;position: absolute; margin-top: 235px;margin-left: 235px; z-index: 6; } #dian{ width: 40px;height: 40px;border-radius: 20px; background-color:white;position: absolute; margin-top: 230px; margin-left: 230px; box-shadow:0px -15px 10px -7px #867c7c inset,0px 0px 2px 0px black ; z-index: 6; } #h{ width:8px;height: 100px;background-color: white; position: absolute;border-radius: 8px;left: 246px;top: 150px; box-shadow:0px 0px 2px 1px #867c7c inset,0px 0px 1px 0px black ; z-index: 3; } #m{ width:6px;height: 150px;background-color: white; position: absolute;top: 100px;left: 247px;border-radius: 6px; box-shadow:0px 0px 1px 0px #867c7c inset,0px 0px 1px 0px black ; z-index: 4; } #s{ width:2px;height: 180px;background-color: red; position: absolute;top: 70px;left: 249px;border-radius: 2px; z-index: 5; } #s,#m,#h{ transform-origin: center bottom; } .tip{ width: 6px;height: 26px;border-radius: 3px;background-color: white; position: absolute;box-shadow:0px 0px 2px 1px #867c7c inset,0px 0px 1px 0px black ; } #time1{ top: 34px;left: 372px;transform-origin:top center ;transform: rotate(30deg); } #time2{ top: 125px;left: 463px;transform-origin:top center ;transform: rotate(60deg); } #time3{ top: 230px;left: 475px;transform: rotate(90deg); width: 10px;height: 40px;border-radius: 5px; } #time4{ top: 349px;left: 460px;transform-origin:bottom center ;transform: rotate(-60deg); } #time5{ top: 440px;left: 369px;transform-origin:bottom center ;transform: rotate(-30deg); } #time6{ top: 460px;left: 245px;width: 10px;height: 40px;border-radius: 5px; } #time7{ top: 440px;left: 122px;transform-origin:bottom center ;transform: rotate(30deg); } #time8{ top: 349px;left: 31px;transform-origin:bottom center ;transform: rotate(60deg); } #time9{ top: 230px;left: 15px;transform: rotate(90deg); width: 10px;height: 40px;border-radius: 5px; } #time10{ top: 124px;left: 30px;transform-origin:top center ;transform: rotate(-60deg); } #time11{ top: 33px;left: 121px;transform-origin:top center ;transform: rotate(-30deg); } #time12{ top: 0px;left: 245px; width: 10px;height: 40px;border-radius: 5px; } </style> </head> <body> <div id="circle"> <div id="h"></div> <div id="m"></div> <div id="s"></div> <div id="ball" class="tip"></div> <div id="dian" class="tip"></div> <div id="time1" class="tip"></div> <div id="time2" class="tip"></div> <div id="time3" class="tip"></div> <div id="time4" class="tip"></div> <div id="time5" class="tip"></div> <div id="time6" class="tip"></div> <div id="time7" class="tip"></div> <div id="time8" class="tip"></div> <div id="time9" class="tip"></div> <div id="time10" class="tip"></div> <div id="time11" class="tip"></div> <div id="time12" class="tip"></div> </div> <script> //獲取div節(jié)點(diǎn) var ball=document.getElementById("ball") var divS=document.getElementById("s"); var divM=document.getElementById("m"); var divH=document.getElementById("h"); //調(diào)用函數(shù),可刪除,刪除后需等待1s才能看到運(yùn)轉(zhuǎn) getTimeDeg(); //設(shè)置間隔時(shí)間為1s的定時(shí)器,每1s運(yùn)行一次函數(shù) setInterval(getTimeDeg,1000); //時(shí)間獲取函數(shù) circleMotion(); setInterval(circleMotion,1000); //時(shí)鐘運(yùn)動(dòng) function getTimeDeg(){ //獲取當(dāng)前時(shí)間,計(jì)算在時(shí)鐘上,時(shí)、分、秒對(duì)應(yīng)轉(zhuǎn)動(dòng)角度 var now=new Date(); var s=now.getSeconds(); var sDeg=s/60*360; var m=now.getMinutes(); var mDeg=(m*60+s)/3600*360; var h=now.getHours(); var hDeg=(h*3600+m*60+s)/(3600*12)*360; divH.style.transform=`rotate(${hDeg}deg)`; divM.style.transform=`rotate(${mDeg}deg)`; divS.style.transform=`rotate(${sDeg}deg)`; } //圓周運(yùn)動(dòng) function circleMotion(){ var now=new Date(); var sball=now.getSeconds(); var saDeg=sball/60*360-90; var r = 250; var radian = saDeg*Math.PI/180; var top = Math.cos(radian)*r; var left = Math.sin(radian)*r; ball.style.left= top+'px'; ball.style.top = left+'px'; } </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
分離與繼承的思想實(shí)現(xiàn)圖片上傳后的預(yù)覽功能:ImageUploadView
本文要介紹的是網(wǎng)頁(yè)中常見(jiàn)的圖片上傳后直接在頁(yè)面生成小圖預(yù)覽的實(shí)現(xiàn)思路,考慮到該功能有一定的適用性,于是把相關(guān)的邏輯封裝成了一個(gè)ImageUploadView組件,實(shí)際使用效果可查看下一段的git效果圖2016-04-04- 在TypeScript中,泛型是一種創(chuàng)建可復(fù)用代碼組件的工具。這種組件不只能被一種類(lèi)型使用,而是能被多種類(lèi)型復(fù)用。類(lèi)似于參數(shù)的作用,泛型是一種用以增強(qiáng)類(lèi)(classes)、類(lèi)型(types)和接口(interfaces)能力的非??煽康氖侄?/div> 2022-09-09
js實(shí)現(xiàn)瀑布流的一種簡(jiǎn)單方法實(shí)例分享
現(xiàn)在說(shuō)瀑布流式布局似乎有點(diǎn)晚了,但是每一項(xiàng)技術(shù)都是向著“精”和“簡(jiǎn)”的方向在不斷發(fā)展,在發(fā)展到極致之前,需要一個(gè)相當(dāng)漫長(zhǎng)的過(guò)程,因此,從這個(gè)角度來(lái)說(shuō),當(dāng)瀑布流被應(yīng)用得越來(lái)越多的時(shí)候,反而更應(yīng)該討論它,討論如何將它改善2013-11-11容易造成JavaScript內(nèi)存泄露幾個(gè)方面
這篇文章主要介紹了容易造成JavaScript內(nèi)存泄露幾個(gè)方面,本文講解了多個(gè)會(huì)在Chrome V8中產(chǎn)生內(nèi)存泄漏的示例,需要的朋友可以參考下2014-09-09一個(gè)JavaScript函數(shù)把URL參數(shù)解析成Json對(duì)象
一個(gè)JavaScript函數(shù)parseQueryString,它的用途是把URL參數(shù)解析為一個(gè)對(duì)象,很實(shí)用,大家可以看看2014-09-09微信小程序?qū)崿F(xiàn)帶參數(shù)的分享功能(兩種方法)
本文通過(guò)兩種方法給大家介紹微信小程序?qū)崿F(xiàn)帶參數(shù)的分享,需要的朋友可以參考下2019-05-05elemetUi 組件--el-upload實(shí)現(xiàn)上傳Excel文件的實(shí)例
這篇文章主要介紹了elemetUi 組件--el-upload實(shí)現(xiàn)上傳Excel文件的實(shí)例的相關(guān)資料,希望通過(guò)本文大家能夠?qū)崿F(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10Javascript中對(duì)象繼承的實(shí)現(xiàn)小例
這篇文章主要介紹了Javascript中對(duì)象繼承的實(shí)現(xiàn),需要的朋友可以參考下2014-05-05當(dāng)json鍵為數(shù)字時(shí)的取值方法解析
對(duì)于數(shù)字鍵名或者非正常變量字符(比如有空格),必須使用 aa[x]的方式2013-11-11最新評(píng)論