vue.js中window.onresize的超詳細使用方法
前言
最近做的項目老是涉及到大小屏切換,但是因為屏幕寬高不一樣的原因,老是要計算表格高度
window.onresize
:監(jiān)聽window
窗口變化,當窗口大小發(fā)生變化時,會觸發(fā)此事件
含義
MDN中的定義是這樣子的:
文檔視圖調(diào)整大小時會觸發(fā) resize事件。
在js中使用
window.onresize = function(){ // todo event }
在html中使用
<body onresize="myFunction()">
在vue中的使用
需要注意的是,this在函數(shù)中指的是window,而不是vue實例
mounted(){ const _this = this window.onresize = function(){ _this.width = document.body.clientWidth // todo event } }
需要注意的兩點:
1、this
在函數(shù)中不可用,他在函數(shù)中不一定指全局上下文
解決辦法如下:
const _this = this window.onresize = function(){ _this.width = document.body.clientWidth }
2、在谷歌瀏覽器中,window.onresize
會觸發(fā)兩次,網(wǎng)上說是谷歌瀏覽器的bug
解決辦法如下,設定一個標識
let flag = true window.onresize = function () { if (flag) { console.log(new Date(), '窗口改變了') flag = false } let timeId = setTimeout(() => { flag = true timeId = null // 清除延時定時器 }, 1000) }
沒使用flag之前
使用之后,如下圖,控制臺只打印了一遍
注意在項目中的使用
1、window.onresize
只能在一個組件中使用,如果多個組件調(diào)用則會出現(xiàn)覆蓋情況,所以我的解決方案是在App.vue中使用,獲取document.documentElement.clientWidth
(即瀏覽器寬度)存放在vuex中,別的組件只需要用computed
(計算屬性)將vuex
的clientWidth
獲取,然后通過watch監(jiān)聽clientWidth的值,即可觸發(fā)組件事件
2、由于window.onresize
是全局事件,在其他頁面改變界面時也會執(zhí)行,這樣可能會出現(xiàn)問題,需要在出這個界面時注銷window.onresize
事件。
created() { this.$bus.$on('resize', this.$_setTableHeight) window.onresize = function () { console.log(new Date(), '窗口改變了') } }, beforeDestroy() { this.$bus.$off('resize', this.$_setTableHeight) window.onresize = null },
注銷之后,切換到其他頁面,控制臺就不會輸出以下信息
window.addEventListener
mounted() { this.$nextTick(() => { this.onDrawLine() window.addEventListener('resize', this.resize()) }) }, beforeDestroy() { console.log('刪除了') // 具名函數(shù)使用removeEventListener清除,但是匿名函數(shù)不行 window.removeEventListener('resize', this.resize()) },
性能優(yōu)化
window.onresize
在監(jiān)聽窗口變化時,固然起到很好的效果,但是對于網(wǎng)頁性能消耗過大。因為html
中每個標簽的變化,都會觸發(fā)window.onresize
事件,比如顯示/隱藏某個抽屜、添加/刪除某個div等等,很有可能會造成循環(huán)觸發(fā)和無限制觸發(fā),于是新推出了另外一個事件**ResizeObserver(對element和svgelement元素進行監(jiān)聽)**
MDN定義如下:
ResizeObserver
避免了通過回調(diào)函數(shù)調(diào)整大小時,通常創(chuàng)建的無限回調(diào)循環(huán)和循環(huán)依賴項。它只能通過在后續(xù)的幀中處理 DOM 中更深層次的元素來做到這一點。如果它的實現(xiàn)遵循規(guī)范,則應在繪制前和布局后調(diào)用 resize 事件。
MDN示例:https://mdn.github.io/dom-examples/resize-observer/resize-observer-text.html
部分源碼如下:
const h1Elem = document.querySelector('h1'); const pElem = document.querySelector('p'); const divElem = document.querySelector('body > div'); const slider = document.querySelector('input[type="range"]'); const checkbox = document.querySelector('input[type="checkbox"]'); divElem.style.width = '600px'; slider.addEventListener('input', () => { divElem.style.width = `${slider.value}px`; }) const resizeObserver = new ResizeObserver((entries) => { for (const entry of entries) { if (entry.contentBoxSize) { // Firefox implements `contentBoxSize` as a single content rect, rather than an array const contentBoxSize = Array.isArray(entry.contentBoxSize) ? entry.contentBoxSize[0] : entry.contentBoxSize; h1Elem.style.fontSize = `${Math.max(1.5, contentBoxSize.inlineSize / 200)}rem`; pElem.style.fontSize = `${Math.max(1, contentBoxSize.inlineSize / 600)}rem`; } else { h1Elem.style.fontSize = `${Math.max(1.5, entry.contentRect.width / 200)}rem`; pElem.style.fontSize = `${Math.max(1, entry.contentRect.width / 600)}rem`; } } console.log('Size changed'); }); resizeObserver.observe(divElem); checkbox.addEventListener('change', () => { if (checkbox.checked) { resizeObserver.observe(divElem); } else { resizeObserver.unobserve(divElem); } });
副作用:兼容性不強,有些瀏覽器兼容,具體情況見Can I Use
參考鏈接:
- https://www.cnblogs.com/yxysuanfa/p/6878016.html
- http://www.dbjr.com.cn/article/245030.htm
- https://developer.mozilla.org/zh-CN/docs/Web/API/ResizeObserver
總結
到此這篇關于vue.js中window.onresize的超詳細使用方法的文章就介紹到這了,更多相關window.onresize使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解vue項目打包后通過百度的BAE發(fā)布到網(wǎng)上的流程
這篇文章主要介紹了將vue的項目打包后通過百度的BAE發(fā)布到網(wǎng)上的流程,主要運用的技術是vue+express+git+百度的應用引擎BAE。需要的朋友可以參考下2018-03-03vueCli?4.x升級5.x報錯:Progress?Plugin?Invalid?Options的解決方法
本文主要介紹了vueCli?4.x升級5.x報錯:Progress?Plugin?Invalid?Options的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-01-01Vue3實現(xiàn)動態(tài)高度的虛擬滾動列表的示例代碼
虛擬滾動列表是一種優(yōu)化長列表渲染性能的技術,通過只渲染可視區(qū)域內(nèi)的列表項,減少DOM的渲染數(shù)量,本文就來介紹一下Vue3實現(xiàn)動態(tài)高度的虛擬滾動列表的示例代碼,具有一定的參考價值,感興趣的可以了解一下2025-01-01Vue.set()和this.$set()使用和區(qū)別
我們發(fā)現(xiàn)Vue.set()和this.$set()這兩個api的實現(xiàn)原理基本一模一樣,那么Vue.set()和this.$set()的區(qū)別是什么,本文詳細的介紹一下,感興趣的可以了解一下2021-06-06vue-cli項目代理proxyTable配置exclude的方法
今天小編就為大家分享一篇vue-cli項目代理proxyTable配置exclude的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09