简体   繁体   English

在Vue.js中调整大小时如何立即获取元素的宽度和高度

[英]How to get the element width and height immediately when it resizing in Vue.js

How to get the element width and height immediately when it resizing in Vue.js? 在Vue.js中调整大小时如何立即获取元素的宽度和高度? Here is my code pen illustration please help me to change it till work,thanks! 这是我的密码笔插图,谢谢您的帮助,直到工作为止!

Codepen Codepen

 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> 

To get immediate feedback that responds to the actual resize action, you might want to try using a MutationObserver . 要获得响应实际调整大小操作的即时反馈,您可能想要尝试使用MutationObserver You can attach it to a ref point of your component and listen for mutations there. 您可以将其附加到组件的ref点,并在其中侦听突变。

You can attach the MutationObserver in the mounted function. 您可以在已mounted函数中附加MutationObserver Be sure to also do any cleanup that you need in the destroyed function. 确保还对destroyed函数进行了所需的任何清理。

 const Resizable = { template: "<div ref='main' @resize='onResize' class='resize'>{{dims.width}} | {{dims.height}}</div>", data() { return { dims: { width: null, height: null } }; }, mounted() { const { width, height } = this.$refs.main.getBoundingClientRect(); this.dims.width = width; this.dims.height = height; const mutationHandler = mutationList => { for (let mutation of mutationList) { if (mutation.type === "attributes") { const { width, height } = mutation.target.getBoundingClientRect(); this.dims.width = width; this.dims.height = height; } } }; const mo = new MutationObserver(mutationHandler); mo.observe(this.$refs.main, { attributes: true, childList: true, subtree: true }); }, methods: { onResize() { console.log("Resized"); } } }; const app = new Vue({ el: "#app", components: { "resizable": Resizable }, data() { return { items: [ "foo", "bar", "fizz" ] } } }); 
 body { background-color: #414141; } .container { display: flex; align-items: center; justify-content: center; } .resize { resize: both; margin: 5px; width: 100px; height: 100px; color: black; overflow: scroll; background-color: white; text-align: center; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.js"></script> <div id="app"> <div class="container"> <resizable v-for="item in items" :key="item" class="resize"></resizable> </div> </div> 

Forgive me, but I'm still learning Vue as well. 请原谅我,但我仍在学习Vue。 I'd suggest you do more modular approach and extract the boxes, then just loop how many you want. 我建议您使用更多的模块化方法并提取框,然后循环您想要的框数。 Please note that this is not best-practice, as the box w/h should probably be coming from props and data being loaded from the root element. 请注意,这不是最佳做法,因为w / h框可能应该来自props和从根元素加载的数据。

 const box = Vue.component("box", { template: '<div class="resize">{{ boxWidth }} x {{ boxHeight}}</div>', data() { return { boxWidth: 100, boxHeight: 100, }; }, mounted: function() { this.$el.addEventListener("mouseup", this.move); }, methods: { move(e) { if (e.target == this.$el) { this.boxWidth = parseInt(this.$el.style.width); this.boxHeight = parseInt(this.$el.style.height); } } } }); let app = new Vue({ el: "#app", components: { box: box }, }); 
 #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.5.2/vue.js"></script> <div id="app"> <div v-for="b in [0,1]"> <box></box> </div> </div> 

Easy way would be plain JS eventListener to change a local variable 简单的方法是将普通JS eventListener更改为局部变量

window.addEventListener('resize', this.getWindowWidth);

data() {
  return {
    windowWidth:0
  }
},
mounted () {
  this.$nextTick(function() {
    window.addEventListener('resize', this.getWindowWidth);
    this.getWindowWidth()
  })
},
methods: {
getWindowWidth() {
  this.windowWidth = document.documentElement.clientWidth
}
}

and don't forget to remove the eventListener on component destroy 并且不要忘记在销毁组件时删除eventListener

beforeDestroy() {
  window.removeEventListener('resize', this.getWindowWidth);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM