uniapp开发微信小程序之左右滑动切换页签(两种方法)、返回上一页(附demo)

两种方式实现左右滑动切换页签:①利用swiper组件 ②监听touch事件

滑动返回上一页:监听touch事件

先看效果

前端demo工程:https://ext.dcloud.net.cn/plugin?id=20674

方案1:利用swiper组件实现左右滑动切换页签

前端代码:swiper.vue

      <view class="tabs">        <view v-for="(tab,index) in tabs" :key="index" class="tab">          <text @click="()=>clickItem(index)" :class="index === current?'active':''">            {{tab}}          </text>        </view>      </view>      <swiper class="swiper" :current="current" @change="changeItem">          <swiper-item v-for="(item, index) in items" :key="index">              <view class="item">                <!-- 页签内容 -->                <text>{{item}}</text>              </view>          </swiper-item>      </swiper>
    data() {      return {        tabs:['数学','语文','物理','化学'],                          //页签        items:['数学是研究数量、结构、变化、空间以及信息等概念的一门学科。',            '语文课一般被认为是语言和文化的综合科。',            '物理是研究物质最一般的运动规律和物质基本结构的学科。',            '化学是在原子、分子水平上研究物质的组成、结构、性质、转化及其应用的基础自然科学。'],//页签内容        current:0                                      //当前页签      }    }
      //点击tab切换页签      clickItem(idx){        this.current = idx;      },      //左右滑动切换页签      changeItem(e){        this.current = e.detail.current;      }

方案二:监听touch事件实现左右滑动切换页签

实现思路:

1)监听touchstart、touchmove、touchend事件,捕捉触碰位置

2)通过位移差,判断左右移动,切换当前页current

3)判断页签的和页签内容index是否与current一致,一致则展示

前端代码:touch.vue

      <view class="tabs">        <view v-for="(tab,index) in tabs" :key="index" class="tab">          <text @click="()=>clickItem(index)" :class="index === current?'active':''">            {{tab}}          </text>        </view>      </view>      <view class="swiper" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd">        <view v-for="(item, index) in items" :key="index">          <view class="item" v-if="index === current">            <!-- 页签内容 -->            <text>{{item}}</text>          </view>        </view>      </view>
    data() {      return {        tabs:['数学','语文','物理','化学'],                          //页签        items:['数学是研究数量、结构、变化、空间以及信息等概念的一门学科。',            '语文课一般被认为是语言和文化的综合科。',            '物理是研究物质最一般的运动规律和物质基本结构的学科。',            '化学是在原子、分子水平上研究物质的组成、结构、性质、转化及其应用的基础自然科学。'],//页签内容        current:0,  //当前页        startX: 0, // 触摸开始的X坐标        endX: 0, // 触摸结束的X坐标        threshold: 50 ,// 滑动的阈值,当滑动超过这个值时触发返回        isSwipe: false // 是否进行了滑动      }    }
      //触摸开始      touchStart(e) {        this.startX = e.touches[0].clientX;      },      //触摸移动      touchMove(e) {        this.endX = e.touches[0].clientX;        this.isSwipe = true; //有触发移动      },      //触摸结束      touchEnd(e) {        if(!this.isSwipe){return ;}//没有滑动。防止误碰触发        this.isSwipe = false ;        let distance = this.endX - this.startX;        if (Math.abs(distance) > this.threshold) {        // 判断滑动方向        if (distance > 0 &&this.current>0) {          this.current--;        } else if(this.current<this.items.length-1){          this.current++;        }        }      },            clickItem(idx){        this.current = idx;      }

说明:

    1) 需在touchMove中增加isSwipe处理,防止误碰

    2) 监听touch事件的view需要给足够的高度

监听touch事件,返回上一页

实现思路和方案二类似

前端代码:view.vue

<view class="content" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd">    <image class="logo" src="/static/logo.png"></image>    <view class="text-area">      {{title}}    </view>  </view>
    data() {      return {        title:'详情页',        startX: 0, // 触摸开始的X坐标        endX: 0, // 触摸结束的X坐标        threshold: 50 ,// 滑动的阈值,当滑动超过这个值时触发返回        isSwipe: false // 是否进行了滑动      }    }
      //触摸开始      touchStart(e) {        this.startX = e.touches[0].clientX;      },      //触摸移动      touchMove(e) {        this.endX = e.touches[0].clientX;        this.isSwipe = true;      },      //触摸结束      touchEnd(e) {        if(!this.isSwipe){return ;}        this.isSwipe = false ;        let distance = this.endX - this.startX;        if (Math.abs(distance) > this.threshold) {        // 判断滑动方向        if (distance > 0) {          // 向右滑动          console.log('向右滑动');          // 调用返回操作          uni.navigateBack({          delta: 1          });        }        }      }

说明:

    1) 需在touchMove中增加isSwipe处理,防止误碰

    2) 监听touch事件的view需要给足够的高度

最后介绍下我开发了两款微信小程序:趣味知识宝典萌娃成长,大家可以免费使用,多多支持下!

趣味知识宝典:一款知识学习工具,在趣味中学习知识,包括成语(包含成语拼音、释义、出处、语法、例句、近义词、反义词、故事(寓言故事、历史典故)等,按照寓言、高考(近10年)、时间等多维度分类),科普知识(十万为什么),谜语(猜字谜、脑筋急转弯)等。

萌娃成长:育儿好帮手,奶瓶喂养、母乳亲喂、辅食喂养、补剂用药、换尿不湿、睡眠记录、身高体重、疫苗接种、发育评估(DQ)统统搞定,记录宝宝成长每个瞬间,与宝妈奶爸一起交流育儿经验,与萌娃一起健康快乐成长。

Uniapp中实现页面左右滑动切换的效果可以通过以下步骤进行操作。首先,你可以使用uni-swipe组件来实现滑动效果。该组件可以在左右滑动切换对应的tabs选项。你可以查阅uni-swipe组件的文档来了解具体的使用方法。如果在uni-swipe组件中没有找到满足你需求的功能,你可以考虑自己封装一个功能组件来实现左右滑动切换的效果。通过封装组件,你可以根据自己的需求来自定义滑动切换的效果。最后,你可以在Uniapp的多个平台中进行测试和使用,包括iOS、Android、H5和小程序等平台。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Uniapp中实战实现左右点击滚动功能](https://blog.youkuaiyun.com/qq_29701691/article/details/130224908)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [uniapp实现页面左右滑动切换tab选项](https://blog.youkuaiyun.com/m0_46442996/article/details/116983049)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值