竹白記事本,Javascript 30,紀錄。
Javascript 30
用 JS 與 CSS 搭配製作一個實時的時鐘效果。
setInterval
計時器課程提供的原始 CSS,在計算角度時,要補 90度,因此這邊會稍作修改。
一個圓有 360 度:
在 .clock-face
新增一個中心點方便定位。
.clock-face::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border-radius: 50%;
background-color: white;
width: 10px;
height: 10px;
}
將原本的 .hand
改掉,
.hand {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
新增 .hour-hand
.min-hand
.second-hand
:
我們將指針的樣式寫在偽元素,這樣旋轉容器時,就會以中心旋轉。
並且將指針起點改成 12 點鐘方向,這樣就不用額外計算多出來的角度。
.hour-hand::after {
position: absolute;
content: '';
width: 10px;
height: 100px;
background-image: linear-gradient(black 50%, transparent 50% 100%);
}
.min-hand::after {
position: absolute;
content: '';
width: 10px;
height: 200px;
background-image: linear-gradient(black 50%, transparent 50% 100%);
}
.second-hand::after {
position: absolute;
content: '';
width: 4px;
height: 160px;
background-image: linear-gradient(black 50%, transparent 50% 100%);
}
const hour = document.querySelector('.hour-hand');
const min = document.querySelector('.min-hand');
const second = document.querySelector('.second-hand');
setClock()
function setClock() {
const now = new Date();
let hourDeg = now.getHours() * 30;
let minDeg = now.getMinutes() * 6;
let secondDeg = now.getSeconds() * 6;
hour.style.transform = `rotate(${hourDeg}deg)`;
min.style.transform = `rotate(${minDeg}deg)`;
second.style.transform = `rotate(${secondDeg}deg)`;
}
JavaScript 的 Date 物件只能以建構式的方式來產生,因此這裡用到 new
關鍵字,取得現在時間。
分別取得秒、分、時的時間,並計算角度,用來控制指針的旋轉度數。
setClock(); // 初始化畫面
setInterval(setClock, 1000);
setInterval
計時器,可以設定每隔一段時間重複執行,這裡設 1 秒。
另外關於畫面顯示的計時器也可以使用 requestAnimationFrame
,它會跟據硬體的效能來控制更新頻率,簡單來說就是會以你硬體能顯示最大 FPS 來刷新頻率。
目前已完成初步功能。
由於現實中指針不會是時間一到就直接跳到下一格,因此這裡的時針與分針還需要補上一些細節。
稍作微調程式碼:
let secondDeg = now.getSeconds() * 6;
let minDeg = now.getMinutes() * 6 + now.getSeconds() * (6 / 60);
let hourDeg = now.getHours() * 30 + now.getMinutes() * (30 / 60);
(function() {
const hour = document.querySelector('.hour-hand');
const min = document.querySelector('.min-hand');
const second = document.querySelector('.second-hand');
function setClock() {
const now = new Date();
let secondDeg = now.getSeconds() * 6;
let minDeg = now.getMinutes() * 6 + now.getSeconds() * (6 / 60);
let hourDeg = now.getHours() * 30 + now.getMinutes() * (30 / 60);
second.style.transform = `rotate(${secondDeg}deg)`;
min.style.transform = `rotate(${minDeg}deg)`;
hour.style.transform = `rotate(${hourDeg}deg)`;
}
setClock(); // 初始化畫面
setInterval(setClock, 1000);
})();