小程序获取元素屏幕中的位置

 wxml代码

<view class="titleMsgs" id='goodsdetali'>
    <text>商品详情</text>
</view>

 JS代码

        const scrool = wx.createSelectorQuery()
        scrool.select('#goodsdetali').boundingClientRect()
        scrool.selectViewport().scrollOffset()
        scrool.exec( (res)=> {
            console.log(res);
          res[0].top // #goodsdetali节点的上边界坐标
          res[1].scrollTop // 显示区域的竖直滚动位置
        })

log打印和页面效果

 

 

猜你喜欢

转载自blog.csdn.net/weixin_69370222/article/details/130287738