如果你想使用 JavaScript 制作一個模擬時鐘,那么本文將對你有所幫助,我們都知道時鐘有兩種,一種是模擬的,一種是數字的,雖然數字時鐘被廣泛使用,但模擬時鐘也被很多人所喜愛,
🏆使用 HTML、CSS 和 JavaScript 的簡單模擬時鐘
- 🎡 JavaScript 模擬時鐘 [現場演示]
- 🎯 使用 HTML、CSS 和 JavaScript 的簡單模擬時鐘
- 🎬 第 1 步:創建時鐘的基本結構
- 🥇 第 2 步:在時鐘上標記 1 到 12
- 🎪 第 3 步:制作三只指標來指示時間
- ? 第 4 步:使用 JavaScript 代碼激活時鐘
- 🔮 JavaScript 代碼詳解
- 🖍 關于秒針
- 🖌 關于分針
- 🛬 wuhu ! 起飛 !

正如你在上圖中所看到的,這里我借助 HTML、CSS 和 JavaScript 制作了一個簡單的模擬時鐘,早些時候我制作了更多型別的模擬和數字手表,如果你愿意,你可以看看這些設計,
使用 HTML、CSS 和 JavaScript制作模擬時鐘(初學者教程)
JavaScript 設計一個 Neumorphism風格的數字時鐘
眾所周知,模擬時鐘表殼有三個指標和從 1 到 12 的數字,在這里,我也在那個地方使用了符號,而不是使用從 1 到 12 的數字,在這款手表中,我將時針設為最小,然后分別設為分針和秒針,
🎡 JavaScript 模擬時鐘 [現場演示]
如果你想了解這個模擬時鐘是如何作業的,那么你可以觀看下面的演示,在這里,我提供了所需的源代碼,以便你可以復制代碼并在你自己的作業中使用它,
演示:https://haiyong.site/simple-javascript-analog-clock

代碼下載:https://download.csdn.net/download/qq_44273429/21105858
如果你想使用 JavaScript 制作一個模擬時鐘,那么本文將對你有所幫助,我們都知道手表有兩種,一種是模擬的,一種是數字的,雖然數字手表被廣泛使用,但模擬手表也在許多地方使用,
🎯 使用 HTML、CSS 和 JavaScript 的簡單模擬時鐘
希望你喜歡這個設計,我在下面分享了有關我如何進行此設計的完整教程,希望下面的教程能幫到你,
為此,首先,你必須創建一個 HTML 和 CSS 檔案,
🎬 第 1 步:創建時鐘的基本結構
這段 HTML 代碼基本上就是這個模擬時鐘的基本結構,我使用了一些 CSS 代碼來設計這款手表的背景和形狀,正如你在上圖中所看到的,它采用了新形態設計的形式,在這里,我使用 CSS 代碼來實作 Neumorphism 設計,
正如你在上面的演示中看到的,我在這個手表周圍使用了一個邊框來制作代碼邊框:7px solid #282828,我使用 box-shadow 使其更清晰,border-radius 50%使這款手表呈圓形,我還使用了高度和寬度 30 rem,如果你想讓這款手表更大,你可以增加它的尺寸,
<div class="clock">
</div>
html {
background: #282828;
text-align: center;
font-size: 10px;
}
body {
margin: 0;
font-size: 2rem;
display: flex;
flex: 1;
min-height: 100vh;
align-items: center;
}
.clock {
width: 30rem;
height: 30rem;
border: 7px solid #282828;
box-shadow: -4px -4px 10px rgba(67,67,67,0.5),
inset 4px 4px 10px rgba(0,0,0,0.5),
inset -4px -4px 10px rgba(67,67,67,0.5),
4px 4px 10px rgba(0,0,0,0.3);
border-radius: 50%;
margin: 50px auto;
position: relative;
padding: 2rem;
}
演示效果:

🥇 第 2 步:在時鐘上標記 1 到 12
<div class="outer-clock-face">
<div class="marking marking-one"></div>
<div class="marking marking-two"></div>
<div class="marking marking-three"></div>
<div class="marking marking-four"></div>
</div>
.outer-clock-face {
position: relative;
width: 100%;
height: 100%;
border-radius: 100%;
background: #282828;
overflow: hidden;
}
.outer-clock-face::after {
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
transform: rotate(90deg)
}
.outer-clock-face::before,
.outer-clock-face::after,
.outer-clock-face .marking{
content: '';
position: absolute;
width: 5px;
height: 100%;
background: #1df52f;
z-index: 0;
left: 49%;
}
演示效果:

.outer-clock-face .marking {
background: #bdbdcb;
width: 3px;
}
.outer-clock-face .marking.marking-one {
transform: rotate(30deg)
}
.outer-clock-face .marking.marking-two {
transform: rotate(60deg)
}
.outer-clock-face .marking.marking-three {
transform: rotate(120deg)
}
.outer-clock-face .marking.marking-four {
transform: rotate(150deg)
}
演示效果:

我使用下面的 HTML 和 CSS 代碼制作了一個圓圈,結果,長線的中間被覆寫,并且它具有完整的 1 到 12 個標記大小,
HTML:
<div class="inner-clock-face">
</div>
CSS
.inner-clock-face {
position: absolute;
top: 10%;
left: 10%;
width: 80%;
height: 80%;
background: #282828;
-webkit-border-radius: 100%;
-moz-border-radius: 100%;
border-radius: 100%;
z-index: 1;
}
.inner-clock-face::before {
content: '';
position: absolute;
top: 50%;
border-radius: 18px;
margin-left: -9px;
margin-top: -6px;
left: 50%;
width: 16px;
height: 16px;
background: #4d4b63;
z-index: 11;
}
演示效果:
🎪 第 3 步:制作三只指標來指示時間
在這個單元格中,我使用了三只手,它們是使用下面的 HTML 和 CSS 代碼制作的,
HTML:
<div class="hand hour-hand"></div>
<div class="hand min-hand"></div>
<div class="hand second-hand"></div>
CSS:
.hand {
width: 50%;
right: 50%;
height: 6px;
background: #61afff;
position: absolute;
top: 50%;
border-radius: 6px;
transform-origin: 100%;
transform: rotate(90deg);
transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
}
.hand.hour-hand {
width: 30%;
z-index: 3;
}
.hand.min-hand {
height: 3px;
z-index: 10;
width: 40%;
}
.hand.second-hand {
background: #ee791a;
width: 45%;
height: 2px;
}
演示效果:

? 第 4 步:使用 JavaScript 代碼激活時鐘
上面我們設計了整只手表,但這款手表還沒有功能,這意味著這款手表的指標沒有任何功能,也沒有顯示準??確的時間,為此,我們需要使用 JavaScript 代碼,
使用下面的 JavaScript,我已經給出了如何旋轉這些手的說明,如果你了解基本的 JavaScript,你肯定會理解它,
我已經在下面充分解釋了這段 JavaScript 代碼是如何作業的,
const secondHand = document.querySelector('.second-hand');
const minsHand = document.querySelector('.min-hand');
const hourHand = document.querySelector('.hour-hand');
function setDate() {
const now = new Date();
const seconds = now.getSeconds(); // second hand rotation
const secondsDegrees = ((seconds / 60) * 360) + 90;
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
const mins = now.getMinutes(); // minutes hand rotation
const minsDegrees = ((mins / 60) * 360) + ((seconds/60)*6) + 90;
minsHand.style.transform = `rotate(${minsDegrees}deg)`;
const hour = now.getHours(); // Hours hand rotation
const hourDegrees = ((hour / 12) * 360) + ((mins/60)*30) + 90;
hourHand.style.transform = `rotate(${hourDegrees}deg)`;
}
🔮 JavaScript 代碼詳解
🖍 關于秒針
const seconds = now.getSeconds(); // second hand rotation
const secondsDegrees = ((seconds / 60) * 360) + 90;
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
🌟 我已將秒針如何旋轉存盤在secondsDegrees中
🌟 然后我使用rotate (${secondsDegrees} deg) 來旋轉秒針
🌟 1 分鐘等于 60 秒所以我除以 60
🌟 圓的一周是360 度,所以我乘以 360
🖌 關于分針
const mins = now.getMinutes(); // minutes hand rotation
const minsDegrees = ((mins / 60) * 360) + ((seconds/60)*6) + 90;
minsHand.style.transform = `rotate(${minsDegrees}deg)`;
🌟 我在minsDegrees中存盤了如何轉動分鐘的指標
🌟 然后我使用(${minsDegrees]deg)來旋轉分針
🌟 1 小時等于 60 分鐘所以我除以 60
🌟 添加了帶分鐘的秒針位置,因為分針在正確的位置取決于秒
setInterval(setDate, 1000);
setDate();

🛬 wuhu ! 起飛 !
希望你在本教程中了解我是如何使用 HTML、CSS 和 JavaScript制作這個模擬時鐘的,你可以使用下面的下載按鈕下載所需的源代碼,
下載按鈕
我之前使用 HTML、CSS 和 JavaScript 制作了更多型別的小工具,如果你愿意,可以查看這些設計,
使用 HTML、CSS 和 JavaScript 制作的隨機密碼生成器
使用 HTML、CSS、JS 和 API 制作一個很棒的天氣 Web 應用程式
我已經寫了很長一段時間的技術博客,并且主要通過CSDN發表,這是我的一篇 Web 小程式教程,我喜歡通過文章分享技術與快樂,你可以訪問我的博客: https://haiyong.blog.csdn.net/ 以了解更多資訊,希望你們會喜歡!😊
💌 歡迎大家在評論區提出意見和建議!💌
如果你真的從這篇文章中學到了一些新東西,喜歡它,收藏它并與你的小伙伴分享,🤗最后,不要忘了?或📑支持一下哦,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/294498.html
標籤:其他
