大概效果是這樣的
html程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>實時時鐘</title>
</head>
<style>
*{
padding: 0px;
margin: 0px;
box-sizing: border-box;
}
body{
font-family: monospace;
font-size: 2rem;
min-height: 100vh;
display: grid;
overflow-y: hidden;
place-content:center;
background: linear-gradient(-45deg,#c4d2ef,#dfe6f6);
}
.hr,
.min,
.sec{
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 2rem;
grid-row: 1/2;
align-items: start;
}
.number{
padding: 0.5rem;
width: 4rem;
height: 4rem;
display: grid;
place-items: center;
transition: 500ms 100ms ease;
border-radius: 50%;
}
.number.pop{
color: #3e6ccd;
font-weight: bold;
transform: scale(1.3);
background-color: #dfe6f6;
box-shadow: -10px -10px 20px -5px #f9fbfd, 10px 10px 20px #a9bee8;
}
.strip{
transition: transform 500ms ease-in-out;
border-radius: 8px;
background: #dfe6f6;
box-shadow: -10px -10px 20px -5px #f9fbfd, 10px 10px 20px #a9bee8;
}
.clock{
display: grid;
grid-template-columns: repeat(3,1fr);
grid-gap:3rem;
height: 4rem;
position: relative;
padding: 0 4rem;
}
</style>
<body>
<div class="clock">
<div class="hr">
<div class="strip">
<div class="number">0</div>
<div class="number">1</div>
<div class="number">2</div>
</div>
<div class="strip">
<div class="number">0</div>
<div class="number">1</div>
<div class="number">2</div>
<div class="number">3</div>
<div class="number">4</div>
<div class="number">5</div>
<div class="number">6</div>
<div class="number">7</div>
<div class="number">8</div>
<div class="number">9</div>
</div>
</div>
<div class="min">
<div class="strip">
<div class="number">0</div>
<div class="number">1</div>
<div class="number">2</div>
<div class="number">3</div>
<div class="number">4</div>
<div class="number">5</div>
</div>
<div class="strip">
<div class="number">0</div>
<div class="number">1</div>
<div class="number">2</div>
<div class="number">3</div>
<div class="number">4</div>
<div class="number">5</div>
<div class="number">6</div>
<div class="number">7</div>
<div class="number">8</div>
<div class="number">9</div>
</div>
</div>
<div class="sec">
<div class="strip">
<div class="number">0</div>
<div class="number">1</div>
<div class="number">2</div>
<div class="number">3</div>
<div class="number">4</div>
<div class="number">5</div>
</div>
<div class="strip">
<div class="number">0</div>
<div class="number">1</div>
<div class="number">2</div>
<div class="number">3</div>
<div class="number">4</div>
<div class="number">5</div>
<div class="number">6</div>
<div class="number">7</div>
<div class="number">8</div>
<div class="number">9</div>
</div>
</div>
</div>
<script>
const strips = [...document.querySelectorAll(".strip")];
const numberSize = "4";
function highlight(strip, d) {
strips[strip]
.querySelector(`.number:nth-of-type(${d + 1})`)
.classList.add("pop");
setTimeout(() => {
strips[strip]
.querySelector(`.number:nth-of-type(${d + 1})`)
.classList.remove("pop");
}, 950);
}
function stripSlider(strip, number) {
let d1 = Math.floor(number / 10);
let d2 = number % 10;
strips[strip].style.transform = `translateY(${d1 * -numberSize}rem)`;
highlight(strip, d1);
strips[strip + 1].style.transform = `translateY(${d2 * -numberSize}rem)`;
highlight(strip + 1, d2);
}
setInterval(() => {
const time = new Date();
const hours = time.getHours();
const mins = time.getMinutes();
const secs = time.getSeconds();
stripSlider(0, hours);
stripSlider(2, mins);
stripSlider(4, secs);
}, 1000);
</script>
</body>
</html>