上述HTML、CSS和JavaScript代码组合实现了一个简单的翻牌式数字时钟。以下是代码的详细分析:
HTML部分
<!DOCTYPE html>
:声明文档类型为HTML5。<html lang="en">
:定义文档的语言为英语。<head>
:包含文档的元数据,如字符集、视口设置、样式和标题。<title>Verify Account</title>
:设置页面的标题为“Verify Account”,尽管这个标题与时钟功能不直接相关,可能是从其他部分复制或用于其他目的的占位标题。<body>
:页面的主体内容,包含时钟的容器<div class="clock">
。<div class="flipper" id="hours">
、<div class="flipper" id="minutes">
、<div class="flipper" id="seconds">
:分别为小时、分钟和秒的翻牌容器,每个容器内有两个子容器<div class="front">
和<div class="back">
用于显示数字的前后两面。
CSS部分
body
:设置页面的布局为居中对齐,填满整个视口高度,背景色为浅灰色。.clock
:时钟容器的样式,设置为flex布局,使内部的翻牌容器间隔10px。.flipper
:翻牌容器的样式,设置了宽度、高度、位置和透视效果,使得内部的数字卡片能够进行3D翻转。.front
和.back
:数字卡片的前后两面,设置了绝对定位、隐藏背面、居中对齐、字体大小、背景色、文字颜色和圆角。.back
:默认翻转180度,使得背面在初始状态下不可见。.flipper.flip .front
和.flipper.flip .back
:定义了翻转动画,当前后数字不一致时触发。@keyframes flip-down
和@keyframes flip-up
:定义了翻转动画的关键帧,分别用于前面的卡片向下翻转和背面的卡片向上翻转。
JavaScript部分
updateClock
:每秒调用一次,获取当前时间,并分别更新小时、分钟和秒的翻牌容器。updateFlipper
:根据传入的id(小时、分钟或秒)和新的值更新对应的翻牌容器。如果新值与当前显示的值不同,则添加flip
类触发翻转动画,并在300毫秒后(动画的一半时间)更新前后两面的数字,然后移除flip
类。setInterval(updateClock, 1000)
:每秒调用updateClock
函数,更新时钟显示。updateClock()
:立即调用一次updateClock
函数,初始化时钟显示。
总结
此代码实现了一个翻牌效果的数字时钟,其中每个数字(小时、分钟、秒)都由两个面(前面和背面)组成。当时间变化导致数字需要更新时,通过CSS3的3D翻转动画,使得数字看起来像是物理翻牌一样从旧值翻转到新值。这种效果增加了时钟的视觉吸引力和动态感。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.clock {
display: flex;
gap: 10px;
}
.flipper {
width: 100px;
height: 100px;
position: relative;
perspective: 1000px;
}
.front,
.back {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
display: flex;
justify-content: center;
align-items: center;
font-size: 48px;
background-color: #333;
color: #fff;
border-radius: 10px;
}
.back {
transform: rotateX(180deg);
}
.flipper.flip .front {
animation: flip-down 0.6s ease-in-out;
}
.flipper.flip .back {
animation: flip-up 0.6s ease-in-out;
}
@keyframes flip-down {
0% {
transform: rotateX(0);
}
100% {
transform: rotateX(-180deg);
}
}
@keyframes flip-up {
0% {
transform: rotateX(180deg);
}
100% {
transform: rotateX(0);
}
}
</style>
<title>Flip Clock</title>
</head>
<body>
<div class="clock">
<div class="flipper" id="hours">
<div class="front">
00</div>
<div class="back">
00</div>
</div>
<div class="flipper" id="minutes">
<div class="front">
00</div>
<div class="back">
00</div>
</div>
<div class="flipper" id="seconds">
<div class="front">
00</div>
<div class="back">
00</div>
</div>
</div>
<script>
function updateClock() {
const now = new Date();
updateFlipper('hours', now.getHours());
updateFlipper('minutes', now.getMinutes());
updateFlipper('seconds', now.getSeconds());
}
function updateFlipper(id, newValue) {
newValue = newValue.toString().padStart(2, '0');
const flipper = document.getElementById(id);
const currentValue = flipper.querySelector('.front').textContent;
if (newValue !== currentValue) {
flipper.classList.add('flip');
setTimeout(() => {
flipper.querySelector('.front').textContent = newValue;
flipper.querySelector('.back').textContent = newValue;
flipper.classList.remove('flip');
}, 300);
}
}
setInterval(updateClock, 1000);
updateClock(); // 初始化时钟
</script>
</body>
</html>