页面滚动到指定dom的位置

    // 1.使用wx.createSelectorQuery()查询到需要滚动到的元素位置
            wx.createSelectorQuery().select('#bb4').boundingClientRect(res => {
                console.log(res);
                // 到这里,我们可以从res中读到class为bb4的top,即离顶部的距离(px)
                // 2使用wx.pageScrollTo()将页面滚动到对应位置
                wx.pageScrollTo({
                    scrollTop: res.top, // 滚动到的位置(距离顶部 px)
                    duration: 500 //滚动所需时间 如果不需要滚动过渡动画,设为0(ms)
                })
            }).exec() 

  

posted @ 2022-08-16 11:28  见贤思“奇”  阅读(353)  评论(0)    收藏  举报