有服务器有域名怎么做网站,济宁优化网络公司,浙江建设职业技术学院提前招网站,岚县网站建设在uniapp开发中#xff0c;需要获取到dom的信息#xff0c;需要用到uniapp的指定方式 uni.createSelectorQuery()#xff0c;但是每次需要用到的时候都需要很长一段的繁琐代码#xff0c;本篇文章将呈现获取dom信息方法封装#xff0c;话不多说#xff0c;上菜#xff1…在uniapp开发中需要获取到dom的信息需要用到uniapp的指定方式 uni.createSelectorQuery()但是每次需要用到的时候都需要很长一段的繁琐代码本篇文章将呈现获取dom信息方法封装话不多说上菜 getDomInfo(id, that) {return new Promise((resolve, reject) {if (!id) return reject(id/类名 不能为空)if (!that) return reject(this指向不能为空)const query uni.createSelectorQuery().in(that);query.select(id).boundingClientRect(data {// console.log(节点离页面顶部的距离为 data.height);resolve(data || {})}).exec();})
}, 因为uni.createSelectorQuery()是一个异步方法所以封装的时候将其封装在一个Promise里面其方法需要传入两个参数元素id/类名当前页面的this下面举例使用 templateview classpageview classdom/view/view
/templatescript
export default {async mounted() {let domInfo await this.$util.getDomInfo(.dom, this);},
}
/scriptstyle langscss scoped
.dom{width: 250rpx;height: 500rpx;
}
/style
注该方法需要早mounted挂载后使用才行要不无法获取到dom信息