js监听div元素的宽高变化, window.onresize是监听浏览器窗口的变化

		mounted() {
			let recordExecutionsNum = 0;
			let MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver
			let element = document.getElementById('asset-list-search')
			this.observer = new MutationObserver( (mutationList) => {
				for (let mutation of mutationList) {
					console.log('mutation', mutation)
				}
				let height = getComputedStyle(element).getPropertyValue('height')
				if (height === this.recordOldValue.height) return
				this.recordOldValue = {
					height
				}
				height = +height.replace('px', '')
				let clientHeight = document.getElementsByClassName('cardlayout-content')[0].clientHeight - 120
				this.height = clientHeight || 500 // 动态设置高度
				this.recordExecutionsNum += 1
			})
			this.observer.observe(element, { attributes: true, characterData: true })
		},

参考地址:js监听div元素的宽高变化
js监听div元素的宽高变化, window.onresize是监听浏览器窗口的变化_第1张图片

注意:一定是属性发生变化,比如 height: 100px; 变为 height: 200px,observer才会监听到,否则不生效

你可能感兴趣的:(javascript,开发语言,ecmascript)