微信小程序获取元素距离顶部的距离

id首位不能是数字,否则boundingClientRect()获取到的res会是null。

let id = `#${元素的id值}`
wx.createSelectorQuery().select(id).boundingClientRect(function(rect){
      // 节点的上边界坐标
      let top = rect.top;
      // 节点的下边界坐标
      let bottom = rect.bottom;
}).exec()

你可能感兴趣的:(微信小程序,微信小程序)