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>
相关推荐
码艺-Alimjan18 分钟前
Vben Admin 新增维吾尔语 Vben-Modal的关键坑之一
前端·javascript·vue.js
可乐鸡翅yeah_1 小时前
hls.js 手动自定义 http 请求 loader,修改请求头实战
开发语言·前端·javascript·网络协议·http·ecmascript·m3u8在线
IT_陈寒1 小时前
Vite静态资源导入这个坑我帮你们踩过了
前端·人工智能·后端
广州华水科技1 小时前
大坝安全监测解决方案:单北斗GNSS形变监测系统应用与维护
前端
yivifu2 小时前
中文古籍电子书注释集成
前端·javascript·python·beautifulsoup·epub
weixin_448290253 小时前
书庐开发实战教学
javascript·css·html
默_笙3 小时前
🛴 从散件到整机:DeepAgents 与 Agent 身上预留的那些"插槽"(前置介绍)
前端·javascript
zhangzeyuaaa3 小时前
深入理解 Ruby 运算符:本质、分类、坑点与重载实战
开发语言·前端·ruby
一木 之林3 小时前
DeepSeek Agent 开发(一)
开发语言·前端·javascript
树下水月4 小时前
Typora破解
linux·服务器·前端