我的网站开发技术经验总结 我的网站开发技术经验总结
首页

fangdown

我的网站开发技术经验总结
首页
  • 大前端

  • 基础

    • js

      • 你知道new操作符是如何实现的吗
      • 说说大文件上传的思路
      • 说说对闭包的理解及应用
      • 说说对单点登录sso的理解
      • 说说对防抖节流的理解
      • 说说对函数式编程的理解
      • 说说对内存泄漏的理解及触发场景
      • 说说对事件模型的理解
      • 说说对事件委托的理解及应用场景
      • 说说对原型及原型链的理解
      • 说说对正则表达式的理解
      • 说说对BOM的理解及常见操作
      • 说说对Dom的理解及常见操作
      • 说说对JavaScript中事件循环的理解
      • 说说对js的执行上下文的理解
      • 说说对js中变量作用域的理解
      • 说说对js中继承的理解及实现方式
      • 说说对this关键字的理解
      • 说说你对 Immutable Data的理解?如何应用在React项目中
      • 说说如何判断数据类型
      • 说说如何判断一个元素在可视区域内
        • 是什么
          • 常见应用
        • 实现方式
          • offsetTop scrollTop
          • getBoundingClientRect
        • 案例
      • 说说如何实现函数缓存
      • 说说深浅拷贝的区别及实现
      • 说说什么是尾递归及其应用
      • 说说为什么0.1+0.2!==0.3
      • 说说下拉刷新,上拉加载的原理
      • 说说ajax的实现原理
      • 说说call-apply-bind的作用及区别
      • 说说js中本地存储有哪些方式及区别
      • 说说js中的类型转换机制
      • Javscript数组的常用方法有哪些?
      • Javscript字符串的常用方法有哪些?
    • css

    • ES6

  • 框架

  • 情商

  • 算法

  • 网络

  • 千锤百炼
  • 基础
  • js
fangdown
2021-09-03
目录

说说如何判断一个元素在可视区域内

# 是什么

可视区域: 浏览器中肉眼能看到的区域

# 常见应用

  • 图片懒加载
  • 列表无限滚动
  • 计算广告的曝光情况
  • 可点击链接的预加载

# 实现方式

  • offsetTop scrollTop
  • getBoundingClientRect
  • intersection Obsever(用的不多)

# offsetTop scrollTop

  • clientWidth = content + padding
  • clientHeight = content + padding
  • 不包括 margin 和 border
function isInViewPortOfOne(el) {
  // viewPortHeight 兼容所有浏览器写法
  const viewPortHeight =
    window.innerHeight ||
    document.documentElement.clientHeight ||
    document.body.clientHeight;
  const offsetTop = el.offsetTop;
  const scrollTop = document.documentElement.scrollTop;
  const top = offsetTop - scrollTop;
  return top <= viewPortHeight;
}

# getBoundingClientRect

  • top 元素顶部到可视区域顶部距离
  • bottom 元素底部到可视区域顶部距离
$0.getBoundingClientRect();

bottom: 252;
height: 50;
left: 106;
right: 960;
top: 202;
width: 854;
x: 106;
  • 可视条件
    • top 大于等于 0
    • left 大于等于 0
    • bottom 小于等于视窗高度
    • right 小于等于视窗宽度
function isInViewPort(element) {
  const viewWidth = window.innerWidth || document.documentElement.clientWidth;
  const viewHeight =
    window.innerHeight || document.documentElement.clientHeight;
  const { top, right, bottom, left } = element.getBoundingClientRect();

  return top >= 0 && left >= 0 && right <= viewWidth && bottom <= viewHeight;
}

# 案例

插入 10 万长列表,滚动颜色变黄

<style>
  .container {
    display: flex;
    flex-wrap: wrap;
  }
  .target {
    margin: 5px;
    width: 20px;
    height: 20px;
    background: red;
  }
</style>

<div class="container"></div>
<script>
  const $container = $(".container");
  function createTagets() {
    const htmls = new Array(10000).fill('<div class="target"></div>').join("");
    $container.html(htmls);
  }
  function isInViewPort(element) {
    const viewWidth = window.innerWidth || document.documentElement.clientWidth;
    const viewHeight =
      window.innerHeight || document.documentElement.clientHeight;
    const { top, right, bottom, left } = element.getBoundingClientRect();

    return top >= 0 && left >= 0 && right <= viewWidth && bottom <= viewHeight;
  }
  createTagets();
  const targets = $(".target");
  $(window).on("scroll", () => {
    // 避免卡顿
    window.requestAnimationFrame(() => {
      targets.each((index, ele) => {
        if (isInViewPort(ele)) {
          $(ele).css("background-color", "yellow");
        } else {
          $(ele).css("background-color", "red");
        }
      });
    });
  });
</script>
#js
上次更新: 2021/12/19, 18:05:42
说说如何判断数据类型
说说如何实现函数缓存

← 说说如何判断数据类型 说说如何实现函数缓存→

最近更新
01
多分支修复撞车的问题
05-01
02
如何成为架构师
01-23
03
服务器部署全过程
11-23
更多文章>
Theme by Vdoing | Copyright © 2019-2026 fangdown | 粤ICP备19079809号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式