JavaScript怎么实现窗体改变事件resize的操作?

如题所述

<script type="text/javascript">  

window.onload=function(){  

changeDivHeight();  

}  

//当浏览器窗口大小改变时,设置显示内容的高度  

window.onresize=function(){  

changeDivHeight();  

}  

function changeDivHeight(){               

var h = document.documentElement.clientHeight;//获取页面可见高度  

document.getElementById("div_ov_y").style.height=h-140+"px";

1.第一步: 先在 data 中去 定义 一个记录宽度是属性;

data: {

screenWidth: document.body.clientWidth   // 这里是给到了一个默认值 (这个很重要)

}

2.第二步: 我们需要讲 reisze 事件在 vue mounted 的时候去挂载一下它的方法;

mounted () {

const that = this

window.onresize = () => {

return (() => {

window.screenWidth = document.body.clientWidth

that.screenWidth = window.screenWidth

})()

}

}

3.第三步: watch 去监听这个 属性值的变化,如果发生变化则讲这个val 传递给 this.screenWidth。

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答