今天fix一个拖拽库的IE8bug,发现DOM元素有一个getBoundingClientRect的方法。
主要介绍getBoundingClientRect的基本属性,以及具体的使用场景和一些需要注意的问题。 getBoundingClientRect Element.getBoundingClientRect() 含义: 方法返回元素的大小及其相对于视口的位置。 这个api,可以写成这样: var positionX = this.getBoundingClientRect().left + document.documentElement.scrollLeft function isElView(el) { var top = el.getBoundingClientRect().top; // 元素顶端到可见区域顶端的距离 var bottom = el.getBoundingClientRect().bottom; // 元素底部端到可见区域顶端的距离 var se = document.documentElement.clientHeight
rectObject = object.getBoundingClientRect(); 2.返回值类型: rectObject.top:元素上边到视窗上边的距离; rectObject.right: 1.2 用户滑动右侧的内容左侧的导航栏会响应式改变 右侧内容监听一个scroll事件,当触发滑动事件的时候获取粘性定位在顶部的标题,根据标题使导航栏定位到相应的li var obj = element.getBoundingClientRect document.getElementsByClassName('goodTitle'); for(var i = 0; i < titles.length; i++) { var style = titles[i].getBoundingClientRect
获取元素位置可以用 offset 或 getBoundingClientRect,使用 offset 因为兼容性不好,比较麻烦,offset获取位置会形成“回溯”。 而 getBoundingClientRect 方法则 兼容性较好,基本所有的浏览器都支持了,且使用起来更容易和简单。 1.使用语法: element.getBoundingClientRect(); 方法中没有任何参数,返回值为对象类型。 和 height 属性的解决方法: 在IE8及以下浏览器中,可以通过计算得到元素的宽和高: 如: var dom = document.querySelector("#demo"), r = dom.getBoundingClientRect
.container { .video-container { height: calc(100% - 90px); } } } 现象:通过元素的 getBoundingClientRect 这里会有一个异步或时间顺序问题,导致获取的与实际的不一致 解决:由于我这里的属性 v-direction 视频的方向是通过监听视频的相关事件获取之后,赋值到 body 上的,所以这里是一个异步函数,执行顺序一定在 getBoundingClientRect () 之后,于是我需要加个 setTimeout setTimeout(() => { const obj = container.getBoundingClientRect() }, 10
getBoundingClientRect介绍 getBoundingClientRect获取元素位置 getBoundingClientRect用于获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置 getBoundingClientRect是DOM元素到浏览器可视范围的距离(不包含文档卷起的部分)。 getBoundingClientRect()最先是IE的私有属性,现在已经是一个W3C标准。 ; getBoundingClientRect判断元素是否在可视区域 以前的办法是通过各种offset判断元素是否可见,网上很多教程,大家可以自己去查找。 getBoundingClientRect是获取可视区域相关位置信息的,使用这个属性来判断更加方便: function isElementInViewport (el) { var rect =
//(注意:offset()的本质是getBoundingClientRect()的top、left + pageYOffset、pageXOffset) return { offset = elem.getBoundingClientRect(); } return { top: offset.top - parentOffset.top 注意: ① getBoundingClientRect() 计算的是目标元素的border的位置(左上角),是不包括margin的 ② 如果不加上margin的话(代码是通过减去,来算上margin的) 举个例子: let p=document.querySelector("#pTwo") console.log(p.getBoundingClientRect(),'pTwo11'); //x:8,y: (),'pTwo11'); //x:8,y:16 可以看到getBoundingClientRect()指定坐标是到border上的,这是不准确的,因为在里面的子元素的位置也会受父元素的border影响
我们实现页面懒加载的方案一般有三种方式: 获取元素clientHeight等位置信息 Element.getBoundingClientRect() IntersectionObserver 我们先写 () Element.getBoundingClientRect() 返回一个DOMRect对象,包含了描述元素的位置属性:left,right,top,bottom。 rectObject = object.getBoundingClientRect(); 我们可以使用定时器或者scroll事件,在回调函数调用元素的getBoundingClientRect()方法, //懒加载核心:判断加载 function islazyLoadBool(dom) { let imgTop = dom.getBoundingClientRect().top; //获取响度浏览器可视区的高度 document.documentElement.scrollTop || document.body.scrollTop; return imgTop <= clientHeight + 100 ; } 因为getBoundingClientRect
getBoundingClientRect方法兼容IE7写法 rectObject = object.getBoundingClientRect(); // 返回元素的大小及其相对于视口的位置 ie7中的 getBoundingClientRect方法只有bottom、left、right、top属性,没有width合height 通过上图可以看出: 除了width和height,其它几个属性都是相对于视口的左上角而言的 根据left、right的值可以推算出width 根据top、bottom的值可以推算出height 完整的代码如下: /** * getBoundingClientRect 兼容IE7 * @param {DOM Object} obj */ function getBoundingClientRect(obj) { var boundingClientRect = obj.getBoundingClientRect newObj.height = boundingClientRect.bottom - boundingClientRect.top; } return newObj; } 相关链接 getBoundingClientRect
代码:
getBoundingClientRect().y const n2y = n2.current?. getBoundingClientRect().y const n3y = n3.current?. getBoundingClientRect().y const n4y = n4.current?. getBoundingClientRect().y const n5y = n5.current?. getBoundingClientRect().y as number const n2y = n2.current?.
().left; var y = event.clientY- canvas.getBoundingClientRect().top; ().left; var y = e.clientY - canvas.getBoundingClientRect().top; context.beginPath ().left; var y = event.clientY - canvas.getBoundingClientRect().top; ().left; var y = e.clientY - canvas.getBoundingClientRect().top; context.beginPath ().left; var y = event.clientY - canvas.getBoundingClientRect().top;
offsetWidth/offsetHeight 返回值包含content + padding + border,效果与 e.getBoundingClientRect()相同 clientWidth/ getBoundingClientRect (IE67的left、top会少2px,并且没有width、height属性)可以用来获取元素位置, 比如我经常用来获取滚动之后元素距离顶端的距离为0来判断 是否滚动到元素这个位置,然后把头部设置为fixed,比如div.getBoundingClientRect().top。 当然上面的属性也可以做到比如: 页面元素距离浏览器工作区顶端的距离 (div.getBoundingClientRect().top) = div.offsetTop – (document.documentElement.scrollTop
第一种技术使用getBoundingClientRect方法在点击时显式动画导航栏项之间的边框。第二种方法使用新的View Transition API实现相同功能。 href="#services">Services About Contact方法一:使用getBoundingClientRect (); const navbarPos = navbar.getBoundingClientRect(); const relativePosX = itemPos.left - navbarPos.left 我们通过两种方法演示了这一点:结合CSS过渡和getBoundingClientRect()方法,以及使用View Transition API。 资源getBoundingClientRect()方法文档View Transition API文档Jake Archibald的"View Transitions: Handling Aspect Ratio
().x - btnDelete.getBoundingClientRect().width * 0.5; let dy1 = y - btnDelete.getBoundingClientRect ().y - btnDelete.getBoundingClientRect().height * 0.5; let dx2 = x - btnCancel.getBoundingClientRect ().x - btnCancel.getBoundingClientRect().width * 0.5; let dy2 = y - btnCancel.getBoundingClientRect ().y - btnCancel.getBoundingClientRect().height * 0.5; let px = (x - confirm.getBoundingClientRect ().x) / confirm.getBoundingClientRect().width; let py = (y - confirm.getBoundingClientRect().y) /
这里的 getBoundingClientRect 是返回元素距离可以可视区域的距离和宽高的: 而 window.pageYOffset 也叫 window.scrollY,顾名思义就是窗口滚动的距离。 想一下,pageY 去了 window.scrollY,去了 getBoundingClientRect().top,剩下的可不就是 offsetY 么: 试一下: const clickHandler 这里 clientY 和 getBoundingClientRect().top 也要区分下: 一个是元素距离可视区域顶部的距离,一个是鼠标事件触发位置到可视区域顶部的距离。 那看起来 getBoundingClientRect().height 和 offsetHeight 一模一样? 绝大多数情况下是的。 但你旋转一下: 就不一样了: getBoundingClientRect 拿到的包围盒的高度,而 offsetHeight 是元素本来的高度。
3大点: 1.元素位置 2.动态脚本 3.动态样式 一.元素位置 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);//元素左边距离页面左边的距离 PS:IE、Firefox3+、Opera9.5、Chrome IE为2 document.documentElement.clientLeft;//非IE为0,IE为2 function getRect(element) { var rect = element.getBoundingClientRect
使用 getBoundingClientRect 方法 getBoundingClientRect 方法可以获取元素相对于视口(viewport)的大小和位置。 我们可以通过以下 JavaScript 代码获取这个 div 元素的位置: const div = document.querySelector('div'); const rect = div.getBoundingClientRect ; console.log(rect.top, rect.right, rect.bottom, rect.left); 这里,我们首先用 querySelector 获取 div 元素,然后调用 getBoundingClientRect 可以通过 getBoundingClientRect 获取按钮的位置,然后计算提示框的位置。 const div = document.querySelector('div'); const getOffset = (el) => { const rect = el.getBoundingClientRect
第一种技术使用getBoundingClientRect方法,在导航栏项目被点击时显式地动画化边框。第二种方法使用新的View Transition API实现相同的功能。 document.querySelector('#highlight'); const width = activeNavItem.offsetWidth; const itemPos = activeNavItem.getBoundingClientRect (); const navbarPos = navbar.getBoundingClientRect() const relativePosX = itemPos.left - navbarPos.left 我们通过两种方法演示了这一点:使用CSS过渡结合getBoundingClientRect()方法,以及使用View Transition API。 资源getBoundingClientRect()方法文档View Transition API文档Jake Archibald的"视图过渡:处理宽高比变化"
().width + (navTargetWidth - nav.getBoundingClientRect().width)*0.1; //这次目标宽度 nav.style.width = navTargetWidth:thisTarget) + "px"; body.style.marginLeft = nav.style.width; if(nav.getBoundingClientRect /*关闭/打开导航*/ function closeNav(nav,body,navTargetWidth){ var thisTarget = nav.getBoundingClientRect ().width + (navTargetWidth - nav.getBoundingClientRect().width)*0.1; //这次目标宽度 nav.style.width navTargetWidth:thisTarget) + "px"; body.style.marginLeft = nav.style.width; if(nav.getBoundingClientRect