vue 3D 翻页效果

<template>
	<view class="swipe-container" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd">
		<view class="page">初始页</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				startX: 0, // 触摸开始的 X 坐标
				moveX: 0, // 当前移动的 X 坐标
				threshold: 100, // 滑动阈值
			};
		},
		methods: {
			onTouchStart(event) {
				this.startX = event.touches[0].clientX;
			},
			onTouchMove(event) {
				this.moveX = event.touches[0].clientX - this.startX;
			},
			onTouchEnd() {
				const page = document.getElementsByClassName("page")[0];
				if (this.moveX > this.threshold) {
					console.log("向右滑动");
					page.style.transformOrigin = 'right';
					
					const animation = page.animate(
						[{
								transform: 'rotateY(0deg)'
							},
							{
								transform: 'rotateY(90deg)'
							}
						], {
							duration: 1000,
							iterations: 1, //循环一次
							//fill: 'forwards'
						}
					);
					//动态加载数据
					animation.onfinish = () => {
						//animation.reverse();
						console.log('动画完成!');
						//数据渲染到.page元素中
					};
				} else if (this.moveX < -this.threshold) {
					console.log("向左滑动");
					page.style.transformOrigin = 'left';
					const animation = page.animate(
						[{
								transform: 'rotateY(0deg)'
							},
							{
								transform: 'rotateY(-90deg)'
							}
						], {
							duration: 1000,
							iterations: 1, //循环一次
							//fill: 'forwards' // 动画结束后保持最终状态
						}
					);
					//动态加载数据
					animation.onfinish = () => {
						//animation.reverse();
						console.log('动画完成2');
						//数据渲染到.page元素中
					};
				}
				this.moveX = 0; // 重置移动距离
			},
		}
	}
</script>

<style scoped lang="scss">
	.swipe-container {
		width: 100%;
		height: 500px;
		display: flex;
		justify-content: center;
		align-items: center;
		
		perspective: 1000px;
	}

	.swipe-container .page {
		width: 100%;
		height: 100%;

		/* 保留 3D 变换 */
		transform-style: preserve-3d;
		background-color: red;
		
		/* 提示浏览器使用硬件加速 */
		will-change: transform;
	}
</style>
相关推荐
天天向上10248 分钟前
VSCode 使用import导入js/vue等时添加智能提示,并可跳转到定义
javascript·vue.js·vscode
阿丽塔~10 分钟前
vue3 下载文件 responseType-blob 或者 a标签
前端·vue·excel
七灵微1 小时前
【前端】Axios & AJAX & Fetch
前端·javascript·ajax
究极无敌暴龙战神X2 小时前
一篇文章学懂Vuex
前端·javascript·vue.js
shaoin_22 小时前
Vue3中ref与reactive的区别
前端·vue.js
院人冲冲冲2 小时前
微前端qiankun打包部署
开发语言·前端·javascript
我命由我123453 小时前
微信小程序 - 页面跳转(wx.navigateTo、wx.redirectTo、wx.switchTab、wx.reLaunch)
前端·微信小程序·小程序·前端框架·html·html5·js
肖老师xy3 小时前
uniapp修改picker-view样式
前端·uni-app
Oracle_6663 小时前
《Linux 指令集:开启极客世界的钥匙_01》
linux·运维·前端
渲染101专业云渲染3 小时前
渲染 101 平台 3ds Max 建筑动画渲染全攻略:费用与时间
3d·云计算·3dsmax·blender·maya·houdini