如何監(jiān)聽DOM大小的變化 wd-domsize-monitor

wd-domsize-monitor
https://blog.csdn.net/mapbar_front/article/details/83190372

export default (function() {
    const elList = [];
    let timer = 0;
    function bind(el, next) {
        let obj = {
            el,
            callback: next,
            style: {
                width: getStyle(el, 'width'),
                height: getStyle(el, 'height'),
            }
        }
        elList.push(obj);
    }
    function remove(el) {
        elList.splice(elList.indexOf(el))
        if (elList.indexOf(el) !== -1) {
            elList.splice(elList.indexOf(el), 1);
        }
    }
    timer = setInterval(() => {
        for (let i = 0; i < elList.length; i++) {
            let dom = elList[i].el
            const style = {
                width: getStyle(dom, 'width'),
                height: getStyle(dom, 'height'),
            }
            if (!isEqul(style, elList[i].style)) {
                elList[i].style = {
                    width: style.width,
                    height: style.height,
                }
                elList[i].callback && elList[i].callback();
            }
        }
    }, 200)
    function getStyle(ele,attr){
        if(window.getComputedStyle){
            return window.getComputedStyle(ele,null)[attr];
        }
        return ele.currentStyle[attr];
    }
    function isEqul(obj1, obj2) {
        let isEqul = true;
        for (var i in obj1) {
            if (obj1[i] !== obj2[i]) {
                isEqul = false;
            }
        }
        return isEqul;
    }
    return {
        bind,
        remove,
    }
})();

運(yùn)行使用

import DomSize from "@/assets/js/domsize"
 mounted() {
    this.init();
    DomSize.bind(this.$refs.container,  ()=> {
      this.listenResize()
    });
}

方法2: window.MutationObserver
https://blog.csdn.net/u010419337/article/details/81474311?utm_medium=distribute.pc_relevant.none-task-blog-title-4&spm=1001.2101.3001.4242

let MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver
    let element = document.querySelector('.xterm-screen')
    this.observer = new MutationObserver((mutationList) => {
      for (let mutation of mutationList) {
        // console.log(mutation)
      }
      let width = getComputedStyle(element).getPropertyValue('width')
      let height = getComputedStyle(element).getPropertyValue('height')
      if (width === this.recordOldValue.width && height === this.recordOldValue.height) return
      this.recordOldValue = {
        width,
        height
      }
      this.firedNum += 1
    })
    this.observer.observe(element, { attributes: true, attributeFilter: ['style'], attributeOldValue: true })
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • TextView 實(shí)現(xiàn)跟隨標(biāo)簽 一、前言 在設(shè)計(jì)中經(jīng)常出現(xiàn)一個(gè)長(zhǎng)度不確定的文本,后面或是前面跟隨一個(gè)或是多個(gè)標(biāo)簽。...
    FlyClound閱讀 747評(píng)論 0 0
  • 使用:參考crossWalk替換webView集成[https://blog.csdn.net/zwrlj527/...
    瑟聞風(fēng)傾閱讀 4,943評(píng)論 0 1
  • JS 篇 閉包 定義:函數(shù)內(nèi)部定義函數(shù),內(nèi)部函數(shù)持有外部函數(shù)參數(shù)作用 讀取函數(shù)內(nèi)部的變量 讓這些變量的值始終保持在...
    高小二的心情閱讀 313評(píng)論 0 0
  • 概念ThreadLocal是Java中一個(gè)用于線程內(nèi)部存儲(chǔ)數(shù)據(jù)的工具類。ThreadLocal是用來存儲(chǔ)數(shù)據(jù)的,線...
    波波維奇c閱讀 349評(píng)論 0 1
  • https://www.cnblogs.com/dreamsqin/p/10885038.html[https:/...
    Find_Your_Way閱讀 155評(píng)論 0 0

友情鏈接更多精彩內(nèi)容