香草js侦测元素是否离开视窗viewport
很多时候,我们需要检查一个元素是否已经部分不在或者全部不在视窗区域,当这种现象发生时做相应的处理。
比如在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-test-if-an-element-is-in-the-viewport-with-vanilla-javascript/
香草js侦测元素是否离开视窗viewport的更多相关文章
- JQ的offset().top与JS的getBoundingClientRect区别详解,JS获取元素距离视窗顶部可变距离
壹 ❀ 引 我在 JQ的offset().top与js的offsetTop区别详解 这篇博客中详细分析了JQ方法offset().top与JS属性offsetTop的区别,并得出了一条offset( ...
- js获取元素位置和style的兼容性写法
今天说一下js获取元素位置和style的方法.当然不只是element.style那么简单.. 主角:getBoundingClientRect,getClientRects,getComputedS ...
- paip.调试js 查看元素事件以及事件断点
paip.调试js 查看元素事件以及事件断点 ff 26 +firebug 查看不出来.. 360 ,虽然也是chrome 基础,但是开发工具烂阿,也是显示不出来.. 作者Attilax 艾龙, ...
- JS子元素oumouseover触发父元素onmouseout
原文:JS子元素oumouseover触发父元素onmouseout JavaScript中,父元素包含子元素: 当父级设置onmouseover及onmouseout时,鼠标从父级移入子级,则触发父 ...
- 【全面总结】js获取元素位置大小
[js获取元素位置+元素大小]全面总结 目录 1.关于offset offsetParent(只读) offsetTop(只读) offsetLeft(只读) offsetHeight(只读) off ...
- JS获得元素相对位置坐标getBoundingClientRect()
getBoundingClientRect用于获取某个元素相对于视窗的位置集合.集合中有top, right, bottom, left等属性. 1.语法:这个方法没有参数. rectObject = ...
- js获取元素提示信息
js获取元素提示信息 var date=$("#date").attr('placeholder'); js修改元素的提示信息 $("#date").attr( ...
- js设置元素不能编辑
js设置元素不能编辑 $("#startLocation").attr("readOnly",true); js设置元素可以编辑 $("#startL ...
- js隐藏元素
js隐藏元素 $("#serviceType").css('display','none'); js显示元素 $("#serviceType3").css('d ...
随机推荐
- Python玩转微信小程序
用Python玩转微信 Python玩转微信 大家每天都在用微信,有没有想过用python来控制我们的微信,不多说,直接上干货! 这个是在 itchat上做的封装 http://itchat. ...
- Python 函数小程序初解
目录 作业 ==程序代码自上往下运行,建议自上而下的完成下列任务== 作业 文件a.txt内容:每一行内容分别为商品名字,价钱,个数,求出本次购物花费的总钱数 sum = 0 f = open('a. ...
- UiPath:Split(","c)以逗号区分遍历字符串数组
学习中遇到同一用户多种职业的情况,因为所有的数据都是从Excel里面取的,所以只能把所有的职业写在一个单元格里,以逗号区分. 那么就需要先把字符串转为数组,然后遍历数组去添加职位.如图 Split(& ...
- Python内容
1.Python介绍.计算机硬件.网络.变量.数据类型:列表+元组+字典+布尔值+字符串+数字+集合.格式化输出.if判断.for循环.while循环. 2.三元运算.字符编码.文件处理:r/rb(读 ...
- 8.Vue的slot
1.什么是slot 在 Vue.js 中我们使用 <slot> 元素作为承载分发内容的出口,作者称其为 插槽,可以应用在组合组件的场景中 2.使用 建立组件预留插槽 定义填充入插槽 ...
- 实现 Cloneable 需要注意
产品Product里面包含BaseInfo对象:Product(productName,companyName,baseinfo)如果implement Cloneable 需要实现 注意强转类 ...
- Python--单元四练习
一.算24 描述: 给出4个小于10的正整数,可以使用加.减.乘.除4种运算以及括号把4个数连接起来得到一个表达式.现在问题是,是否存在一种方式使得所得表达式的结果等于24. ...
- nginx 配置实例(ssl、proxy、cache、gzip、upstream等优化)
[root@xxxxxxxxxxxxxx ~]# cat /usr/local/nginx/conf/nginx.conf user nobody; worker_processes ; worker ...
- Nacos配置的多环境管理
实现多环境管理有下面几种方式 1.使用Data ID与profiles实现 在 Nacos Config Starter 中,dataId 的拼接格式为 ${prefix} - ${spring.pr ...
- c语言中static 函数和普通函数的区别
C程序一直由下列部分组成: 1)正文段——CPU执行的机器指令部分:一个程序只有一个副本:只读,防止程序由于意外事故而修改自身指令: 2)初始化数据段(数据段)——在程序中所有赋了初值的全局变量,存放 ...