const query = wx.createSelectorQuery()获取const query = wx.createSelectorQuery().in(this)获取setTimeou()
并不能获取到dom节点,获取为null获取没出现什么东西
const query = wx.createSelectorQuery()获取const query = wx.createSelectorQuery().in(this)获取setTimeou()
并不能获取到dom节点,获取为null获取没出现什么东西
1 个回答
我封装了一个函数,小程序和web端通用,异步调用,传入dom节点,比如this.$refs.***或者document.getElementById('***'),获取dom的宽高left和top,如果需要更多数据,可以参考这个,往外传递。
export async function getDomPosition(dom) { // console.log(process.env.isMiniprogram) if (process.env.isMiniprogram) { return new Promise((resolve) => { dom.$$getBoundingClientRect().then(rect => { // console.log('rect') // console.log(rect) resolve({ x: rect.left, y: rect.top, width: rect.width, height: rect.height }) }) }) } else { return new Promise((resolve) => { resolve({ x: dom.offsetLeft, y: dom.offsetTop, width: dom.offsetWidth, height: dom.offsetHeight }) }) } }