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【元素是否整体在容器内】
      • 1.示例
      • 2.入参说明
      • 3.源码
    • batchElsPosInContainer【批量 dom 元素在 父的滚动视图位置】 🔥
    • isElementVisibleInViewport【是否可见在某个股东元素上】 🔥
    • observerElementMutation【监听dom元素属性变化】 🔥
    • print【打印】 🔥
    • partialCb【分片加载】
    • domUtils【dom方法操作基础】 🔥
  • 拓展

  • nodejs

目录

isElementInContainer【元素是否整体在容器内】

描述

判断是否元素 el 整体都包含在 container 容器里,返回值 Boolean

# 1.示例

推荐打开控制查看 dom元素节点
容器
盒子1:【盒子1】是否存在【容器】中:否
盒子2:【盒子2】是否存在【容器】中:否
<template>
  <div>
    <el-divider content-position="left">推荐打开控制查看 dom元素节点</el-divider>

    <div class="isElementInContainer-box">
      容器
      <div class="isElementInContainer-box-1">盒子1:【盒子1】是否存在【容器】中:{{ _isInLabel1 }}</div>
    </div>

    <div class="isElementInContainer-box-2">盒子2:【盒子2】是否存在【容器】中:{{ _isInLabel2 }}</div>
  </div>
</template>

<script>
import sfutils from 'sf-utils2'

export default {
  data() {
    return {
      isIn1: false,
      isIn2: false
    }
  },

  computed: {
    _isInLabel1() {
      return this.isIn1 ? '是' : '否'
    },
    _isInLabel2() {
      return this.isIn2 ? '是' : '否'
    }
  },

  mounted() {
    const containerDom = document.querySelector('.isElementInContainer-box')
    const box1Dom = document.querySelector('.isElementInContainer-box-1')
    const box2Dom = document.querySelector('.isElementInContainer-box-2')
    this.isIn1 = sfutils.isElementInContainer(box1Dom, containerDom)
    this.isIn2 = sfutils.isElementInContainer(box2Dom, containerDom)
  }
}
</script>
<style>

.isElementInContainer-box {
  padding: 20px;
  border: 1px solid cornsilk;
  background-color: coral;
  color: #fff;
}
.isElementInContainer-box-1 {
  padding: 30px;
  background: #adad55;
}
.isElementInContainer-box-2 {
  padding: 30px;
  background: #08f784;
  margin-top: 10px;
}

</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
显示代码 复制代码 复制代码

# 2.入参说明

参数 说明 类型 是否必填 默认值
el 目标元素 HTMLElement 是 -
container 某个 dom 容器 HTMLElement 是 -

# 3.源码

源码,点开查看 👈
/**
 * 判断是否元素el位置在 container容器里
 * @param el
 * @param container
 * @returns {boolean}
 */
function isElementInContainer(el, container) {
  if (!el || !container) return false
  const elRect = el.getBoundingClientRect()
  let containerRect
  if ([window, document, document?.documentElement, null, undefined].includes(container)) {
    containerRect = {
      top: 0,
      right: window?.innerWidth,
      bottom: window?.innerHeight,
      left: 0
    }
  } else {
    containerRect = container.getBoundingClientRect()
  }
  return (
    elRect.top < containerRect.bottom &&
    elRect.bottom > containerRect.top &&
    elRect.right > containerRect.left &&
    elRect.left < containerRect.right
  )
}
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
上次更新: 2025/07/01, 14:52:29
scrollToElement【滚动到某个dom元素】 🔥
batchElsPosInContainer【批量 dom 元素在 父的滚动视图位置】 🔥

← scrollToElement【滚动到某个dom元素】 🔥 batchElsPosInContainer【批量 dom 元素在 父的滚动视图位置】 🔥→

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