当前位置: 首页 > news >正文

javascript获取元素在浏览器中工作区域的左、右、上、下距离,或带滚动条的元素在页面中的大小

//获取元素在包含元素框中的大小
//第1个函数为获取元素在包含元素中左内边框的距离
function getELementLeft(element){//获取元素在包含元素左边距离var actualeft=element.offsetLeft;//获取元素的上级包含元素var current=element.offsetParent;//循环到一直没有包含元素while(current !==null){actualeft+=current.offsetLeft;current=current.offsetParent;}return actualeft;}
//第2个函数为获取元素在包含元素中顶部内边框的距离function getElementTop(element){var actuatop=element.offsetTop;var current=element.offsetParent;while(current !==null){actuatop += current.offsetTop;current=current.offsetParent;}return actuatop;}//获取元素在浏览器工作区域中的位置,相对于浏览器工作区域的左、右、上、下的值
function getBoundingClientRect(element){//获取带有垂直滚动条的页面区,包括在上面的隐藏内容的像素数,document.body.scrollTop为兼容IEvar scrollTop=document.documentElement.scrollTop || document.body.scrollTop;//获取带有水平滚动条的页面区,包括在左边的隐藏内容的像素数,document.body.scrollLeft为兼容IEvar scrollLeft=document.documentElement.scrollLeft || document.body.scrollLeft;//检查元素的getBoundingClientRect方法,这个方法是元素自有的,不是我们上面的那个函数名if(element.getBoundingClientRect){//设置arguments.callee的offset属性,才设置肯定为NaN,不等于numberif(typeof arguments.callee.offset !="number"){//创建一个零时的div元素,设置他的left top 为0var temp=document.createElement("div");temp.style.cssText="position:absolute;left:0;top:0;";document.body.appendChild(temp);//让offset获取到值,如果是IE8浏览器以前的为-2arguments.callee.offset=-temp.getBoundingClientRect().top-scrollTop;document.body.removeChild(temp);temp=null;}var rect=element.getBoundingClientRect();//获取offset值var offset=arguments.callee.offset;//返回元素对于浏览器工作区左中上下距离return {left:rect.left + offset,right:rect.right+offset,top:rect.top+offset,bottom:rect.bottom+offset};//如果元素没有element.getBoundingClientRect,则以我们上面两个函数来计算带滚动条的工作区坐标位置}else{var actualLeft=getElementLeft(element);var actualTop=getElementTop(element);}return {left:actualLeft-scrollLeft,right:actualLeft+element.offsetWidth - scrollLeft,top:actulTop-scrollTop,bottom:actualTop+element.offsetHeight - scrollTop}            }
var div=document.getElementById("container");
var bd=document.getElementsByClassName("bd")[0];
var size=getBoundingClientRect(bd);
console.log(size.left);

//html代码部分

<div id="container"><div class="bd"></div></div>

//css

<style type="text/css">#container{width: 800px;height: 500px;border: 1px solid #ccc;margin: 0 atuo;margin-top: 50px;}div#container .bd{width:400px;height: 400px;border: solid 1px blue;position: relative;top: 50px;left:100px;}</style>
http://www.lryc.cn/news/171034.html

相关文章:

  • VSCode 安装使用教程 环境安装配置 保姆级教程
  • c盘中temp可以删除吗?appdata\local\temp可以删除吗?
  • Java手写聚类算法
  • 解密Java多线程中的锁机制:CAS与Synchronized的工作原理及优化策略
  • solid works草图绘制与设置零件特征的使用说明
  • vue3使用router.push()页面跳转后,该页面不刷新问题
  • 如何理解数字工厂管理系统的本质
  • 笔记1.3 数据交换
  • 实时车辆行人多目标检测与跟踪系统(含UI界面,Python代码)
  • 谷歌AI机器人Bard发布强大更新,支持插件功能并增强事实核查;全面整理高质量的人工智能、机器学习、大数据等技术资料
  • NI SCXI-1125 数字量控制模块
  • 链表oj题1(Leetcode)——移除链表元素,反转链表,链表的中间节点,
  • 【libuv】与uvgrtrp的_SSIZE_T_定义不同
  • 安卓ROM定制 修改必备常识-----初步了解system系统分区文件夹的基本含义 【二】
  • GPT会统治人类吗
  • win系统环境搭建(六)——Windows安装nginx
  • Java中使用BigDecimal类相除保留两位小数
  • 激光雷达在ADAS测试中的应用与方案
  • malloc与free
  • 计算周包材,日包材用来发送给外围系统
  • R语言柱状图直方图 histogram
  • Linux磁盘管理:最佳实践
  • uni-app:通过三目运算动态增加样式效果(class)
  • API安全
  • 手写一个翻页功能
  • element show-overflow-tooltip 复制
  • 【C语言】指针的进阶(三)—— 模拟实现qsort函数以及指针和数组的笔试题解析
  • Python 图像处理库PIL ImageOps笔记
  • 全球南方《乡村振兴战略下传统村落文化旅游设计》许少辉八一新枝——2023学生开学季辉少许
  • 【C语言】指针的进阶(一)