源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

获取元素距离浏览器周边的位置的方法getBoundingClientRect

  • 时间:2021-06-29 00:28 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:获取元素距离浏览器周边的位置的方法getBoundingClientRect
[u]复制代码[/u] 代码如下:
var box = document.getElementById( "gaga1" ); /* alert( box.getBoundingClientRect().top ); alert( box.getBoundingClientRect().right ); alert( box.getBoundingClientRect().bottom ); alert( box.getBoundingClientRect().left ) */ function getRect( elements ){ var rect = elements.getBoundingClientRect(); var clientTop = document.documentElement.clientTop; var clientLeft = document.documentElement.clientLeft; return { // 兼容ie多出的两个px top : rect.top - clientTop, // 距离顶部的位置 bottom : rect.bottom - clientTop, // 距离顶部加上元素本身的高度就等于bottom的位置 left : rect.left - clientLeft, // 距离左边的位置 right : rect.right - clientLeft // 距离右边的位置就是 距离左边的位置加上元素本身的宽度 }; }; alert( getRect(box).top )
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部