首页 > 其他分享 >MutationObserver监听DOM变化示例

MutationObserver监听DOM变化示例

时间:2024-09-13 21:28:46浏览次数:1  
标签:style right const DOM 示例 subview MutationObserver outer

示例代码:

<template>
  <div class="it-bottom-button" :style="{ right: bottomBarRight }">
    <slot></slot>
  </div>
</template>
<script>
export default {
  name: "itBottomBar",
  componentNmame: "itBottomBar",
  props: {
    el: {
      type: String,
      default: "#subview"
    },
    right: {
      type: String,
      default: "20px"
    }
  },
  data() {
    return {
      bottomBarScrollBarWidth: 0,
      bottomBarRight: "20px",
      mutationObserver: null
    };
  },
  watch: {
    right() {
      this.bottomBarRight = this.right;
    }
  },
  mounted() {
    this.getScrollWidth();
    this.initMutationObserver();
  },
  beforeDestroy() {
    // 停止监控
    this.mutationObserver.disconnect();
    this.mutationObserver = null;
  },
  methods: {
    getScrollWidth() {
      const outer = document.createElement("div");
      outer.className = "el-scrollbar__wrap";
      outer.style.visibility = "hidden";
      outer.style.width = "100px";
      outer.style.position = "absolute";
      outer.style.top = "-9999px";
      document.body.appendChild(outer);

      const widthNoScroll = outer.offsetWidth;
      outer.style.overflow = "scroll";

      const inner = document.createElement("div");
      inner.style.width = "100%";
      outer.appendChild(inner);

      const widthWithScroll = inner.offsetWidth;
      outer.parentNode.removeChild(outer);
      this.bottomBarScrollBarWidth = widthNoScroll - widthWithScroll;
    },

    // 初始化MutationObserver监听DOM变化
    initMutationObserver() {
      const MutationObserver = window.MutationObserver || window.WebkitMutationObserver || window.MozMutationObserver;
      // 监测浏览器是否支持
      const observeMutationSupport = !!MutationObserver;
      if (!observeMutationSupport) return;
      const subview = document.querySelector(this.el);
      const config = {
        attributes: true,
        childList: true,
        characterData: true,
        subtree: true
      };
      this.mutationObserver = new MutationObserver(() => {
        this.bottomBarViewChange();
      });
      this.mutationObserver.observe(subview, config);
    },

    bottomBarViewChange() {
      const subview = document.querySelector(this.el);
      let subviewScrollHeight = subview.scrollHeight; // 内容高度
      let subvviewOffsetHeight = subview.offsetHeight; // 可视区域高度
      if (subviewScrollHeight > subvviewOffsetHeight) {
        this.bottomBarRight = this.bottomBarScrollBarWidth + Number(this.right.replace("px", "")) + "px";
      } else {
        this.bottomBarRight = this.right;
      }
    }
  }
};
</script>

使用示例:

<it-bottom-bar>
      <el-button @click="goBack">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">保 存</el-button>
</it-bottom-bar>

标签:style,right,const,DOM,示例,subview,MutationObserver,outer
From: https://www.cnblogs.com/moqiutao/p/18412884

相关文章

  • MongoDB与Pymongo深度实践:从基础概念到无限级评论应用示例
    文章目录前言一、MongoDB1.基本介绍2.概念解析3.常见的数据类型4.Docker安装5.常用命令二、Pymongo1.基本操作(连接、数据库、集合)2.基本操作(增删改查)三、MongoDB应用示例:无限级评论1.MongoDB工具类2.实现无限级评论逻辑3.Vue树形结构展示无限级评论前言  ......
  • 【flask系列】基于flask的 RESTful API示例
    原创xlwin136人工智能教学实践RESTfulAPI(RepresentationalStateTransferApplicationProgrammingInterface)是一种基于REST架构风格的网络应用程序接口。REST是一种设计网络服务的架构风格,它通过使用HTTP协议的通用动词(如GET、POST、PUT、DELETE等)来允许客户端和......
  • 高德地图SDK Android版开发 11 覆盖物示例 4 线
    高德地图SDKAndroid版开发11覆盖物示例4线前言界面布局MapPolyline类常量成员变量初始值创建覆盖物移除覆盖物设置属性加载地图和释放地图MapPolylineActivity类控件响应事件运行效果图前言文本通过创建多个不同线宽的折线和大地曲线,介绍Polyline的使用方法。......
  • C# 标准事件模式示例
    1usingSystem;23namespaceConsoleApp3_Test4{56internalclassAa7{89staticvoidMain(string[]args)10{11Stockstock=newStock("THPW");12stock.Price=27.10M;131......
  • 示例 32: 简单的天气应用
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>WeatherApp</title>......
  • 百度地图SDK Android版开发 11 覆盖物示例 4 线
    百度地图SDKAndroid版开发11覆盖物示例4线前言界面布局MapPolyline类常量成员变量初始值创建覆盖物移除覆盖物设置属性加载地图和释放地图MapPolylineActivity类控件响应事件运行效果图前言文本通过创建多个不同线宽的折线和大地曲线,介绍Polyline的使用方法。......
  • Python 内置函数汇总(使用示例)
    abs(number)求解整数,浮点数绝对值,返回复数模大小aiter()异步的方式遍历可迭代对象的异步迭代器importasynciocount=0asyncdefexample():asyncdefasync_generator():globalcountwhileTrue:count+=1......
  • 《华为防火墙基本配置示例》
    目录华为防火墙基本配置示例: 1. 登录防火墙2. 基本系统配置 3. 安全区域配置4. 策略配置 5. NAT配置(如果需要) 6. 防火墙攻击防范配置 7. 保存配置华为防火墙基本配置示例: 1. 登录防火墙 -通过Console线将电脑与防火墙的Console接口连接......
  • 【抽奖小程序示例代码】
    以下是一个使用Python的Flask框架创建的简单网页抽奖小程序示例代码: fromflaskimportFlask,render_templateimportrandomapp=Flask(__name__)@app.route('/')deflottery():  prizes=["一等奖","二等奖","三等奖","谢谢参与"]  resul......
  • 全国增值税发票查验接口平台-JavaScript发票验真api示例
    全国增值税发票查验接口平台旨在优化纳税服务,加强企业发票管理,确保税收工作的准确性。企业财务可以通过发票查验接口方便快捷的验证增值税发票管理系统开具发票的真伪,以实现发票的自动化管理,减少人工操作失误,提高识别、录入、查验的准确性和工作效率,从而有效防止税务欺诈和逃......