sf-utils2 sf-utils2
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
  • 快速开始

  • 基础-Base

  • 对象-Object

  • 数组-Array✨✨✨

  • 方法-Function

  • 字符串-String

  • 数学-Math

  • 文件-Buffer

  • 节点-dom

    • 序言 👏
    • _constant
    • getHttpBlob【获取远程二进制数据流】
    • downloadFile【http下载文件】
    • loadJsSync【单个加载js文件】
    • loadJsOrCssMulSync【批量加载远程css和js文件】
    • getAbsOffsetTop【距离父滚动区域顶部绝对距离】 🔥
    • getParentScrollElement【获取父元素滚动元素】 🔥
    • isScroll【是否是滚动容器】
    • scrollToX【水平滚动到某个具体位置】 🔥
    • scrollToY【垂直滚动到某个具体位置】 🔥
    • scrollToElement【滚动到某个dom元素】 🔥
    • isElementInContainer【元素是否整体在容器内】
    • batchElsPosInContainer【批量 dom 元素在 父的滚动视图位置】 🔥
    • isElementVisibleInViewport【是否可见在某个股东元素上】 🔥
      • 1.示例
      • 2.入参说明
      • 3.源码
    • observerElementMutation【监听dom元素属性变化】 🔥
    • print【打印】 🔥
    • partialCb【分片加载】
    • domUtils【dom方法操作基础】 🔥
  • 拓展

  • nodejs

目录

isElementVisibleInViewport【是否可见在某个股东元素上】 🔥

描述

判断是否元素 el 当前是否 可见在某个滚动元素上。
有个原生方法,IntersectionObserver (opens new window) 兼容性不是太好。,不过这个是相对于整个屏幕可视区域,不能定制划到某个特定滚动元素

# 1.示例

滚动一下下面区域👇试图
第8个box 部分可见: 否
第8个box 全部可见: 否
第20个box 部分可见: 否
第20个box 全部可见: 否
第1个box
第2个box
第3个box
第4个box
第5个box
第6个box
第7个box
第8个box
第9个box
第10个box
第11个box
第12个box
第13个box
第14个box
第15个box
第16个box
第17个box
第18个box
第19个box
第20个box
第21个box
第22个box
第23个box
第24个box
第25个box
第26个box
第27个box
第28个box
第29个box
第30个box
第31个box
第32个box
第33个box
第34个box
第35个box
第36个box
第37个box
第38个box
第39个box
第40个box
<template>
  <div>
    <el-divider content-position="left">滚动一下下面区域👇试图</el-divider>
    <el-row>
      <el-col :span="12">
        <div>
          第8个box 部分可见:
          <el-tag type="success" effect="dark" v-if="box8Visible">是</el-tag>
          <el-tag type="danger" effect="dark" v-else>否</el-tag>
        </div>
      </el-col>
      <el-col :span="12">
        <div>
          第8个box 全部可见: <el-tag type="success" effect="dark" v-if="box8AllVisible">是</el-tag>
          <el-tag type="danger" effect="dark" v-else>否</el-tag>
        </div>
      </el-col>
      <el-col :span="24">
        <div style="height: 12px"></div>
      </el-col>
      <el-col :span="12">
        <div>
          第20个box 部分可见: <el-tag type="success" effect="dark" v-if="box20Visible">是</el-tag>
          <el-tag type="danger" effect="dark" v-else>否</el-tag>
        </div>
      </el-col>
      <el-col :span="12">
        <div>
          第20个box 全部可见: <el-tag type="success" effect="dark" v-if="box20AllVisible">是</el-tag>
          <el-tag type="danger" effect="dark" v-else>否</el-tag>
        </div>
      </el-col>
    </el-row>
    <div
      class="isElementVisibleInViewport-view"
      ref="scrollView"
      style="overflow-x: hidden; overflow-y: auto; height: 400px;border: 1px solid chocolate;"
    >
      <div
        :class="[
          `isElementVisibleInViewport-box-${item}`,
          [8, 20].includes(item) && `isElementVisibleInViewport-box-active`
        ]"
        v-for="item in total"
        :key="item"
      >
        第{{ item }}个box
      </div>
    </div>
  </div>
</template>

<script>
import { isElementVisibleInViewport, throttle } from 'sf-utils2'

export default {
  data() {
    return {
      total: 40,
      box8Visible: false,
      box8AllVisible: false,
      box20Visible: false,
      box20AllVisible: false
    }
  },
  mounted() {
    console.log(this)
    this._throttleScroll = throttle(this.onScroll, 20)
    this.$refs.scrollView.addEventListener('scroll', this._throttleScroll, false)
  },
  methods: {
    onScroll() {
      this.box8Visible = isElementVisibleInViewport({
        el: this.$refs.scrollView.querySelector('.isElementVisibleInViewport-box-8'),
        elScrollView: this.$refs.scrollView,
        partiallyVisible: true
      })
      this.box20Visible = isElementVisibleInViewport({
        el: this.$refs.scrollView.querySelector('.isElementVisibleInViewport-box-20'),
        elScrollView: this.$refs.scrollView,
        partiallyVisible: true
      })
      this.box8AllVisible = isElementVisibleInViewport({
        el: this.$refs.scrollView.querySelector('.isElementVisibleInViewport-box-8'),
        elScrollView: this.$refs.scrollView,
        partiallyVisible: false
      })
      this.box20AllVisible = isElementVisibleInViewport({
        el: this.$refs.scrollView.querySelector('.isElementVisibleInViewport-box-20'),
        elScrollView: this.$refs.scrollView,
        partiallyVisible: false
      })
    }
  }
}
</script>
<style>

.isElementVisibleInViewport-view {
  overflow-x: hidden;
  overflow-y: auto;
  height: 400px;
  border: 1px solid chocolate;
  margin: 12px 0;
}
.isElementVisibleInViewport-view > div {
  background: #eee;
  padding: 28px;
  text-align: center;
}
.isElementVisibleInViewport-view > div + div {
  margin-top: 12px;
}
.isElementVisibleInViewport-view > div.isElementVisibleInViewport-box-active {
  background: darkorange;
  color: #fff;
}

</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
显示代码 复制代码 复制代码

# 2.入参说明

参数 说明 类型 是否必填 默认值
el 目标元素 HTMLElement 是 -
elScrollView 滚动区域元素,不传默认是 body 滚动元素 HTMLElement - -
partiallyVisible 是否部分显示 Boolean - true
direction 方向 可选值 horizontal(水平) vertical(垂直), 默认值 vertical String - -
offsetTop 顶部偏移量 Number - vertical
offsetBottom 底部偏移量 Number - 0
offsetLeft 左部偏移量 Number - 0
offsetRight 右部偏移量 Number - 0

# 3.源码

源码,点开查看 👈
/**
 * 检查指定的元素是否在视口中可见。
 * @param el 目标元素
 * @param elScrollView 滚动区域元素
 * @param partiallyVisible 是否部分显示
 * @param direction 方向 可选值  horizontal(水平)  vertical(垂直), 默认值 vertical
 * @param offsetTop 顶部偏移量
 * @param offsetBottom 底部偏移量
 * @param offsetLeft 左部偏移量
 * @param offsetRight  右部偏移量
 * @returns {boolean}
 */
function isElementVisibleInViewport({
  el,
  elScrollView,
  partiallyVisible = true,
  direction = 'vertical', // horizontal  vertical
  offsetTop = 0,
  offsetBottom = 0,
  offsetLeft = 0,
  offsetRight = 0
}) {
  if (!elScrollView) {
    elScrollView = document.documentElement || document.body
  }
  const elRect = el.getBoundingClientRect()
  const elScrollViewRect = elScrollView.getBoundingClientRect()
  if (direction === 'vertical') {
    return partiallyVisible
      ? elRect.bottom > elScrollViewRect.top + offsetTop && elRect.top < elScrollViewRect.bottom + offsetBottom
      : elRect.top > elScrollViewRect.top + offsetTop && elRect.bottom < elScrollViewRect.bottom + offsetBottom
  } else if (direction === 'horizontal') {
    return partiallyVisible
      ? elRect.right > elScrollViewRect.left + offsetLeft && elRect.left < elScrollViewRect.right + offsetRight
      : elRect.left > elScrollViewRect.left + offsetLeft && elRect.right < elScrollViewRect.right + offsetRight
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
上次更新: 2025/07/01, 14:52:29
batchElsPosInContainer【批量 dom 元素在 父的滚动视图位置】 🔥
observerElementMutation【监听dom元素属性变化】 🔥

← batchElsPosInContainer【批量 dom 元素在 父的滚动视图位置】 🔥 observerElementMutation【监听dom元素属性变化】 🔥→

Theme by Vdoing | Copyright © 2022-2025 bianpengfei
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×