Vue監(jiān)測一個(gè)div寬度變化的幾種方法匯總
在 Vue 中監(jiān)測一個(gè) div 的寬度變化,可以使用以下幾種方法,主要結(jié)合 ResizeObserver 或其他方式來實(shí)現(xiàn)動(dòng)態(tài)監(jiān)聽。以下是具體實(shí)現(xiàn)方案:
方法 1:使用 ResizeObserver
ResizeObserver 是現(xiàn)代瀏覽器提供的 API,專門用于監(jiān)聽元素尺寸變化。它性能高效,適合動(dòng)態(tài)監(jiān)測 div 的寬度變化。
<template>
<div ref="targetDiv" class="target-div">
這是一個(gè)可調(diào)整大小的 div
</div>
</template>
<script>
export default {
data() {
return {
divWidth: 0,
};
},
mounted() {
// 創(chuàng)建 ResizeObserver 實(shí)例
const observer = new ResizeObserver((entries) => {
for (let entry of entries) {
// 獲取 div 的寬度
this.divWidth = entry.contentRect.width;
console.log('Div 寬度變化:', this.divWidth);
}
});
// 監(jiān)聽目標(biāo) div
observer.observe(this.$refs.targetDiv);
// 組件銷毀時(shí)清理 observer
this.$on('hook:beforeDestroy', () => {
observer.disconnect();
});
},
};
</script>
<style>
.target-div {
width: 200px;
height: 100px;
background: lightblue;
resize: horizontal; /* 允許水平拖動(dòng)調(diào)整大小 */
overflow: auto;
}
</style>
說明:
ResizeObserver會(huì)在 div 尺寸變化時(shí)觸發(fā)回調(diào),獲取最新的寬度。- 使用
this.$refs.targetDiv獲取 DOM 元素。 - 在組件銷毀時(shí)調(diào)用
observer.disconnect()清理監(jiān)聽,避免內(nèi)存泄漏。 resize: horizontal是 CSS 屬性,方便測試寬度調(diào)整(需要配合overflow: auto)。
方法 2:結(jié)合 Vue 的 watch 監(jiān)聽動(dòng)態(tài)寬度
如果 div 的寬度是由響應(yīng)式數(shù)據(jù)(如 style 或計(jì)算屬性)控制的,可以通過 watch 監(jiān)聽相關(guān)數(shù)據(jù)的變化。
<template>
<div :style="{ width: divWidth + 'px' }" class="target-div">
寬度: {{ divWidth }}px
</div>
</template>
<script>
export default {
data() {
return {
divWidth: 200,
};
},
watch: {
divWidth(newWidth) {
console.log('Div 寬度變化:', newWidth);
},
},
};
</script>
<style>
.target-div {
height: 100px;
background: lightcoral;
}
</style>
說明:
- 適用于寬度由 Vue 響應(yīng)式數(shù)據(jù)驅(qū)動(dòng)的場景。
- 如果寬度變化是由外部(如用戶拖動(dòng)或 CSS)引起的,這種方法不適用。
方法 3:使用 window resize 事件(間接監(jiān)測)
如果 div 的寬度變化與窗口大小相關(guān)(例如百分比寬度),可以監(jiān)聽 window 的 resize 事件。
<template>
<div ref="targetDiv" class="target-div">
這是一個(gè)寬度隨窗口變化的 div
</div>
</template>
<script>
export default {
data() {
return {
divWidth: 0,
};
},
methods: {
updateWidth() {
this.divWidth = this.$refs.targetDiv.offsetWidth;
console.log('Div 寬度:', this.divWidth);
},
},
mounted() {
this.updateWidth(); // 初始化寬度
window.addEventListener('resize', this.updateWidth);
// 清理事件監(jiān)聽
this.$on('hook:beforeDestroy', () => {
window.removeEventListener('resize', this.updateWidth);
});
},
};
</script>
<style>
.target-div {
width: 50%; /* 寬度隨窗口變化 */
height: 100px;
background: lightgreen;
}
</style>
說明:
- 適合 div 寬度依賴窗口大小的場景(如
width: 50%)。 - 使用
offsetWidth獲取 div 的實(shí)際寬度。 - 注意清理事件監(jiān)聽以防止內(nèi)存泄漏。
方法 4:使用第三方庫(如 element-resize-detector)
如果需要兼容舊瀏覽器或更復(fù)雜的場景,可以使用第三方庫如 element-resize-detector。
安裝庫:
npm install element-resize-detector
在 Vue 組件中使用:
<template>
<div ref="targetDiv" class="target-div">
這是一個(gè)可調(diào)整大小的 div
</div>
</template>
<script>
import elementResizeDetectorMaker from 'element-resize-detector';
export default {
data() {
return {
divWidth: 0,
};
},
mounted() {
const erd = elementResizeDetectorMaker();
erd.listenTo(this.$refs.targetDiv, (element) => {
this.divWidth = element.offsetWidth;
console.log('Div 寬度變化:', this.divWidth);
});
// 清理監(jiān)聽
this.$on('hook:beforeDestroy', () => {
erd.removeAllListeners(this.$refs.targetDiv);
});
},
};
</script>
<style>
.target-div {
width: 200px;
height: 100px;
background: lightyellow;
resize: horizontal;
overflow: auto;
}
</style>
說明:
element-resize-detector提供了跨瀏覽器兼容的尺寸變化監(jiān)聽。- 適合不支持
ResizeObserver的舊瀏覽器。
推薦方案
- 首選
ResizeObserver:現(xiàn)代、性能高、代碼簡潔,適合大多數(shù)場景。 - 如果 div 寬度由響應(yīng)式數(shù)據(jù)控制,使用
watch。 - 如果寬度與窗口大小相關(guān),使用
window resize事件。 - 如果需要兼容舊瀏覽器,考慮
element-resize-detector。
注意事項(xiàng)
- 性能:避免在大量元素上綁定監(jiān)聽,可能導(dǎo)致性能問題。
- 清理:總是清理
ResizeObserver、事件監(jiān)聽或第三方庫的綁定,防止內(nèi)存泄漏。 - 瀏覽器兼容性:
ResizeObserver在現(xiàn)代瀏覽器(Chrome 64+、Firefox 69+ 等)支持良好,舊瀏覽器需 polyfill 或使用第三方庫。
以上就是Vue監(jiān)測一個(gè)div寬度變化的幾種方法匯總的詳細(xì)內(nèi)容,更多關(guān)于Vue監(jiān)測div寬度變化的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue實(shí)現(xiàn)定時(shí)刷新數(shù)據(jù),每隔5分鐘執(zhí)行一次
這篇文章主要介紹了vue實(shí)現(xiàn)定時(shí)刷新數(shù)據(jù),每隔5分鐘執(zhí)行一次問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
vuedraggable實(shí)現(xiàn)簡單拖拽功能
這篇文章主要為大家詳細(xì)介紹了vuedraggable實(shí)現(xiàn)拖拽功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
vue使用threeJs導(dǎo)入obj模型并實(shí)現(xiàn)添加標(biāo)注
這篇文章主要介紹了vue使用threeJs導(dǎo)入obj模型并實(shí)現(xiàn)添加標(biāo)注方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
vuex 實(shí)現(xiàn)getter值賦值給vue組件里的data示例
今天小編就為大家分享一篇vuex 實(shí)現(xiàn)getter值賦值給vue組件里的data示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Vue3+TypeScript+printjs實(shí)現(xiàn)標(biāo)簽批量打印功能的完整過程
最近在做vue項(xiàng)目時(shí)使用到了print-js打印,這里給大家分享下,這篇文章主要給大家介紹了關(guān)于Vue3+TypeScript+printjs實(shí)現(xiàn)標(biāo)簽批量打印功能的完整過程,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09

