uniapp获取元素到顶部的距离 元素位置
dearweb
发布:2021-08-27 15:58:27阅读:
不少小伙伴在使用uniapp是需要获取dom元素的位置,小编今天亲测之后将方法分享出来,给大家。
在uniapp里有一个api,uni.createSelectorQuery()
// let that = this
// let queryDom = uni.createSelectorQuery().in(that) // 使用api并绑定当前组件
// queryDom
// .select('.app-mobile-package-nav')
// .boundingClientRect((res) => {
// that.navTop = res.top + 160
// console.log(that.navTop)
// })
// .exec()
// let that = this
// let queryDom = uni.createSelectorQuery().in(that)
// queryDom
// .select('.app-mobile-package-nav')
// .boundingClientRect((res) => {
// that.navTop = res.top + 160
// console.log(that.navTop)
// })
// .exec()获取异步返回 res 的值
{
bottom: 1221 // 元素距离底部的距离
dataset: {}
height: 856 // 元素高度
left: 0 // 元素距离左边的距离
right: 375 // 元素距离右侧的距离
top: 365 // 元素距离顶部的距离
width: 375 // 元素宽度
}uniapp中获取元素位置的api方法,你看懂了吗?
小礼物走一波,支持作者
赏还没有人赞赏,支持一波吧