138

Is there any way with jQuery or JavaScript to trigger a function when the user ends to resize the browser window?

In other terms:

  1. Can I detect mouse up event when user is resizing the browser window? Otherwise:
  2. Can I detect when a window resize operation is finished?

I'm currently only able to trigger an event when the user start to resize the window with jQuery

Teun Zengerink
  • 4,277
  • 5
  • 30
  • 32
aneuryzm
  • 63,052
  • 100
  • 273
  • 488

4 Answers4

244

You can use .resize() to get every time the width/height actually changes, like this:

$(window).resize(function() {
  //resize just happened, pixels changed
});

You can view a working demo here, it takes the new height/width values and updates them in the page for you to see. Remember the event doesn't really start or end, it just "happens" when a resize occurs...there's nothing to say another one won't happen.


Edit: By comments it seems you want something like a "on-end" event, the solution you found does this, with a few exceptions (you can't distinguish between a mouse-up and a pause in a cross-browser way, the same for an end vs a pause). You can create that event though, to make it a bit cleaner, like this:

$(window).resize(function() {
    if(this.resizeTO) clearTimeout(this.resizeTO);
    this.resizeTO = setTimeout(function() {
        $(this).trigger('resizeEnd');
    }, 500);
});

You could have this is a base file somewhere, whatever you want to do...then you can bind to that new resizeEnd event you're triggering, like this:

$(window).bind('resizeEnd', function() {
    //do something, window hasn't changed size in 500ms
});

You can see a working demo of this approach here

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 1
    It depends on the browser, when and how many resize events are fired: http://www.quirksmode.org/dom/events/resize.html – Chris Lercher Jun 08 '10 at 10:27
  • @chris_l: I'm not sure how accurate that page is anymore...open up the demo I posted in the latest Firefox, it's firing it every time the size changes here. I don't have Opera to test, it may still be different, but they're at least more consistent than quirksmode suggests, I'll send them a note this needs updating. – Nick Craver Jun 08 '10 at 10:31
  • @Nick: Yeah, the quirksmode page only covers FF until 3.1b2 - but that kind of shows, that it depends on the browser... – Chris Lercher Jun 08 '10 at 10:35
  • Here the point is that I want to know when a resize *ends* and not when it starts. Instead it is just triggered when it occurs, and what is worse is that the event is triggered several times instead of once. – aneuryzm Jun 08 '10 at 10:38
  • @chris_l: I completely agree it's definitely something to be aware of, just noting for others to not take that page as 100% accurate at the moment, I send a note to [ppk](http://www.quirksmode.org/about/) to see if he'll take a look and update it when he gets time. – Nick Craver Jun 08 '10 at 10:38
  • @Patrick: It "resized" all those times though, the size actually changed. If you want one re-size event you'll have to use a timer so it waits a specified amount of time before firing, and clears/resets if another event happens in-between if that's what you're after...there's not a built-in event like you describe. For example `mousemove` doesn't fire when the mouse stops for a minute, it fires every time the cursor changes position. – Nick Craver Jun 08 '10 at 10:40
  • I've found a solution here: http://stackoverflow.com/questions/277759/html-onresizeend-event-or-equivalent-way-to-detect-end-of-resize – aneuryzm Jun 08 '10 at 10:42
  • @Patrick: That's the *exact* approach I mentioned in my last comment :) Note thought it's just softening the same issue, if you pause for the duration of the timeout it'll still fire using the timeout as well...you can't ensure the user *won't resize again*, so as I mentioned before, there is no "end" event. – Nick Craver Jun 08 '10 at 11:19
  • @Nick: On Windows it even depends on the status of "show window content while moving it". – Steve Schnepp Jun 28 '10 at 16:00
  • 7
    I just want to drop by and says that this is also triggered when the user zooms in the page on mobile devices. – Hoffmann Aug 30 '13 at 14:26
  • 4
    window.addEventListener('resize', function(){}, true); – WebWanderer Nov 07 '14 at 16:54
  • @Hoffmann I had to upvote you for how useful that info was. I can do some pretty cool things now that I know that. – WebWanderer Nov 07 '14 at 16:55
21

Another way of doing this, using only JavaScript, would be this:

window.addEventListener('resize', functionName);

This fires every time the size changes, like the other answer.

functionName is the name of the function being executed when the window is resized (the brackets on the end aren't necessary).

James Skemp
  • 8,018
  • 9
  • 64
  • 107
James Douglas
  • 3,328
  • 2
  • 22
  • 43
  • 1
    This is the better option than the most approved answer! Jquery has its place, but if you don't already have it in the project, this is the solution. – Jason Jun 07 '22 at 19:00
7

This can be achieved with the onresize property of the GlobalEventHandlers interface in JavaScript, by assigning a function to the onresize property, like so:

window.onresize = functionRef;

The following code snippet demonstrates this, by console logging the innerWidth and innerHeight of the window whenever it's resized. (The resize event fires after the window has been resized)

function resize() {
  console.log("height: ", window.innerHeight, "px");
  console.log("width: ", window.innerWidth, "px");
}

window.onresize = resize;
<p>In order for this code snippet to work as intended, you will need to either shrink your browser window down to the size of this code snippet, or fullscreen this code snippet and resize from there.</p>
JSON C11
  • 11,272
  • 7
  • 78
  • 65
1

If You want to check only when scroll ended, in Vanilla JS, You can come up with a solution like this:

Super Super compact

var t
window.onresize = () => { clearTimeout(t) t = setTimeout(() => { resEnded() }, 500) }
function resEnded() { console.log('ended') }

All 3 possible combinations together (ES6)

var t
window.onresize = () => {
    resizing(this, this.innerWidth, this.innerHeight) //1
    if (typeof t == 'undefined') resStarted() //2
    clearTimeout(t); t = setTimeout(() => { t = undefined; resEnded() }, 500) //3
}

function resizing(target, w, h) {
    console.log(`Youre resizing: width ${w} height ${h}`)
}    
function resStarted() { 
    console.log('Resize Started') 
}
function resEnded() { 
    console.log('Resize Ended') 
}
Claudio Ferraro
  • 4,551
  • 6
  • 43
  • 78