




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第JavaScript實現時鐘功能本文實例為大家分享了JavaScript實現時鐘功能的具體代碼,供大家參考,具體內容如下
1、HTML和CSS部分
1.1HTML部分
1.1.1先放在一個容器中clock,存放用來存放時針、分鐘、秒鐘的容器
divclass="clock"/div
1.1.2用來存放時針、分鐘、秒鐘的容器
div
div/div
/div
1.1.3各個時針、分針、秒鐘的容器
div
div
div/div
div/div
div/div
/div
/div
1.2CSS部分
1.2.1背景、布局
html{
background:#018DEDurl(https://unsplash.it/1500/1000image=881blur=5);
background-size:cover;
font-family:'helveticaneue';
text-align:center;
font-size:10px;
body{
margin:0;
font-size:2rem;
display:flex;
flex:1;
min-height:100vh;
align-items:center;
}
1.2.2設置時鐘的外圓邊框
.clock{
width:30rem;
height:30rem;
border:20pxsolidwhite;
border-radius:50%;
margin:50pxauto;
position:relative;
padding:2rem;
box-shadow:
0004pxrgba(0,0,0,0.1),
inset0003px#EFEFEF,
inset0010pxblack,
0010pxrgba(0,0,0,0.2);
}
1.2.3設置存放時分秒的容器
.clock-face{
position:relative;
width:100%;
height:100%;
transform:translateY(-3px);
.clock-face:after{
content:'';
width:30px;
height:30px;
border-radius:100%;
background-color:white;
display:block;
top:50%;
left:50%;
transform:translate(-50%,-50%);
position:absolute;
}
1.2.4設置時分秒對應的容器
.hand{
position:absolution;
width:100%;
height:100%;
.second-hand:after{
content:'';
display:block;
width:5px;
height:50%;
background-color:black;
left:50%;
bottom:50%;
transform:translate(-50%,0%);
position:absolute;
.min-hand:after{
content:'';
display:block;
width:10px;
height:40%;
background-color:gray;
left:50%;
bottom:50%;
transform:translate(-50%,0%);
position:absolute;
.hour-hand:after{
content:'';
display:block;
width:15px;
height:20%;
background-color:white;
left:50%;
bottom:50%;
transform:translate(-50%,0%);
position:absolute;
}
transform布局的方式
2、JS部分(IIFE立即函式,不會外露)
(1)首先要實現的是獲取當前的時間和針
(2)設置時間的轉動
2.1先獲取每個針
letsecond=document.querySelector('.second-hand');
letmin=document.querySelector('.min-hand');
lethour=document.querySelector('.hour-hand');
2.2時針的指向
2.2.1獲取當前的時間
functionsetDate(){
//首先先獲取時間,用Data()
letdata=newData();
}
2.2.2讓上面獲取的指針等于Data()的時間
1.這里要使用到transform:rotate()函數,獲得要轉動的角度。
second.style.transform=`rotate(數字/變量+Deg)`;
min.style.transform=`rotate(數字/變量+Deg)`;
hour.style.transform=`rotate(數字/變量+Deg)`;
2.由于當前時間的時分秒還沒有獲取,所以要先獲取當前時分秒的時間,當成變量賦值給rotate函數
letsecondDeg=data.getSeconds();
letminDeg=data.getMinutes();
lethourDeg=data.getHours();
3.由于rotate需要獲得的是角度而不是時間,所以你需要分別計算出時分秒走動一次需要多少的角度。
letsecondDeg=data.getSeconds()*6;
letminDeg=data.getMinutes()*6;
lethourDeg=data.getHours()*30;
4.完整的賦值
letsecond=document.querySelector('.second-hand')
letmin=document.querySelector('.min-hand')
lethour=document.querySelector('.hour-hand')
functionsetClock(){
letdata=newDate();
letsecondDeg=data.getSeconds()*6//(deg=360/60)
letminDeg=data.getMinutes()*6//(deg=360/60)
lethourDeg=data.getHours()*30//(deg=360/12)
second.style.transform=`rotate(${secondDeg}deg)`
min.style.transform=`rotate(${minDeg}deg)`
hour.style.transform=`rotate(${hourDeg}deg)`
setClock();
2.2設置計時器讓他們走動
/**多種計時器的方法
*1.setInterval(函數,時間)
*2.setTimeout(函數,時間)
*3.window.requestAnimationFrame(函數)這個不需要設置時間,它會根據你硬件的速度來更改時間
**/
setInterval(setDate,1000);//設定間隔,執行很多次
/*functiontimeoutHandler(){
setDate();
setTimeout(timeoutHandler,1000);//這樣就可以連續執行了
setTimeout(timeoutHandler,1000);//延遲多久執行一次*/
/*functionanimationHandler(){
setDate();
window.requestAnimationFrame(animationHandler);//處理畫面更新的setTimeout
window.requestAnimationFrame(animationHandler);*/
2.3發現問題
發現分針和時針會等到秒鐘走完一圈后才移動一個,我們應該讓他緩慢的移動,所以需要在時分后面算上分秒的角度
*一分鐘走60秒,而分針走一小格的角度是6,所以一度走幾秒算出來,單位是度/秒。最后乘以時間,所以單位是度/秒秒=度。
letsecondDeg=d
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 陜西省渭南市蒲城縣2024-2025學年七年級下學期末質量檢測語文試卷(含答案)
- 山東省德州市2025屆高三下學期三模生物試卷(有答案)
- 巾幗活動展覽活動方案
- 工會冬季出游活動方案
- 工廠小年活動策劃方案
- 展館內文化沙龍活動方案
- 小班特色涂鴉活動方案
- 居家辦公解壓活動方案
- 就醫補貼活動方案
- 常州金店端午活動方案
- 《應用寫作》(第三版)復習思考、案例訓練參考答案
- 超星《中華詩詞之美》葉嘉瑩期末考試答案文學藝術古代文學資料
- 湖北襄陽市檢察機關-襄陽市城郊地區檢察院招考聘用67人模擬預測(共500題)筆試參考題庫附答案詳解
- 勸返復學情況說明(包含原因、措施、方案、成效)
- 國家開放大學電大《建筑制圖基礎》機考網考題庫及答案
- GB/T 3098.6-2023緊固件機械性能不銹鋼螺栓、螺釘和螺柱
- 業務為本:華為和阿里的HRBP價值創造三層十二式
- 2022-2023學年河南省鄭州外國語中學數學八年級第二學期期末調研試題含解析
- 畜禽糞污處理項目合作協議書
- RFJ05-2009-DQ人民防空工程電氣大樣圖集
- Unit 2 Neither Pine nor Apple in Pineapple-高中英語外研版(2019)必修第一冊
評論
0/150
提交評論