微信小程序scroll左右联动

const { get } = require("../../utils/request")
//存放右侧分类的高度累加数组
var heightList = [];
//记录scroll-view滚动过程中距离顶部的高度
var distanceToTop = 0;

Page({
    /**
     * 页面的初始数据
     */
    data: {
        scrollList: [],//分类数据
        windowHeight: '',//可视区域高度
        curIndex: 0,//当前选中的分类
        loading: true,//显示骨架屏
        navTo: 'nav0',//右边scroll跳转至第n个scroll-item
    },
    // 获取分类数据
    getScrollList() {
        get('/api/public/v1/categories').then(res => {
            // console.log(res);
            this.setData({
                scrollList: res.data.message
            }, () => {
                this.setData({
                    loading: false
                })
                this.getProListHeight()
            })
        })
    },
    // 点击左边scroll
    leftScroll(e) {
        let { index } = e.currentTarget.dataset
        console.log(index);
        this.setData({
            curIndex: index,
            navTo: `nav${index}`
        })
    },
    getProListHeight() {
        let tmpHeightList = [];
        let tmpH = 0; //临时存放每个分类的高度
        // 获取元素信息 返回一个 SelectorQuery 对象实例
        const query = wx.createSelectorQuery()
        query.selectAll('.scroll-right-item').boundingClientRect()
        query.exec(function (res) {
            // console.log(res);
            res[0].forEach((item) => {
                tmpH += item.height;
                // Math.floor()向下取整 【根据个人需要修改】
                tmpHeightList.push(Math.floor(tmpH));
            })
            heightList = tmpHeightList; //[386, 658, 930, 1202, ...]
            // console.log(heightList);
        })
    },
    // 滚动右边scroll
    scroll(e) {
        if (heightList.length == 0) return;

        let scrollTop = e.detail.scrollTop; //滚动位置
        let current = this.data.curIndex;
        // console.log('scrollTop======>', scrollTop);
        if (scrollTop > distanceToTop) {
            // 超过可视区域的一半
            if ((current + 1 < heightList.length) && (scrollTop >= heightList[current] - this.data.windowHeight / 2)) {
                this.setData({
                    curIndex: current + 1
                })
            }
        } else {
            //如果右侧 可视区域的竖直滚动位置 小于 当前列表选中的项距顶部一半的高度,则更新左侧选中项
            if ((current - 1 >= 0) && (scrollTop < heightList[current - 1] - this.data.windowHeight / 2)) {
                this.setData({
                    curIndex: current - 1
                })
            }
        }
        // 更新顶部的距离
        distanceToTop = scrollTop;
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let self = this
        this.getScrollList()
        wx.getSystemInfo({
            success(res) {
                self.setData({
                    windowHeight: res.windowHeight,
                    dpi: 750 / res.screenWidth
                })
            }
        })

    }
})
  • 1
    点赞
  • 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、付费专栏及课程。

余额充值