Vue JS - How to get window size whenever it changes


Javascript Problem Overview

I'm new to vuejs but I was trying to get the window size whenever I resize it so that i can compare it to some value for a function that I need to apply depending on the screen size. I also tried using the watch property but not sure how to handle it so that's probably why it didn't work

  methods: {

    elem() {
     this.size = window.innerWidth;
     return this.size;
  mounted() {
    if (this.elem < 767){ //some code }

Javascript Solutions

Solution 1 - Javascript

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.

Solution 2 - Javascript

Simplest approach


import Vue from 'vue';
import VueWindowSize from 'vue-window-size';

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

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

Solution 3 - Javascript

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 {
        refreshScrollableArea: undefined,

> Note: if you are using getElementById with something like (ie: an element that is a child in this component), document.getElementById( 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;
            return this.draw(this.sketch);

Conclusion: I tested with and:


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 =;
const { offsetWidth, offsetHeight } = document.getElementById('app');
const t1 =;

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.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.

Solution 4 - Javascript

You can use this anywhere anytime

methods: {
    //define below method first.
    winWidth: function () {
        setInterval(() => {
            var w = window.innerWidth;
            if (w < 768) {
                this.clientsTestimonialsPages = 1
            } else if (w < 960) {
                this.clientsTestimonialsPages = 2
            } else if (w < 1200) {
                this.clientsTestimonialsPages = 3
            } else {
                this.clientsTestimonialsPages = 4
        }, 100);
mounted() {
    //callback once mounted


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsaraView Question on Stackoverflow
Solution 1 - JavascriptGoranView Answer on Stackoverflow
Solution 2 - JavascriptCalvintwrView Answer on Stackoverflow
Solution 3 - Javascriptagm1984View Answer on Stackoverflow
Solution 4 - JavascriptGayan SandamalView Answer on Stackoverflow