UniApp滑动页面动画效果实现:简单易懂的示例教程
<p>{/'template/':/'<template>/n <view class=/'slide-container/'>/n <!-- 页面内容 -->/n </view>/n</template>/',/'style/':/'<style>/n.slide-container {/n width: 100%;/n height: 100%;/n overflow: hidden;/n position: relative;/n transition: transform 0.3s ease;/n}/n</style>/',/'script/':/'<script>/nexport default {/n data() {/n return {/n startX: 0, // 记录触摸起始位置/n currentX: 0, // 记录当前位置/n slideWidth: 0, // 滑动页面的宽度/n slideIndex: 0 // 当前页面索引/n }/n },/n methods: {/n // 触摸开始事件/n touchStart(e) {/n this.startX = e.touches[0].clientX;/n },/n // 触摸移动事件/n touchMove(e) {/n const moveX = e.touches[0].clientX - this.startX;/n const translateX = this.currentX + moveX;/n this.slideContainerStyle.transform = <code>translateX(${translateX}px)</code>;/n },/n // 触摸结束事件/n touchEnd(e) {/n const moveX = e.changedTouches[0].clientX - this.startX;/n if (Math.abs(moveX) > this.slideWidth / 2) {/n // 滑动距离超过一半时,切换至下一页或上一页/n this.slideIndex = moveX > 0 ? this.slideIndex - 1 : this.slideIndex + 1;/n }/n this.currentX = -this.slideWidth * this.slideIndex;/n this.slideContainerStyle.transform = <code>translateX(${this.currentX}px)</code>;/n }/n },/n computed: {/n slideContainerStyle() {/n return {/n transform: <code>translateX(${this.currentX}px)</code>/n }/n }/n },/n mounted() {/n // 获取滑动页面的宽度/n const query = uni.createSelectorQuery().in(this);/n query.select('.slide-container').boundingClientRect((rect) => {/n this.slideWidth = rect.width;/n }).exec();/n }/n}/n</script>/</p>
原文地址: https://www.cveoy.top/t/topic/p9dX 著作权归作者所有。请勿转载和采集!