vue监听页面宽度高度

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vue监听页面宽度高度相关的知识,希望对你有一定的参考价值。

参考技术A 监听窗口变化:window.onresize
JS的一些方法:
网页可见区域宽:document.body.clientWidth
网页可见区域高:document.body.clientHeight
网页可见区域宽:document.body.offsetWidth (包括边线的宽)
网页可见区域高:document.body.offsetHeight (包括边线的宽)

data ()
return
screenWidth: document.body.clientWidth

,
mounted ()
const that = this
window.onresize = () =>
return (() =>
window.screenWidth = document.body.clientWidth
that.screenWidth = window.screenWidth
)()


watch:
screenWidth(val)
// 为了避免频繁触发resize函数导致页面卡顿,使用定时器
if(!this.timer)
// 一旦监听到的screenWidth值改变,就将其重新赋给data里的screenWidth
this.screenWidth = val
this.timer = true
let that = this
setTimeout(function()
// 打印screenWidth变化的值
console.log(that.screenWidth)
that.timer = false
,400)


以上是关于vue监听页面宽度高度的主要内容,如果未能解决你的问题,请参考以下文章

Vue中 引入使用 element-resize-detector 监听 Dom 元素 宽度高度 变化

vue iframe嵌套页面高度自适应 (ios 宽度扩大的bug , ios展示比例问题)

包含 Vue.js 后,图像宽度和高度始终为 0 [关闭]

HTML,VUE,嵌入iframe,实现iframe的100%高度和宽度,代码分享

React 监听子元素dom内容高度变化

设置控件高度,宽度等