实现效果:
解决方法:
1.使用this.$refs.dom获取元素宽度,再进行计算
经过测试: h5正常,模拟器和手机获取dome宽度都是undefined,所以这个不可以使用
let isInteger = this.isInteger(this.$refs.menu.length/2), num = 0
if(isInteger) {
num = this.$refs.menu.length/2
}else{
num = parseInt(this.$refs.menu.length/2) +1
}
this.widthMenu = this.$refs.menu[0].$el.offsetWidth
2.使用uniapp的节点信息uni.createSelectorQuery()
经过测试:h5,模拟器,手机都正常
const query = uni.createSelectorQuery().in(this);
query.select('.menu-list').boundingClientRect(data => {
let isInteger = this.isInteger(this.menuList.length/2), num = 0
if(isInteger) {
num = this.menuList.length/2
}else{
num = parseInt(this.menuList.length/2) +1
}
this.widthMenu = data.width*num*2
}).exec();