uniapp实现左右滑动布局思路共享

一、问题背景:

        开发中,左右滑动布局的应用非常常见。在此分享下我对左右滑动布局实现的思路。

二、思路展示

        直接展示例图:

        如上图,我画了两个草稿图以表达我的做法。

1、滑动布局中一个滑动item中横向显示4个item内容

2、滑动布局中一个滑动item中纵向显示4个item内容

数据:我的滑动布局数据在一个数组中(如:[item1,item2,item3,item4......])

数组方法:我是将数组中的所有item项for一遍,拿到每个要显示的内容(item)。

        然后将其切割成4个一组的数组,再push进一个空数组中。然后就成了如下的数组:

        [ [item1,item2,item3,item4][item1,item2,item3] ]

数据渲染:再到渲染逻辑处,swiper-item循环最外层的数组,拿到里面有多少个小数组(多少个              小 数组就是多少个滑动页,如:[ [item1,item2,item3,item4][item1,item2,item3] ]           大数组中包含2个小数组就是2个滑动页)。

        然后再在swiper-item内进行循环小数组,拿到小数组的每一个item(需要显示的内容,如:[ [item1,item2,item3,item4],[item1,item2,item3] ])。

实际效果:实际渲染出来的图就是如此了。

三、总结

        uniapp的左右滑动布局,可以用swiper完成,但每个滑动页(swiper-item)它默认只渲染1个item。我们想办法让swiper-item渲染时还存在数组,再在swiper-item内渲染数组里的每一项即可。

知识点:数据请求、数组处理(splice、push等)、for循环嵌套

以上思路供各位参考,相关知识点可查看本人主页。如有大神,望赐教。

  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
实现左右滑动切换页面,可以使用uni-app提供的swiper组件,具体步骤如下: 1. 在页面中添加swiper组件: ```html <swiper :current="current" @change="swiperChange" :duration="500"> <swiper-item> <!-- 第1个页面内容 --> </swiper-item> <swiper-item> <!-- 第2个页面内容 --> </swiper-item> <swiper-item> <!-- 第3个页面内容 --> </swiper-item> </swiper> ``` 2. 在data中定义current变量,默认值为0表示当前显示第1个页面: ```js data() { return { current: 0 } } ``` 3. 在methods中添加swiperChange方法,监听swiper组件的change事件,更新current变量的值: ```js methods: { swiperChange(event) { this.current = event.detail.current; } } ``` 4. 在样式中设置swiper的宽度和高度,以及swiper-item的宽度和高度为100%: ```css swiper { width: 100%; height: 100%; } swiper-item { width: 100%; height: 100%; } ``` 5. 最后,使用touch事件监听用户的滑动操作,根据滑动的方向更新current变量的值,实现页面切换: ```js methods: { // 左右滑动切换页面 touchstart(event) { this.touchStartX = event.touches[0].pageX; }, touchend(event) { this.touchEndX = event.changedTouches[0].pageX; if (this.touchEndX - this.touchStartX > 50 && this.current > 0) { this.current--; } else if (this.touchEndX - this.touchStartX < -50 && this.current < 2) { this.current++; } } } ``` 完整代码如下: ```html <template> <swiper :current="current" @change="swiperChange" :duration="500"> <swiper-item> <!-- 第1个页面内容 --> </swiper-item> <swiper-item> <!-- 第2个页面内容 --> </swiper-item> <swiper-item> <!-- 第3个页面内容 --> </swiper-item> </swiper> </template> <script> export default { data() { return { current: 0, touchStartX: 0, touchEndX: 0 } }, methods: { swiperChange(event) { this.current = event.detail.current; }, touchstart(event) { this.touchStartX = event.touches[0].pageX; }, touchend(event) { this.touchEndX = event.changedTouches[0].pageX; if (this.touchEndX - this.touchStartX > 50 && this.current > 0) { this.current--; } else if (this.touchEndX - this.touchStartX < -50 && this.current < 2) { this.current++; } } } } </script> <style> swiper { width: 100%; height: 100%; } swiper-item { width: 100%; height: 100%; } </style> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

乡下小菜鸟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值