React实现翻页时钟

1. 布局、样式、翻页动画

翻页时钟把数字分为上下两部分,翻页效果的实现需要通过设置 position 把所有的数组放在同一个位置叠加起来。

翻页时钟的动画是当前显示时间的 up 部分向下翻转至于屏幕垂直的位置,在这个位置需要显示的下一个时间的 down 部分向下翻转,这样就实现了翻页时钟的完整动画。

HTML 结构如下所示:

html 复制代码
<div class="container">
    <ul class="flip">
        <li>
            <div class="up">
                <div class="content">0</div>
            </div>
            <div class="down">
                <div class="content">0</div>
            </div>
        </li>
        <li>
            <div class="up">
                <div class="content">1</div>
            </div>
            <div class="down">
                <div class="content">1</div>
            </div>
        </li>
    </ul>
</div>

CSS 样式

css 复制代码
.container {
	height: 90px;
}
.container .flip {
	position: relative;
	float: left;
	width: 60px;
	height: 100%;
	margin: 0 5px;
}

.flip li {
	position: absolute;
	left: 0;
	top: 0;
	width: 100%;
	height: 100%;
	font-size: 80px;
	text-align: center;
	line-height: 90px;
	font-weight: bold;
	border-radius: 6px;
	perspective: 200px;
	transform-style: preserve-3d;
}

.flip li:first-child {
	z-index: 2;
}
// 设置class为up和down的高度为父元素容器的一半高度
.flip li > div {
	position: absolute;
	left: 0;
	height: 50%;
	width: 100%;
	overflow: hidden;
}

.flip .up {
	/* 改变选中轴的位置 使翻转轴位于up、down部分的中间*/
	transform-origin: 0 100%;
	top: 0;
	border-radius: 4px;
}

.flip .down {
	/* 改变选中轴的位置 使翻转轴位于up、down部分的中间 */
	transform-origin: 0 0;
	bottom: 0;
	border-radius: 4px;
}

.flip .content {
	position: absolute;
	width: 100%;
	border-radius: 6px;
	color: #ccc;
	background-color: #333;
	text-shadow: 0 1px 2px #000;
}

.flip .up .content {
	top: 0;
}
.flip .down .content {
	bottom: 0;
}
// 设置翻页折痕
.flip .up::after {
	content: "";
	position: absolute;
	top: 43px;
	left: 0;
	width: 100%;
	height: 4px;
	background-color: rgba(0, 0, 0, 0.4);
}

实现的3D效果如下:

2. 使用 JS 使动画持续翻页

完整的翻页时钟为 00:00:00,需要翻页的有 6 个部分,可以先把翻页部分提取出单独的组件。

1、准备数据

因为需要时分秒各个翻页部分显示的数字范围不同,所以这里先把数据准备好。

2、封装翻页组件

组件接收两个参数:1、当前翻页类型(时、分、秒); 2、当前类型时间。

index 与当前类型时间相同时,当前元素的 class 为 active,上一个元素的 class 为 before;如果当前元素为第一个元素,最后一个元素的class为before。

3、各个翻页组件间的联动

这里还需要定义一个对象,用于翻页组件间的联动关系的对应。

秒钟的个位转完一周后,秒钟的十位需要进行一次翻页,以此类推,直到时钟的十位进行翻转。实现方式如下:

源码地址

stackblitz.com/edit/vitejs...

相关推荐
xjt_09013 分钟前
浅析Web存储系统
前端
foxhuli22941 分钟前
禁止ifrmare标签上的文件,实现自动下载功能,并且隐藏工具栏
前端
青皮桔1 小时前
CSS实现百分比水柱图
前端·css
失落的多巴胺1 小时前
使用deepseek制作“喝什么奶茶”随机抽签小网页
javascript·css·css3·html5
DataGear1 小时前
如何在DataGear 5.4.1 中快速制作SQL服务端分页的数据表格看板
javascript·数据库·sql·信息可视化·数据分析·echarts·数据可视化
影子信息1 小时前
vue 前端动态导入文件 import.meta.glob
前端·javascript·vue.js
青阳流月1 小时前
1.vue权衡的艺术
前端·vue.js·开源
样子20181 小时前
Vue3 之dialog弹框简单制作
前端·javascript·vue.js·前端框架·ecmascript
kevin_水滴石穿1 小时前
Vue 中报错 TypeError: crypto$2.getRandomValues is not a function
前端·javascript·vue.js
翻滚吧键盘1 小时前
vue文本插值
javascript·vue.js·ecmascript