前端實(shí)現(xiàn)時(shí)鐘網(wǎng)頁(yè)的詳細(xì)代碼示例
項(xiàng)目介紹
時(shí)鐘顯示在網(wǎng)頁(yè)中央,并且使網(wǎng)頁(yè)能夠切換白天和夜晚兩種模式。搭建基本的html結(jié)構(gòu),動(dòng)態(tài)得到實(shí)時(shí)的時(shí),分,秒通過(guò)Date()函數(shù)獲得。將得到的數(shù)字根據(jù)邏輯,綁定給各div結(jié)構(gòu),實(shí)行動(dòng)態(tài)旋轉(zhuǎn)。點(diǎn)擊按鈕,改變背景顏色給出最終的HTML代碼。
代碼
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>現(xiàn)代風(fēng)格時(shí)鐘</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
transition: background-color 0.5s;
}
.clock-container {
display: flex;
flex-direction: column;
align-items: center;
}
.clock {
width: 200px;
height: 200px;
border-radius: 50%;
background-color: white;
position: relative;
box-shadow: 0 0 20px rgba(0,0,0,0.1);
}
.hand {
position: absolute;
bottom: 50%;
left: 50%;
transform-origin: 50% 100%;
border-radius: 5px;
}
.hour {
width: 4px;
height: 50px;
background-color: #333;
}
.minute {
width: 3px;
height: 70px;
background-color: #666;
}
.second {
width: 2px;
height: 90px;
background-color: #e74c3c;
}
.center {
position: absolute;
top: 50%;
left: 50%;
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #333;
transform: translate(-50%, -50%);
}
.digital-time {
font-size: 48px;
margin-top: 20px;
color: #333;
}
.mode-switch {
margin-top: 20px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #333;
color: white;
border: none;
border-radius: 5px;
transition: background-color 0.3s;
}
.mode-switch:hover {
background-color: #555;
}
.night-mode {
background-color: #222;
}
.night-mode .clock {
background-color: #333;
box-shadow: 0 0 20px rgba(255,255,255,0.1);
}
.night-mode .hour, .night-mode .minute {
background-color: #fff;
}
.night-mode .second {
background-color: #e74c3c;
}
.night-mode .center {
background-color: #fff;
}
.night-mode .digital-time {
color: #fff;
}
.night-mode .mode-switch {
background-color: #f0f0f0;
color: #333;
}
.night-mode .mode-switch:hover {
background-color: #ddd;
}
</style>
</head>
<body>
<div class="clock-container">
<div class="clock">
<div class="hand hour"></div>
<div class="hand minute"></div>
<div class="hand second"></div>
<div class="center"></div>
</div>
<div class="digital-time"></div>
<button class="mode-switch">Dark mode</button>
</div>
<script>
function updateClock() {
const now = new Date();
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
const hourDeg = (hours % 12 + minutes / 60) * 30;
const minuteDeg = (minutes + seconds / 60) * 6;
const secondDeg = seconds * 6;
document.querySelector('.hour').style.transform = `translateX(-50%) rotate(${hourDeg}deg)`;
document.querySelector('.minute').style.transform = `translateX(-50%) rotate(${minuteDeg}deg)`;
document.querySelector('.second').style.transform = `translateX(-50%) rotate(${secondDeg}deg)`;
const digitalTime = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')} ${hours >= 12 ? 'PM' : 'AM'}`;
document.querySelector('.digital-time').textContent = digitalTime;
}
function toggleMode() {
document.body.classList.toggle('night-mode');
const button = document.querySelector('.mode-switch');
button.textContent = document.body.classList.contains('night-mode') ? 'Light mode' : 'Dark mode';
}
setInterval(updateClock, 1000);
updateClock(); // 立即更新一次,避免延遲
document.querySelector('.mode-switch').addEventListener('click', toggleMode);
</script>
</body>
</html>
效果圖

總結(jié)
到此這篇關(guān)于前端實(shí)現(xiàn)時(shí)鐘網(wǎng)頁(yè)的文章就介紹到這了,更多相關(guān)前端時(shí)鐘網(wǎng)頁(yè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JavaScript實(shí)現(xiàn)簡(jiǎn)單的時(shí)鐘實(shí)例代碼
- html5 canvas js(數(shù)字時(shí)鐘)實(shí)例代碼
- JS 實(shí)現(xiàn)倒計(jì)時(shí)數(shù)字時(shí)鐘效果【附實(shí)例代碼】
- js實(shí)現(xiàn)一個(gè)簡(jiǎn)單的數(shù)字時(shí)鐘效果
- 一個(gè)簡(jiǎn)易時(shí)鐘效果js實(shí)現(xiàn)代碼
- 五步輕松實(shí)現(xiàn)JavaScript HTML時(shí)鐘效果
- Javascript實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果
- JavaScript實(shí)現(xiàn)抖音羅盤(pán)時(shí)鐘
- 使用JS實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘
相關(guān)文章
ajax請(qǐng)求get與post的區(qū)別總結(jié)
本篇文章是對(duì)ajax請(qǐng)求get與post的區(qū)別進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11
解決layer彈出層的內(nèi)容頁(yè)點(diǎn)擊按鈕跳轉(zhuǎn)到新的頁(yè)面問(wèn)題
今天小編就為大家分享一篇解決layer彈出層的內(nèi)容頁(yè)點(diǎn)擊按鈕跳轉(zhuǎn)到新的頁(yè)面問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
小程序?qū)崿F(xiàn)訂單評(píng)價(jià)和商家評(píng)價(jià)
這篇文章主要為大家詳細(xì)介紹了小程序?qū)崿F(xiàn)訂單評(píng)價(jià)和商家評(píng)價(jià)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
JS實(shí)現(xiàn)圖表日期分類(lèi)按色顯示效果
這篇文章主要介紹了JS實(shí)現(xiàn)圖表日期分類(lèi)按色顯示,文中需要注意若 iOS 手機(jī)預(yù)覽時(shí)顯示NaN ,可能是時(shí)間格式的問(wèn)題,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05
Javascript如何判斷數(shù)據(jù)類(lèi)型和數(shù)組類(lèi)型
這篇文章主要介紹Javascript如何判斷數(shù)據(jù)類(lèi)型和數(shù)組類(lèi)型,通俗易懂,需要的朋友可以參考下。2016-06-06

