位置:首页 > web前端 > vue

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方法,你看懂了吗?

24人点赞 返回栏目 提问 分享一波

小礼物走一波,支持作者

还没有人赞赏,支持一波吧

留言(问题紧急可添加微信 xxl18963067593) 评论仅代表网友个人 留言列表

暂无留言,快来抢沙发吧!

本刊热文
网友在读
手机扫码查看 手机扫码查看