uniapp 左右滑动切换内容

在这里插入图片描述

<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值