uniapp 左右滑动切换内容

html 复制代码
<template>
	<view class="pages" @touchstart="touchStart" @touchend="touchEnd">
		<view class="title">
			<view class="e_title" v-for="(item,index) in list1" :key="index"
				@click.stop="e_click(index)">
				<view :class="num === index ? 'title_fw_big' : 'title_color'">{{item}}</view>
			</view>
		</view>
		<view class="content" v-if="num == 0">内容1</view>
		<view class="content" v-if="num == 1">内容2</view>
		<view class="content" v-if="num == 2">内容3</view>
	</view>
</template>  
<script>  
    export default {  
        data() {  
            return {  
				list1: ['标签1', '标签2', '标签3'],
				num: 0,
                touchStartX: 0,  // 触屏起始点x  
                touchStartY: 0,  // 触屏起始点y  
            };  
        },  
        methods: { 
			//点击 tab切换
			e_click(index) {
				this.num = index;
			},
            //触摸开始  
            touchStart(e) {  
                // console.log("触摸开始")  
                this.touchStartX = e.touches[0].clientX;  
                this.touchStartY = e.touches[0].clientY;  
            },  

            //触摸结束 
            touchEnd(e) {  
                // console.log("触摸结束")  
                let deltaX = e.changedTouches[0].clientX - this.touchStartX;  
                let deltaY = e.changedTouches[0].clientY - this.touchStartY;  
				console.log(deltaX,deltaY,Math.abs(deltaX),Math.abs(deltaY));
				console.log('kkkk',deltaX );
                if (Math.abs(deltaX) > 50 && Math.abs(deltaX) > Math.abs(deltaY)) {
                    if (deltaX >= 0) {  
                        console.log("右滑")  
						if(this.num == 0 ){
							this.num = 0
							// 如果等于0  有别的需求(比如网络请求啥的都可以写)
						}else if(this.num == 1 ){
							this.num = 0
						}else if(this.num == 2 ){
							this.num = 1
						}
                    } else {  
                        console.log("左滑")  
						if(this.num == 0 ){
							this.num = 1
						}else if(this.num == 1 ){
							this.num = 2
						}else if(this.num == 2 ){
							this.num = 2
						}
                    }  
                } 
                //这里我没用到上啦下拉,就注释了
				// else if(Math.abs(deltaY) > 50&& Math.abs(deltaX) < Math.abs(deltaY)) {
    //                 if (deltaY < 0) {  
    //                     console.log("上拉")  
    //                 } else {  
    //                     console.log("下拉")  
    //                 }  
    //             } 
				else {  
                    console.log("可能是点击!")  
                }  
            },            
        }  
    };  
</script>
<style scoped>
	.pages{
		width: 100%;
		height: 100vh;
		background-color: #F0F0F0;
	}
	.title {
		display: flex;
		justify-content: space-around;
		align-items: center;
		height: 80rpx;
		background: #B3D66E;
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		box-sizing: border-box;
	}
	
	.e_title {
		display: flex;
		align-items: center;
		flex-direction: column;
		padding: 15rpx 20rpx;
		font-weight: 300;
	}
	.title_color {
		font-size: 24rpx;
		color: #969696;
	}
	
	.title_fw_big {
		font-size: 30rpx;
		font-weight: bold;
		color: #191919;
		border-bottom: 8rpx solid #F0AD4E;
	}
	.content{
		margin-top: 80rpx;
		width: 100%;
		height: calc(100vh - 80rpx);
		background-color: #FE726B;
		text-align: center;
		font-size: 100rpx
	}
</style>
相关推荐
程序员黑豆5 小时前
Java 注释详解:单行、多行与文档注释的完整指南
java·前端·ai编程
剪刀石头布啊6 小时前
antd中可编辑表格中巧用useWatch实现联动高性能效果,以及定制延伸学习
前端
油丶酸萝卜别吃6 小时前
前端转全栈学习路线
前端·学习
浮生望6 小时前
前端路由进阶:History API原理与手写HistoryRouter
前端
陆枫Larry7 小时前
JavaScript 中的竞态是什么,为啥会有竟态?
前端
上海安当技术9 小时前
半天接入:USBKey RESTful API + C 动态库,Web 和 C/S 两套集成路径实战
前端·后端·restful·集成·usbkey
DevUI团队9 小时前
从“即兴创作”到“规格先行”,华为云码道(CodeArts)代码智能体持续深耕企业级规范驱动开发能力
前端·人工智能·后端
weixin_4316004410 小时前
NestJS 入门(3):Guard 如何挡住未登录请求?
前端·后端·学习·nest.js
avi911110 小时前
[AI教做人]AI平台做2项目;一个3D模型展示,另一个框架多人
javascript·人工智能·ai·3d模型·3d引擎·顶点和法线
kyriewen11 小时前
我用Claude Code两天干完了团队两周的排期——周报发出去那一刻我就后悔了
前端·javascript·ai编程