微信小程序实现上下手势滑动切换

效果图

思路

实现一个微信小程序的复合滚动页面,主要通过Swiper组件实现垂直方向的轮播功能,每个轮播项内部使用Scroll-View组件来展示可垂直滚动的长内容,如图片和文本。

代码

<!-- wxml -->
<view class="swiper-container">
  <swiper class="swiper" vertical="true" bindchange="onSwiperChange">
    <swiper-item>
      <!-- 第一个模块的滚动视图 -->
      <scroll-view class="scroll-view" scroll-y="true">
        <!-- 这里是第一个模块的长内容 -->
        <view class="long-content">
		    <image class="img" src="http://img0.baidu.com/it/u=1836749971,2875128665&fm=253&app=138&f=JPEG?w=800&h=1129&#34;     mode=""/>
        </view>
      </scroll-view>
    </swiper-item>
    <swiper-item>
      <!-- 第二个模块的滚动视图 -->
      <scroll-view class="scroll-view" scroll-y="true">
        <!-- 这里是第二个模块的长内容 -->
        <view class="long-content">
          <image class="img" src="https://p9-pc-sign.douyinpic.com/tos-cn-i-0813/8506ad012d1d40ff8940482f2a69e7ca~tplv-dy-aweme-images:q75.webp?biz_tag=aweme_images&from=3213915784&s=PackSourceEnum_AWEME_DETAIL&sc=image&se=false&x-expires=1712764800&x-signature=alMSPxEM4w4svYO29%2BIIpY8Wsog%3D&#34;     mode=""/>
        </view>
      </scroll-view>
    </swiper-item>
  </swiper>
</view>
/* wxss */
.swiper-container {
  width: 100vw;
	height: 100vh;
	background-color: black;
}
.swiper {
  width: 100%;
  height: 100%;
}
.scroll-view {
  width: 100%;
  height: 100%;
}
.long-content {
	height: 100vh;
}
.img {
	width: 100%;
	height: 100%;
}
// js
Page({
  data: {
    current: 0
  },
  onSwiperChange(e) {
    // 当swiper的current改变时,处理切换逻辑
    console.log('当前swiper-item的索引:', e.detail.current);
  }
});

其他

其他方式实现:微信小程序页面上下滚动 - 灰信网(软件开发博客聚合),发现的问题:鼠标中键直接滚动会导致页面切换不彻底(相当于页面A和页面B拼接成一个页面滑动)。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值