监听元素宽高变化

·

1 min read

// 监听 div#contentTable 宽高变化
data(){
return {
recordOldValue:''
}
}
            changeStyle() {
                let MutationObserver = window.MutationObserver ||                 window.WebKitMutationObserver || window.MozMutationObserver
                let element = $("#contentTable").get(0)
                this.observer = new MutationObserver((mutationList) => {
                    let width = parseFloat(getComputedStyle(element).getPropertyValue('width'));
                    if (width === this.recordOldValue.width) {
                        return
                    }
                    this.recordOldValue = width
                    this.pageWidth = this.recordOldValue
                })
                this.observer.observe(element, { attributes: true, subtree: true, characterData: true, attributeFilter: ['style'], attributeOldValue: true })
            },