Vue JS - How to get window size whenever it changes Vue JS - How to get window size whenever it changes vue.js vue.js

Vue JS - How to get window size whenever it changes


Put this code inside your Vue component:

created() {  window.addEventListener("resize", this.myEventHandler);},destroyed() {  window.removeEventListener("resize", this.myEventHandler);},methods: {  myEventHandler(e) {    // your code for handling resize...  }}

This will register your Vue method on component creation, trigger myEventHandler when the browser window is resized, and free up memory once your component is destroyed.


Simplest approach

https://www.npmjs.com/package/vue-window-size

Preview

import Vue from 'vue';import VueWindowSize from 'vue-window-size';Vue.use(VueWindowSize);

You would then access it normally from your components like this:

<template>  <div>    <p>window width: {{ windowWidth }}</p>    <p>window height: {{ windowHeight }}</p>  </div></template>


I looked at the code of that library vue-window-size, and besides the additional logic, it's just adding an event listener on window resize, and it looks like it can be instructed to debounce. Source

The critical problem for me is that my Vue SPA app does not emit a window resize event when a vue-router route changes that makes the <html> element go from 1000px to 4000px, so it's causing me all kinds of problems watching a canvas element controlled by p5.js to redraw a wallpaper using p5.resizeCanvas().

I have a different solution now that involves actively polling the page's offset height.

The first thing to be aware of is JavaScript memory management, so to avoid memory leaks, I put setInterval in the created lifecycle method and clearInterval in the beforeDestroy lifecycle method:

created() {    this.refreshScrollableArea = setInterval(() => {        const { offsetWidth, offsetHeight } = document.getElementById('app');        this.offsetWidth = offsetWidth;        this.offsetHeight = offsetHeight;    }, 100);},beforeDestroy() {    return clearInterval(this.refreshScrollableArea);},

As hinted in the above code, I also placed some initial state:

data() {    const { offsetWidth, offsetHeight } = document.querySelector('#app');    return {        offsetWidth,        offsetHeight,        refreshScrollableArea: undefined,    };},

Note: if you are using getElementById with something like this.id (ie: an element that is a child in this component), document.getElementById(this.id) will be undefined because DOM elements load outer-to-inner, so if you see an error stemming from the data instantiation, set the width/height to 0 initially.

Then, I put a watcher on offsetHeight to listen for height changes and perform business logic:

    watch: {        offsetHeight() {            console.log('offsetHeight changed', this.offsetHeight);            this.state = IS_RESET;            this.setState(this.sketch);            return this.draw(this.sketch);        },    },

Conclusion: I tested with performance.now() and:

document.querySelector('#app').offsetHeight document.getElementById('app').offsetHeightdocument.querySelector('#app').getClientBoundingRect().height

all execute in about the exact same amount of time: 0.2ms, so the above code is costing about 0.2ms every 100ms. I currently find that reasonable in my app including after I adjust for slow clients that operate an order of magnitude slower than my localmachine.

Here is the test logic for your own R&D:

const t0 = performance.now();const { offsetWidth, offsetHeight } = document.getElementById('app');const t1 = performance.now();console.log('execution time:', (t1 - t0), 'ms');

Bonus: if you get any performance issue due to long-running execution time on your setInterval function, try wrapping it in a double-requestAnimationFrame:

created() {    this.refreshScrollableArea = setInterval(() => {        return requestAnimationFrame(() => requestAnimationFrame(() => {            const { offsetWidth, offsetHeight } = document.getElementById(this.id);            this.offsetWidth = offsetWidth;            this.offsetHeight = offsetHeight;        }));    }, 100);},

requestAnimationFrame itself a person should research. I will leave it out of the scope of this answer.

In closing, another idea I researched later, but am not using is to use a recursive setTimeout function with a dynamic timeout on it (ie: a timeout that decays after the page loads); however, if you consider the recursive setTimeout technique, be conscious of callstack/function-queue length and tail call optimization. Stack size could run away on you.