很多时候,我们需要检查一个元素是否已经部分不在或者全部不在视窗区域,当这种现象发生时做相应的处理。

比如在CMS编辑内容时,其工具菜单很有可能因为内容区域过长导致滑出视窗区域,而工具栏又是经常要使用的,这就非常不便。

那么如何检查这种情况呢?我们来看看 getBoundingClientRect() 函数吧。

element.getBoundingClientRect()调用将返回一个对象,该对象包含了该元素相对于viewport的top,bottom,left,和right的位置。

我们看看以下代码:

var elem = document.querySelector('#some-element');
var bounding = elem.getBoundingClientRect(); // Returns something like this:
// {top: -123, left: 0, right: 0, bottom: 25}
console.log(bounding);

我们可以通过简单的数学比较来检查是否元素已经跑到了viewport的外部。

如果bounding.top或者bounding.left小于0,那么top或者left部分已经不在viewport里面。如果bounding.right大于viewport.width或者bounding.bottom大于viewport.height,则right或者bottom部分就不在viewport里面了。

if (bounding.top < 0) {
// Top is out of viewport
} if (bounding.left < 0) {
// Left side is out of viewoprt
} if (bounding.bottom > (window.innerHeight || document.documentElement.clientHeight)) {
// Bottom is out of viewport
} if (bounding.right > (window.innerWidth || document.documentElement.clientWidth)) {
// Right side is out of viewport
}

需要注意的是并不是所有的浏览器都支持window.innerWidth和window.innerHeight,因此我们必须有能力回滚兼容到document.documentElement.clientWidth和document.documentElement.cleintHeight属性上面。

写一个helper函数:

/*!
* Check if an element is out of the viewport
* (c) 2018 Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {Node} elem The element to check
* @return {Object} A set of booleans for each side of the element
*/
var isOutOfViewport = function (elem) { // Get element's bounding
var bounding = elem.getBoundingClientRect(); // Check if it's out of the viewport on each side
var out = {};
out.top = bounding.top < 0;
out.left = bounding.left < 0;
out.bottom = bounding.bottom > (window.innerHeight || document.documentElement.clientHeight);
out.right = bounding.right > (window.innerWidth || document.documentElement.clientWidth);
out.any = out.top || out.left || out.bottom || out.right;
out.all = out.top && out.left && out.bottom && out.right; return out; };

随后,我们通过下面的代码来简单调用检查是否不在viewport中:

var elem = document.querySelector('#some-element');
window.addEventListener( 'scroll', handler )

function handler(){
var isOut = isOutOfViewport(elem); if (isOut.top) {
// Top is out of viewport
} if (isOut.left) {
// Left side is out of viewoprt
} if (isOut.bottom) {
// Bottom is out of viewport
} if (isOut.right) {
// Right side is out of viewport
}
if (isOut.any) {
// At least one side of the element is out of viewport
} if (isOut.all) {
// The entire element is out of viewport
}
}
 

https://gomakethings.com/how-to-check-if-any-part-of-an-element-is-out-of-the-viewport-with-vanilla-js/

https://gomakethings.com/how-to-test-if-an-element-is-in-the-viewport-with-vanilla-javascript/

香草js侦测元素是否离开视窗viewport的更多相关文章

  1. JQ的offset().top与JS的getBoundingClientRect区别详解,JS获取元素距离视窗顶部可变距离

     壹 ❀ 引 我在 JQ的offset().top与js的offsetTop区别详解 这篇博客中详细分析了JQ方法offset().top与JS属性offsetTop的区别,并得出了一条offset( ...

  2. js获取元素位置和style的兼容性写法

    今天说一下js获取元素位置和style的方法.当然不只是element.style那么简单.. 主角:getBoundingClientRect,getClientRects,getComputedS ...

  3. paip.调试js 查看元素事件以及事件断点

    paip.调试js  查看元素事件以及事件断点 ff 26 +firebug 查看不出来.. 360 ,虽然也是chrome 基础,但是开发工具烂阿,也是显示不出来.. 作者Attilax  艾龙,  ...

  4. JS子元素oumouseover触发父元素onmouseout

    原文:JS子元素oumouseover触发父元素onmouseout JavaScript中,父元素包含子元素: 当父级设置onmouseover及onmouseout时,鼠标从父级移入子级,则触发父 ...

  5. 【全面总结】js获取元素位置大小

    [js获取元素位置+元素大小]全面总结 目录 1.关于offset offsetParent(只读) offsetTop(只读) offsetLeft(只读) offsetHeight(只读) off ...

  6. JS获得元素相对位置坐标getBoundingClientRect()

    getBoundingClientRect用于获取某个元素相对于视窗的位置集合.集合中有top, right, bottom, left等属性. 1.语法:这个方法没有参数. rectObject = ...

  7. js获取元素提示信息

    js获取元素提示信息 var date=$("#date").attr('placeholder'); js修改元素的提示信息 $("#date").attr( ...

  8. js设置元素不能编辑

    js设置元素不能编辑 $("#startLocation").attr("readOnly",true); js设置元素可以编辑 $("#startL ...

  9. js隐藏元素

    js隐藏元素 $("#serviceType").css('display','none'); js显示元素 $("#serviceType3").css('d ...

随机推荐

  1. python处理孤立的异常点

    假设有一个列表,a = [61, 40, 70, 80, 86, 50, 88, 33, 76, 64],保存的是设备的状态值随时间的变化,超过60即为异常,但是对于孤立的异常点,我们需要将其忽略,只 ...

  2. 非root用户安装、配置mysql

    1. 下载mysql,可能是因为服务器操作系统版本较低(CentOS4.3),安装5.7时提示缺lib,刚好我不需要一定安装新版,所以下载了5.1 Linux - Generic (glibc 2.5 ...

  3. Portainer

    docker search portainer docker pull portainer/portainer docker run -it \ --name prtainer \ -p 9000:9 ...

  4. 201871010126 王亚涛《面向对象程序设计(Java)》第十二周学习总结

      内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p/ ...

  5. SpingBoot四——前后端分离

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:https://www.cnblogs.com/by-dream/p/11336057.html 根据之前的教程,我们已经可以独立 ...

  6. 莫烦TensorFlow_05 add_layer

    import tensorflow as tf import numpy as np def add_layer(inputs, in_size, out_size, activation_funct ...

  7. 【转载】Makedown数学公式语法

    Typora数学模块 行间表达式快捷键($$) 点击"段落"->"公式块" 快捷键Ctrl+Shift+m "$$"+回车 行内表达式 ...

  8. node端console.log输出不同颜色文字

    我们知道console.log直接输出是按着终端的默认颜色来显示的, console.log('message') 那么如何指定他们的颜色显示呢?很简单,直接再加一个参数就可以了,例如: consol ...

  9. onchange onpropertychange 和 oninput 事件的区别

    onchange 事件在内容改变(两次内容有可能还是相等的)且失去焦点时触发. onpropertychange 事件却是实时触发,即每增加或删除一个字符就会触发,通过 js 改变也会触发该事件,但是 ...

  10. 简析平衡树(四)——FHQ Treap

    前言 好久没码过平衡树了! 这次在闪指导的指导下学会了\(FHQ\ Treap\),一方面是因为听说它可以可持久化,另一方面则是因为听说它是真的好写. 简介 \(FHQ\ Treap\),又称作非旋\ ...