微信小程序实现新闻轮播、禁止手动竖向滑动

效果

在这里插入图片描述

图片

WXML

    在 swiper-item 上加入 catchtouchmove 方法,截获竖向滑动

<view class="container">
  <view class="news">
    <image class="icon-news" src="/images/news.png"></image>
    <swiper class="swiper" autoplay circular vertical interval="3000" easing-function="easeInOutCubic">
      <swiper-item class="item"
                   wx:for="{{newsList}}"
                   wx:key="index"
                   data-id="{{item.id}}"
                   catchtouchmove="catchTouchMove" 
                   bindtap="toNewsDetail">
        <view class="txt">{{item.desc}}</view>
      </swiper-item>
    </swiper>
  </view>
</view>

JS

Page({
  data: {
  	newsList: [
      { id: 1, desc: '我是第一条新闻哦我是第一条新闻哦'},
      { id: 2, desc: '我是第二条新闻哦我是第二条新闻哦我是第二条新闻哦'},
      { id: 3, desc: '我是第三条新闻哦我是第三条新闻哦'},
      { id: 4, desc: '我是第四条新闻哦我是第四条新闻哦我是第四条新闻哦'},
      { id: 5, desc: '我是第五条新闻哦我是第五条新闻哦'},
      { id: 6, desc: '我是第六条新闻哦'}
    ]
  },
  onLoad(options) {
    
  },
  onShow() {

  },
  // 禁止手动竖向滑动 swiper
  catchTouchMove() {
    return false
  },
  // 跳转到新闻详情
  toNewsDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.showToast({
      title: '第'+id+'条新闻',
      icon: 'none',
      duration: 1600
    })
  }
})

WXSS

page {
  background: #fff;
}
.container {
  width: 100%;
}
.news {
  width: 100%;
  padding: 18rpx 30rpx;
  box-sizing: border-box;
  display: flex;
}
.news .icon-news {
  flex: 0 0 44rpx;
  height: 44rpx;
  padding-right: 12rpx;
  border-right: 2rpx solid #ebebeb;
  margin-right: 12rpx;
}
.news .swiper {
  flex: 1;
  height: 40rpx;
}
.news .swiper .item {
  font-size: 26rpx;
  display: flex;
  align-items: center;
}
.news .swiper .item .txt {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半度℃温热

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值