微信小程序开发--列表滚动上下联动效果

简介: 列表滚动

当点击tab栏的时候,列表数据也跟随联动。

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

网络异常,图片无法展示
|

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

2、实现

2.1 原理介绍

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

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

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

2.1 页面布局代码

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

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

代码实现:

<!--index.wxml--><viewclass="list"><!--顶部固定区域--><viewstyle="height: 88rpx;width: 100%;background-color: burlywood;text-align: center;">头部区域</view><!--可滚动区域--><scroll-viewscroll-y="true"style="width: 100%; height: {{scrollAreaHeight}}px;"bindscroll="scroll"scroll-into-view="{{scrollToItem}}"scroll-with-animation="true"scroll-top="{{scrollTop}}"><!--水平滚动的tab栏--><scroll-viewscroll-x="true"style="height: 88rpx;width: 100%;"><viewclass="head-area {{float ? 'head-float' : ''}}"><viewclass="head-area-item {{curSelectTab === index ? 'head-area-item-select' : ''}}"wx:for="{{appGroupList}}"bindtap="tabClick"data-index="{{index}}">    {{item.name}}
</view></view></scroll-view><!--数据列表--><viewclass="list-group"style="height: {{listGroupHeight}}px;"><viewclass="list-group-item"id="v_{{index}}"wx:for="{{appGroupList}}"data-index="{{index}}"><viewclass="group-name">      {{item.name}}
</view><viewclass="group-children"><viewwx:for="{{item.children}}"class="group-children-item"style="width: {{itemWidth}}px;"><imagesrc="{{item.url}}"></image><view>{{item.name}}</view></view></view></view></view></scroll-view></view>

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

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

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

/**index.wxss**/
.list{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.head-area{
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  height: 88rpx;
  width: 100%;
  padding: 0 10;
}
.head-area-item{
  display: flex;
  height: 88rpx;
  text-align: center;
  width: 150rpx;
  align-items: center;
  justify-content: center;
}
.head-area-item-select{
  color: #09bb07;
}
image{
  width: 88rpx;
  height: 88rpx;
}
.list-group{
  display: flex;
  width: 100%;
  height: 1000%;
  flex-direction: column;
}
.list-group-item{
  display: flex;
  width: 100%;
  background-color: #aaa;
  flex-direction: column;
}
.group-name{
  height: 88rpx;
  display: flex;
  text-align: center;
  align-items: center;
  margin-left: 20rpx;
}
.group-children{
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 100%;
}
.group-children-item{
  height: 160rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.head-float{
  position: fixed;
  top: 88rpx;
  background-color: #ffffff;
}

js逻辑层

// index.js
Page({
  heightArr: [],
  //记录scroll-view滚动过程中距离顶部的高度
  distance: 0,
  data: {
    appGroupList:[
      {name:"分组01",children:[{"name":"测试0","url":"/images/bluetooth.png"},
      {"name":"测试1","url":"/images/bluetooth.png"},
      {"name":"测试2","url":"/images/bluetooth.png"},
      {"name":"测试3","url":"/images/bluetooth.png"},
      {"name":"测试4","url":"/images/bluetooth.png"},
      {"name":"测试5","url":"/images/bluetooth.png"},
      {"name":"测试6","url":"/images/bluetooth.png"},
      {"name":"测试7","url":"/images/bluetooth.png"}]},
      {name:"分组02",children:[{"name":"测试0","url":"/images/bluetooth.png"},
      {"name":"测试1","url":"/images/bluetooth.png"},
      {"name":"测试2","url":"/images/bluetooth.png"},
      {"name":"测试3","url":"/images/bluetooth.png"},
      {"name":"测试4","url":"/images/bluetooth.png"},
      {"name":"测试5","url":"/images/bluetooth.png"},
      {"name":"测试6","url":"/images/bluetooth.png"},
      {"name":"测试7","url":"/images/bluetooth.png"}]},
      {name:"分组03",children:[{"name":"测试0","url":"/images/bluetooth.png"},
      {"name":"测试1","url":"/images/bluetooth.png"},
      {"name":"测试2","url":"/images/bluetooth.png"},
      {"name":"测试3","url":"/images/bluetooth.png"},
      {"name":"测试4","url":"/images/bluetooth.png"},
      {"name":"测试5","url":"/images/bluetooth.png"},
      {"name":"测试6","url":"/images/bluetooth.png"},
      {"name":"测试7","url":"/images/bluetooth.png"}]},
      {name:"分组04",children:[{"name":"测试0","url":"/images/bluetooth.png"},
      {"name":"测试1","url":"/images/bluetooth.png"},
      {"name":"测试2","url":"/images/bluetooth.png"},
      {"name":"测试3","url":"/images/bluetooth.png"},
      {"name":"测试4","url":"/images/bluetooth.png"},
      {"name":"测试5","url":"/images/bluetooth.png"},
      {"name":"测试6","url":"/images/bluetooth.png"},
      {"name":"测试7","url":"/images/bluetooth.png"}]},
      {name:"分组05",children:[{"name":"测试0","url":"/images/bluetooth.png"},
      {"name":"测试1","url":"/images/bluetooth.png"},
      {"name":"测试2","url":"/images/bluetooth.png"},
      {"name":"测试3","url":"/images/bluetooth.png"},
      {"name":"测试4","url":"/images/bluetooth.png"},
      {"name":"测试5","url":"/images/bluetooth.png"},
      {"name":"测试6","url":"/images/bluetooth.png"},
      {"name":"测试7","url":"/images/bluetooth.png"}]},
    ],
    itemWidth: wx.getSystemInfoSync().windowWidth / 4,
    scrollAreaHeight:wx.getSystemInfoSync().windowHeight - 44,
    float:false,
    curSelectTab:0,
    scrollToItem:null,
    scrollTop: 0, //到顶部的距离
    listGroupHeight:0,
  },
  onReady: function () {
    this.cacluItemHeight();
  },
  scroll:function(e){
    console.log("scroll:",e);
    if(e.detail.scrollTop>=44){
      this.setData({
        float : true
      })
    } else if(e.detail.scrollTop<44){this.setData({float:false})}letscrollTop = e.detail.scrollTop;letcurrent = this.data.curSelectTab;if(scrollTop>= this.distance) {
      //页面向上滑动
      //列表当前可视区域最底部到顶部的距离 超过 当前列表选中项距顶部的高度(且没有下标越界),则更新tab栏
      if (current + 1 <this.heightArr.length&&scrollTop>= this.heightArr[current]) {
        this.setData({
          curSelectTab: current + 1
        })
      }
    } else { 
      //页面向下滑动
      //如果列表当前可视区域最顶部到顶部的距离 小于 当前列表选中的项距顶部的高度,则切换tab栏的选中项
      if (current - 1 >= 0 && scrollTop <this.heightArr[current-1]){this.setData({curSelectTab:current-1})}}//更新到顶部的距离this.distance = scrollTop;},tabClick(e){this.setData({curSelectTab:e.currentTarget.dataset.index,scrollToItem:"v_"+e.currentTarget.dataset.index})},//计算每一个item高度cacluItemHeight(){letthat = this;this.heightArr = [];leth = 0;constquery = wx.createSelectorQuery();query.selectAll('.list-group-item').boundingClientRect()query.exec(function(res){res[0].forEach((item) => {
        h += item.height;
        that.heightArr.push(h);
      })
      console.log(that.heightArr);
      that.setData({
        listGroupHeight: that.heightArr[that.heightArr.length - 1 ]
      })
    })
  },
})

以上内容仅供参考

相关文章
|
1天前
|
小程序
微信小程序开发---购物商城系统。【详细业务需求描述+实现效果】
这篇文章详细介绍了作者开发的微信小程序购物商城系统,包括功能列表、项目结构、具体页面展示和部分源码,涵盖了从首页、商品分类、商品列表、商品详情、购物车、支付、订单查询、个人中心到商品收藏和意见反馈等多个页面的实现效果和业务需求描述。
微信小程序开发---购物商城系统。【详细业务需求描述+实现效果】
|
1天前
|
小程序
关于我花了一个星期学习微信小程序开发、并且成功开发出一个商城项目系统的心得体会
这篇文章是作者关于学习微信小程序开发并在一周内成功开发出一个商城项目系统的心得体会,分享了学习基础知识、实战项目开发的过程,以及小程序开发的易上手性和开发周期的简短。
关于我花了一个星期学习微信小程序开发、并且成功开发出一个商城项目系统的心得体会
|
3天前
|
小程序 前端开发 持续交付
小程序全栈开发中的CI/CD流程与自动化部署是一种高效的开发模式。
本文探讨小程序全栈开发中的CI/CD流程与自动化部署,介绍持续集成与部署的概念,包括自动化构建、测试、代码审查及部署实践。通过提高代码质量、迭代速度及团队协作效率,确保小程序稳定运行与良好用户体验。
14 2
|
3天前
|
小程序 前端开发 API
微信小程序全栈开发中的异常处理与日志记录是一个重要而复杂的问题。
微信小程序作为业务拓展的新渠道,其全栈开发涉及前端与后端的紧密配合。本文聚焦小程序开发中的异常处理与日志记录,从前端的网络、页面跳转等异常,到后端的数据库、API调用等问题,详述了如何利用try-catch及日志框架进行有效管理。同时强调了集中式日志管理的重要性,并提醒开发者注意安全性、性能及团队协作等方面,以构建稳定可靠的小程序应用。
13 1
|
3天前
|
小程序 前端开发 API
微信小程序全栈开发中的多端适配与响应式布局是一种高效的开发模式。
探讨小程序全栈开发中的多端适配与响应式布局,旨在实现统一的用户体验。多端适配包括平台和设备适配,确保小程序能在不同环境稳定运行。响应式布局利用媒体查询和弹性布局技术,使界面适应各种屏幕尺寸。实践中需考虑兼容性、性能优化及用户体验,借助跨平台框架如Taro或uni-app可简化开发流程,提升效率。
13 1
|
3天前
|
小程序 前端开发 数据安全/隐私保护
微信小程序全栈开发中的身份认证与授权机制是一个重要而复杂的问题。
微信小程序作为业务拓展的新渠道,其全栈开发中的身份认证与授权机制至关重要。本文概览了身份认证方法,包括手机号码验证、微信及第三方登录;并介绍了授权机制,如角色权限控制、ACL和OAuth 2.0。通过微信登录获取用户信息,利用第三方登录集成其他平台,以及实施角色权限控制和ACL,开发者能有效保障小程序的安全性和提供良好用户体验。此外,还强调了在实现过程中需注重安全性、用户体验和合规性。
7 0
|
9天前
|
小程序 JavaScript 前端开发
微信小程序开发必备前置知识:基本代码构成与语法
【8月更文挑战第8天】微信小程序的基本代码构成与语法
17 0
微信小程序开发必备前置知识:基本代码构成与语法
|
17天前
|
人工智能 搜索推荐 安全
从零到一:微信机器人开发的实战心得
从零到一:微信机器人开发的实战心得
58 2
|
22天前
|
移动开发 开发框架 前端开发
微信门户开发框架-使用指导说明书(2)--基于框架的开发过程
微信门户开发框架-使用指导说明书(2)--基于框架的开发过程
|
22天前
|
存储 开发框架 小程序
微信门户开发框架-使用指导说明书
微信门户开发框架-使用指导说明书