非组件页面:
wx.createSelectorQuery().in(this).select('.scroll-list').boundingClientRect((rect)=>{ console.log(rect) this.setData({scrollViewWidth:Math.round(rect.width)}) // scroll-view元素长度 }).exec()
组件页面:
1. 在组件在视图层布局完成后执行,即要在ready函数中操作 2. wx.createSelectorQuery().in(this).select('#the-id') 3. 官方文档:https://developers.weixin.qq.com/miniprogram/dev/api/wxml/SelectorQuery.in.html
// 组件生命周期函数-在组件布局完成后执行 ready () { wx.createSelectorQuery().in(this).select('.scroll-list').boundingClientRect((rect)=>{ console.log(rect) this.setData({scrollViewWidth:Math.round(rect.width)}) // scroll-view元素长度 }).exec() },