浅谈小程序怎么实现列表滚动上下联动效果

 4232

小程序怎么实现列表滚动上下联动效果?下面本篇文章给大家介绍一下微信小程序开发列表滚动上下联动效果的方法,希望对大家有所帮助!


浅谈小程序怎么实现列表滚动上下联动效果


1、背景

最近在做公司的一款小程序,其中有一块的设计的是在列表做上下滚动的是时候,顶部的tab栏跟着一起联动,当点击tab栏的时候,列表数据也跟随联动。

下面是实现的一个效果图:


浅谈小程序怎么实现列表滚动上下联动效果


顶部的头部区域不跟随列表滚动; 头部区域以下属于滚动区域。

2、实现

2.1 原理介绍

这个地方的实现主要借助了微信小程序原生的scroll-view组件。

使用它的 scroll-into-view 属性,可以实现点击顶部的tab栏,将页面滚动到指定的列表位置;

使用 bindscroll 事件,可以知道当前页面滚动的距离,根据滚动的距离做tab栏的切换操作;

2.1 页面布局代码

先说下界面的整体布局,主要分为两部分,头部固定区域 + 可滚动列表区域。

可滚动的列表区域的标题栏当滚动一定的距离后,它也要固定在顶部。

代码实现:

  1. <!--index.wxml-->
  2. <view class="list">
  3.  
  4. <!--顶部固定区域-->
  5. <view style="height: 88rpx;width: 100%;background-color: burlywood;text-align: center;">头部区域</view>
  6.  
  7. <!--可滚动区域-->
  8. <scroll-view scroll-y="true" style="width: 100%; height: {{scrollAreaHeight}}px;" bindscroll="scroll" scroll-into-view="{{scrollToItem}}" scroll-with-animation="true"  scroll-top="{{scrollTop}}">
  9.  
  10.    <!--水平滚动的tab栏-->
  11.   <scroll-view scroll-x="true" style="height: 88rpx;width: 100%;">
  12.   <view class="head-area {{float ? 'head-float' : ''}}" >
  13.     <view class="head-area-item {{curSelectTab === index ? 'head-area-item-select' : ''}}" wx:for="{{appGroupList}}" bindtap="tabClick" data-index="{{index}}">
  14.     {{item.name}}
  15.   </view>
  16.   </view>
  17.  
  18.   </scroll-view>
  19.  
  20. <!--数据列表-->
  21. <view class="list-group" style="height: {{listGroupHeight}}px;">
  22.   <view class="list-group-item" id="v_{{index}}" wx:for="{{appGroupList}}" data-index="{{index}}">
  23.     <view class="group-name">
  24.       {{item.name}}
  25.     </view>
  26.     <view class="group-children" >
  27.       <view wx:for="{{item.children}}" class="group-children-item" style="width: {{itemWidth}}px;">
  28.       <image src="{{item.url}}"></image>
  29.       <view>{{item.name}}</view>
  30.     </view>
  31.     </view>
  32.  
  33.   </view>
  34. </view> 
  35. </scroll-view>
  36.  
  37. </view>

在布局代码中有几个点需要注意:

1、scrollAreaHeight 滚动区域的高度计算。 --- 通过获取当前设备的窗口高度减去顶部固定区域的高度

2、水平tab栏是否置顶。 --- 根据页面的滚动距离来判断,如果滚动距离 大于或者等于 水平tab栏的高度,则置顶;

3、设置数据列表的id="v_{{index}}" id,后续点击tab栏滚动到指定的位置就是根据这个id去实现的。


2.2 样式代码

  1. /**index.wxss**/
  2. .list{
  3.   width: 100%;
  4.   height: 100%;
  5.   display: flex;
  6.   flex-direction: column;
  7. }
  8.  
  9. .head-area{
  10.   display: flex;
  11.   flex-direction: row;
  12.   flex-wrap: nowrap;
  13.   height: 88rpx;
  14.   width: 100%;
  15.   padding: 0 10;
  16. }
  17.  
  18. .head-area-item{
  19.   display: flex;
  20.   height: 88rpx;
  21.   text-align: center;
  22.   width: 150rpx;
  23.   align-items: center;
  24.   justify-content: center;
  25. }
  26.  
  27. .head-area-item-select{
  28.   color: #09bb07;
  29. }
  30.  
  31. image{
  32.   width: 88rpx;
  33.   height: 88rpx;
  34. }
  35.  
  36. .list-group{
  37.   display: flex;
  38.   width: 100%;
  39.   height: 1000%;
  40.   flex-direction: column;
  41. }
  42.  
  43. .list-group-item{
  44.   display: flex;
  45.   width: 100%;
  46.   background-color: #aaa;
  47.   flex-direction: column;
  48. }
  49.  
  50. .group-name{
  51.   height: 88rpx;
  52.   display: flex;
  53.   text-align: center;
  54.   align-items: center;
  55.   margin-left: 20rpx;
  56. }
  57.  
  58. .group-children{
  59.   display: flex;
  60.   flex-direction: row;
  61.   flex-wrap: wrap;
  62.   width: 100%;
  63. }
  64.  
  65. .group-children-item{
  66.   height: 160rpx;
  67.   display: flex;
  68.   flex-direction: column;
  69.   justify-content: center;
  70.   align-items: center;
  71. }
  72.  
  73. .head-float{
  74.   position: fixed;
  75.   top: 88rpx;
  76.   background-color: #ffffff;
  77. }


2.3 逻辑代码

  1. // index.js
  2. Page({
  3.   heightArr: [],
  4.   //记录scroll-view滚动过程中距离顶部的高度
  5.   distance: 0,
  6.   data: {
  7.     appGroupList:[
  8.       {name:"分组01",children:[{"name":"测试0","url":"/images/bluetooth.png"},
  9.       {"name":"测试1","url":"/images/bluetooth.png"},
  10.       {"name":"测试2","url":"/images/bluetooth.png"},
  11.       {"name":"测试3","url":"/images/bluetooth.png"},
  12.       {"name":"测试4","url":"/images/bluetooth.png"},
  13.       {"name":"测试5","url":"/images/bluetooth.png"},
  14.       {"name":"测试6","url":"/images/bluetooth.png"},
  15.       {"name":"测试7","url":"/images/bluetooth.png"}]},
  16.       {name:"分组02",children:[{"name":"测试0","url":"/images/bluetooth.png"},
  17.       {"name":"测试1","url":"/images/bluetooth.png"},
  18.       {"name":"测试2","url":"/images/bluetooth.png"},
  19.       {"name":"测试3","url":"/images/bluetooth.png"},
  20.       {"name":"测试4","url":"/images/bluetooth.png"},
  21.       {"name":"测试5","url":"/images/bluetooth.png"},
  22.       {"name":"测试6","url":"/images/bluetooth.png"},
  23.       {"name":"测试7","url":"/images/bluetooth.png"}]},
  24.       {name:"分组03",children:[{"name":"测试0","url":"/images/bluetooth.png"},
  25.       {"name":"测试1","url":"/images/bluetooth.png"},
  26.       {"name":"测试2","url":"/images/bluetooth.png"},
  27.       {"name":"测试3","url":"/images/bluetooth.png"},
  28.       {"name":"测试4","url":"/images/bluetooth.png"},
  29.       {"name":"测试5","url":"/images/bluetooth.png"},
  30.       {"name":"测试6","url":"/images/bluetooth.png"},
  31.       {"name":"测试7","url":"/images/bluetooth.png"}]},
  32.       {name:"分组04",children:[{"name":"测试0","url":"/images/bluetooth.png"},
  33.       {"name":"测试1","url":"/images/bluetooth.png"},
  34.       {"name":"测试2","url":"/images/bluetooth.png"},
  35.       {"name":"测试3","url":"/images/bluetooth.png"},
  36.       {"name":"测试4","url":"/images/bluetooth.png"},
  37.       {"name":"测试5","url":"/images/bluetooth.png"},
  38.       {"name":"测试6","url":"/images/bluetooth.png"},
  39.       {"name":"测试7","url":"/images/bluetooth.png"}]},
  40.       {name:"分组05",children:[{"name":"测试0","url":"/images/bluetooth.png"},
  41.       {"name":"测试1","url":"/images/bluetooth.png"},
  42.       {"name":"测试2","url":"/images/bluetooth.png"},
  43.       {"name":"测试3","url":"/images/bluetooth.png"},
  44.       {"name":"测试4","url":"/images/bluetooth.png"},
  45.       {"name":"测试5","url":"/images/bluetooth.png"},
  46.       {"name":"测试6","url":"/images/bluetooth.png"},
  47.       {"name":"测试7","url":"/images/bluetooth.png"}]},
  48.     ],
  49.     itemWidth: wx.getSystemInfoSync().windowWidth / 4,
  50.     scrollAreaHeight:wx.getSystemInfoSync().windowHeight - 44,
  51.     float:false,
  52.     curSelectTab:0,
  53.     scrollToItem:null,
  54.     scrollTop: 0, //到顶部的距离
  55.     listGroupHeight:0,
  56.   },
  57.  
  58.   onReady: function () {
  59.     this.cacluItemHeight();
  60.   },
  61.  
  62.   scroll:function(e){
  63.     console.log("scroll:",e);
  64.     if(e.detail.scrollTop>=44){
  65.       this.setData({
  66.         float : true
  67.       })
  68.     } else if(e.detail.scrollTop<44) {
  69.       this.setData({
  70.         float : false
  71.       })
  72.     }
  73.     let scrollTop = e.detail.scrollTop;
  74.     let current = this.data.curSelectTab;
  75.     if (scrollTop >= this.distance) {
  76.       //页面向上滑动
  77.       //列表当前可视区域最底部到顶部的距离 超过 当前列表选中项距顶部的高度(且没有下标越界),则更新tab栏
  78.       if (current + 1 < this.heightArr.length && scrollTop >= this.heightArr[current]) {
  79.         this.setData({
  80.           curSelectTab: current + 1
  81.         })
  82.       }
  83.     } else { 
  84.       //页面向下滑动
  85.       //如果列表当前可视区域最顶部到顶部的距离 小于 当前列表选中的项距顶部的高度,则切换tab栏的选中项
  86.       if (current - 1 >= 0 && scrollTop < this.heightArr[current - 1]) {
  87.         this.setData({
  88.           curSelectTab: current - 1
  89.         })
  90.       }
  91.     }
  92.     //更新到顶部的距离
  93.     this.distance = scrollTop;
  94.   },
  95.  
  96.   tabClick(e){
  97.     this.setData({
  98.       curSelectTab: e.currentTarget.dataset.index,
  99.       scrollToItem: "v_"+e.currentTarget.dataset.index
  100.     })
  101.   },
  102.  
  103.   //计算每一个item高度
  104.   cacluItemHeight() {
  105.     let that = this;
  106.     this.heightArr = [];
  107.     let h = 0;
  108.     const query = wx.createSelectorQuery();
  109.     query.selectAll('.list-group-item').boundingClientRect()
  110.     query.exec(function(res) {
  111.       res[0].forEach((item) => {
  112.         h += item.height;
  113.         that.heightArr.push(h);
  114.       })
  115.       console.log(that.heightArr);
  116.       that.setData({
  117.         listGroupHeight: that.heightArr[that.heightArr.length - 1 ]
  118.       })
  119.     })
  120.   },
  121. })

在逻辑代码中最主要的有两个地方:

1、cacluItemHeight 计算列表中item的高度数组,并将最终计算的结果保存在 heightArr数组中。

heightArr数组中的每一项的值是在前一项的基础之上进行累加。

2、scroll 中判断当前的滚动方向,根据滚动判断当前的方向,然后根据滚动的距离设置当前选择的tab。

好了,就这么多,基于以上的内容基本可以实现想要的滚动联动、切换tab联动效果。


本文网址:https://www.zztuku.com/index.php/detail-10455.html
站长图库 - 浅谈小程序怎么实现列表滚动上下联动效果
申明:本文转载于《掘金社区》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐