微信小程序swiper禁止用户手动滑动

本文介绍如何在微信小程序中使用catchtouchmove事件禁用Swiper组件的滑动功能,同时保持点击按钮的交互性。通过在Swiper-item上绑定catchtouchmove事件并返回false,可以有效阻止用户的手动滑动,而不会影响到点击事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用 catchtouchmove 事件来截获用户手动滑动事件,这样既解决了禁用用户手动滑动,有解决了有点击按钮不影响使用

wxml

<swiper class='yaohe' vertical='true' circular='true' autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
    <block wx:for='{{yaohe}}'>
      <swiper-item catchtouchmove='catchTouchMove'>   <!--重点这里-->
        <block wx:for='{{item}}'>
          <view class='yaohe_item'>
            <view class='yaohe_img'>
              <image src='{{item.goods_icon}}'></image>
            </view>
            <view class='yaohe_right'>
              <view class='yaohe_text'>{{item.goods_feature}}</view>
              <view class='boss_price'>
                <view class='boss'>{{item.shop_name}}</view>
                <view class='price' bindtap='goGoodsDetail' data-shop_goods_id='{{item.shop_goods_id}}'>¥{{item.goods_price}}</view>
              </view>
            </view>
          </view>
        </block>
      </swiper-item>
    </block>
  </swiper>

重点js

// 截获竖向滑动
  catchTouchMove:function(res){
    return false
  }
微信小程序中的 `swiper` 组件默认是横向轮播的,但如果想要实现纵向轮播的效果,可以通过设置其属性来调整方向。 ### 实现步骤 1. **修改 swiper 的 orientation 属性** 在 `swiper` 标签中添加 `vertical="true"` 来将滑动方向改为竖直方向,默认值为水平方向 (`false`)。 2. **自定义样式** 可能需要对每个 `swiper-item` 设置高度或其他样式以便适应页面需求。例如: ```html <swiper vertical="true" circular="{{true}}" interval="{{3000}}"> <swiper-item> <view class="item">内容一</view> </swiper-item> <swiper-item> <view class="item">内容二</view> </swiper-item> <swiper-item> <view class="item">内容三</view> </swiper-item> </swiper> ``` 3. **绑定动画效果和数据源** 如果有动态的数据展示,可以结合 WXML 和 JS 文件中的数据渲染逻辑,并通过控制 `current` 值实现手动切换或自动播放等功能。 4. **优化用户体验** - 添加滚动指示点(dots),用户可以更直观了解当前处于哪一页; - 设定合适的定时器时间间隔 (interval)、延迟时间和缓动函数(easing-function),让过渡更为流畅自然; 示例代码片段如下: ```wxml <swiper indicator-dots="{{true}}" autoplay="{{autoplay}}" circular="{{circular}}" duration="500" current="{{currentIndex}}" bindchange="onSwiperChange" vertical="{{true}}"> <block wx:for="{{items}}" wx:key="index"> <swiper-item> <!-- 您的内容 --> <text>{{item}}</text> </swiper-item> </block> </swiper> ``` ```js Page({ data:{ currentIndex : 0, items:["第一页","第二页","第三页"], }, onSwiperChange:function(event){ this.setData({currentIndex:event.detail.current}) } }); ``` --- #### 注意事项: - 确保每张卡片的高度一致以避免布局错乱。 - 调整好各项参数如是否循环(circular)及自动播放(autoplay)等选项满足实际场景需求。 - 测试不同设备上显示情况保证兼容性和美观度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值