获取浏览器滚动的高度:

scrollTop=document.documentElement.scrollTop || document.body.scrollTop

getBoundingClientRect()

这个方法返回一个矩形对象,包含四个属性:left、top、right和bottom。分别表示元素各边与页面上边和左边的距离。

var box=document.getElementById('box');         // 获取元素

alert(box.getBoundingClientRect().top);         // 元素上边距离页面上边的距离

alert(box.getBoundingClientRect().right);       // 元素右边距离页面左边的距离

alert(box.getBoundingClientRect().bottom);      // 元素下边距离页面上边的距离

alert(box.getBoundingClientRect().left);        // 元素左边距离页面左边的距离

注意:IE、Firefox3+、Opera9.5、Chrome、Safari支持,在IE中,默认坐标从(2,2)开始计算,导致最终距离比其他浏览器多出两个像素,我们需要做个兼容。

document.documentElement.clientTop;  // 非IE为0,IE为2

document.documentElement.clientLeft; // 非IE为0,IE为2

functiongGetRect (element) {

var rect = element.getBoundingClientRect();

var top = document.documentElement.clientTop;

var left= document.documentElement.clientLeft;

return{

top    :   rect.top - top,

bottom :   rect.bottom - top,

left   :   rect.left - left,

right  :   rect.right - left

}

}

分别加上外边据、内边距、边框和滚动条,用于测试所有浏览器是否一致。

最新文章

  1. C++内联函数
  2. SQL 中的 AND OR
  3. 安装出现了error launching installer
  4. word20161225
  5. 在QtCreator 2.1.0 下使用opencv231库加载图片并显示
  6. CentOS6.5 (64bit) 光盘内部FTP源
  7. equals()方法
  8. weblogic sockets 和 thread 问题解决
  9. 218. The Skyline Problem
  10. android测试分析1
  11. 贝叶斯网络基础(Probabilistic Graphical Models)
  12. mysql5.7-Group Replication
  13. [LeetCode] Valid Square 验证正方形
  14. 图片和base64相互转化
  15. mysql基本命令总结
  16. vuex学习
  17. 避免for循环
  18. Elasticsearch5.x批量插入数据(Java)
  19. Python爬虫实战(二):爬百度贴吧
  20. spring结合mybatis实现数据库读写分离

热门文章

  1. Centos6下安装Hadoop2.6 问题总结
  2. spark1.4.1 启动过程
  3. (转)Doxygen文档生成工具
  4. 数据结构(Java描述)之线性表
  5. WinCE常用调试工具汇总
  6. 错误"ORA-04091: table is mutating, trigger/function may not see it"的原因以及解决办法
  7. CCPC2016沈阳站
  8. 1323 union解题报告
  9. java 装饰者模式与继承的区别
  10. HTML form 表单