微信小程序scroll左右联动

这篇文章描述了一个微信小程序页面的实现,其中左侧的scroll-view展示商品分类,右侧则展示商品列表。两个scroll-view可以互相联动,当左侧分类滚动时,右侧列表会自动滚动到相应分类的商品,反之亦然。通过计算和存储右侧商品项的高度,动态调整滚动位置,实现了平滑的交互体验。
摘要由CSDN通过智能技术生成

在商品列表页面里,左边是商品分类,右边是商品列表二个scroll-view 互相联动,

var heightList = [];  //存放右侧分类的高度累加数组
var distanceToTop = 0;  //记录scroll-view滚动过程中距离顶部的高度
var scrollRightCount = 0;
var scrollRightItemHeight = 30;   //右边item高度 滚动右边时累加左边值
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classify: [
    ],
    windowHeight: '',//可视区域高度
    curClassIndex: 0,//当前选中的分类
    navToRight: 'nav0',//右边scroll跳转至第n个scroll-item
    scrollRight: 0,
  },
  onClassChange(event) {
    let { detail } = event
    let index = event.detail
    console.log("index=", index, "detail=", detail);
    this.setData({
      curClassIndex: index,
      navToRight: `nav${index}`
    })
  },

  onLeftScroll(e) {
    scrollRightCount = e.detail.scrollTop; //滚动位置
  },

  // 滚动右边scroll
  onRightScroll(e) {
    if (heightList.length == 0) return;
    let scrollTop = e.detail.scrollTop; //滚动位置
    let current = this.data.curClassIndex;
    if (scrollTop > distanceToTop) {
      if ((current + 1 < heightList.length) && (scrollTop >= heightList[current])) {
        this.setData({
          curClassIndex: current + 1
        })
      }
    } else {
      if ((current - 1 >= 0) && (scrollTop < heightList[current - 1])) {
        this.setData({
          curClassIndex: current - 1
        })
      }
    }
    if (current > (heightList.length / 3) && scrollRightCount < this.data.windowHeight / 3) {
      scrollRightCount = scrollRightCount + scrollRightItemHeight
      this.setData({
        scrollRight: scrollRightCount
      });
    }
    else if (scrollRightCount >= scrollRightItemHeight) {
      scrollRightCount = scrollRightCount - scrollRightItemHeight
      if (scrollRightCount <= 0)
        scrollRightCount = 0;
      this.setData({
        scrollRight: scrollRightCount
      });
    }
    // 更新顶部的距离
    distanceToTop = scrollTop;
  },


  //获取右边每个item高度数组 
  getRightItemHeight() {
    heightList = [];
    let tmpH = 0; 
    // 获取元素信息 返回一个 SelectorQuery 对象实例
    const query = wx.createSelectorQuery()
    query.selectAll('.scroll-right-item').boundingClientRect()
    query.exec(function (res) {
      res[0].forEach((item) => {
        tmpH += item.height;
        heightList.push(Math.floor(tmpH));
      })
      console.log(heightList);
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var systemInfo = wx.getSystemInfoSync();
    this.setData({
      headHeight: systemInfo.windowHeight - 100,
      windowHeight: systemInfo.windowHeight
    });

    var classify = [];
    for (var i = 0; i < 50; i++) {
      classify.push({
        id: "class" + i,
        name: "标签:" + i,
        selected: false
      });
    }
    this.setData({
      classify: classify
    });
    this.getRightItemHeight();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序实现左右列表联动的代码可参考以下示例: wxml部分: ```html <view class="container"> <view class="left"> <scroll-view scroll-y="{{true}}" scroll-into-view="{{scrollIntoView}}" scroll-with-animation="{{true}}" class="scroll-view-left"> <block wx:for="{{leftList}}" wx:key="index"> <view class="item {{leftIndex == index ? 'active' : ''}}" data-index="{{index}}" bindtap="handleLeftItemTap">{{item}}</view> </block> </scroll-view> </view> <view class="right"> <scroll-view scroll-y="{{true}}" scroll-into-view="{{scrollIntoView}}" scroll-with-animation="{{true}}" class="scroll-view-right"> <block wx:for="{{rightList[leftIndex].children}}" wx:key="index"> <view class="item" data-index="{{index}}" bindtap="handleRightItemTap">{{item}}</view> </block> </scroll-view> </view> </view> ``` js部分: ```javascript Page({ data: { leftIndex: 0, scrollIntoView: '', leftList: ['分类1', '分类2', '分类3'], rightList: [ { name: '分类1', children: ['子分类1', '子分类2', '子分类3'] }, { name: '分类2', children: ['子分类4', '子分类5', '子分类6'] }, { name: '分类3', children: ['子分类7', '子分类8', '子分类9'] } ] }, handleLeftItemTap: function(e) { const index = e.currentTarget.dataset.index; this.setData({ leftIndex: index, scrollIntoView: `right-item-${index}-0` }); }, handleRightItemTap: function(e) { const index = e.currentTarget.dataset.index; wx.showToast({ title: `点击了${this.data.rightList[this.data.leftIndex].children[index]}`, icon: 'none' }); } }) ``` 其中,`leftList` 为左侧列表,`rightList` 为右侧列表,`leftIndex` 为左侧选中的索引,`scrollIntoView` 为滚动到的位置。 在左侧列表中点击某个分类时,将 `leftIndex` 更新为当前索引并将 `scrollIntoView` 更新为对应右侧列表中第一个子分类的 `id`,从而实现左侧列表与右侧列表的联动。 右侧列表中点击某个子分类时,可以触发相应的逻辑操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值