js dynamically monitors the height changes of dom elements.
related articles
- Reference article @ALISONLY
- Reference article @缇茶makewinkk
related information
- window.onsize = function(){}
Disadvantages: It can only monitor changes in the viewport but not changes in dom elements.- Advantages of MutationObserver
: MutationObserver can be used to monitor changes in the entire DOM.- Disadvantages of ResizeObserver
: Poor compatibility.
<script>
export default {
methods: {
handleResize() {
console.log("handle resize");
}
},
mounted() {
const dom = this.$refs.target.$el; // 假设this.$refs.target返回是VueComponent对象
this.observer = new ResizeObserver(this.handleResize);
this.observer.observe(dom, {
box: "border-box" });
},
beforeDestroy() {
this.observer.disconnect();
}
};
</script>
Related dependencies
- v-resize-observer
document address @document- vue-resize