div宽度根据vuejs中的屏幕进行调整,但最初宽度设置为width.innerWidth函数



我已经将div的宽度设置为window.innerWidth,因为我必须动态设置它。现在我想让它根据屏幕宽度自动调整。例如,当我们调整开发人员选项时。

要监听屏幕更改,只需使用resize事件:

window.addEventListener('resize', () => {
document.getElementById('example-div').style.width = window.innerWidth + 'px';
});
data(){
return{
width:"";
}
},
created() {
window.addEventListener("resize", this.reSizer);
},
destroyed() {
window.removeEventListener("resize", this.reSizer);
},
methods: {
reSizer(e) {
// your code for handling resize...
this.width=window.innerWidth+"px";
}
}

最新更新