Vue 實(shí)時(shí)監(jiān)聽窗口變化 windowresize的兩種方法
下面給大家分享兩種方法來介紹Vue 實(shí)時(shí)監(jiān)聽窗口變化 windowresize,具體內(nèi)容如下所示:
方法一:
First-step : 定義變量
data(){
return{
formLabelWidth : '123px'
}
},
Second-step: 根據(jù)生命周期 在mounted 中綁定 窗口變化
mounted(){
const that = this
window.onresize = () => {
return (() => {
window.screenWidth = document.body.clientWidth
that.screenWidth = window.screenWidth
})()
}
},
Third-step: 綁定監(jiān)聽 watch
watch: {
screenWidth (val) {
if (!this.timer) {
this.screenWidth = val
this.timer = true
let that = this
setTimeout(function () {
// that.screenWidth = that.$store.state.canvasWidth
console.log(that.screenWidth)
// that.init()
that.timer = false
}, 400)
}
}
},
方法二:在vue.2x里面時(shí)候,mounted 里面可以直接掛載 window.onresize事件。全局監(jiān)聽
mounted(){
window.onresize = () => {
return (() => {
this.handleLableWidth();
})()
}
this.handleLableWidth();
},
完全可以做到檢測(cè)窗口變化
總結(jié)
以上所述是小編給大家介紹的Vue 實(shí)時(shí)監(jiān)聽窗口變化 windowresize的兩種方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue3的vite中圖片動(dòng)態(tài)加載3種方式
這篇文章主要給大家介紹了關(guān)于Vue3的vite中圖片動(dòng)態(tài)加載3種方式的相關(guān)資料,圖片進(jìn)入可視區(qū)域,進(jìn)行動(dòng)態(tài)加載圖片操作,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
ant-design-vue中設(shè)置Table每頁顯示的條目數(shù)量方式
這篇文章主要介紹了ant-design-vue中設(shè)置Table每頁顯示的條目數(shù)量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue+express+Socket實(shí)現(xiàn)聊天功能
這篇文章主要為大家詳細(xì)介紹了Vue+express+Socket實(shí)現(xiàn)聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
Mint UI組件庫CheckList使用及踩坑總結(jié)
這篇文章主要介紹了Mint UI組件庫CheckList使用及踩坑總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
vue監(jiān)聽瀏覽器原生返回按鈕,進(jìn)行路由轉(zhuǎn)跳操作
這篇文章主要介紹了vue監(jiān)聽瀏覽器原生返回按鈕,進(jìn)行路由轉(zhuǎn)跳操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09

