Vue.js如何在调整大小时立即获取元素的宽度和高度?这是我的代码笔插图,请帮我修改它直到工作,谢谢!
let app = new Vue({
el: '#app',
data: {
boxs: [{
width: 100,
height: 100
},
{
width: 100,
height: 100
}
]
}
});
#app {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
}
.resize {
display: flex;
justify-content: center;
align-items: center;
margin: 5px;
width: 100px;
height: 100px;
overflow: hidden;
resize: both;
background-color: #C3E2CE;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.js"></script>
<div id="app">
<div v-for="box,key in boxs" class="resize">
{{ box.width }} x {{ box.height }}
</div>
</div>
原文由 Fan 发布,翻译遵循 CC BY-SA 4.0 许可协议
要获得响应实际调整大小操作的即时反馈,您可能想尝试使用
MutationObserver
。您可以将它附加到组件的ref
点并在那里监听突变。您可以在
mounted
函数中附加MutationObserver
。请务必在destroyed
函数中执行您需要的任何清理工作。