views:

339

answers:

1

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

thanks

+9  A: 

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
It depends on the browser, when and how many resize events are fired: http://www.quirksmode.org/dom/events/resize.html
Chris Lercher
@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
@Nick: Yeah, the quirksmode page only covers FF until 3.1b2 - but that kind of shows, that it depends on the browser...
Chris Lercher
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.
Patrick
@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
@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
I've found a solution here: http://stackoverflow.com/questions/277759/html-onresizeend-event-or-equivalent-way-to-detect-end-of-resize
Patrick
@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
yeah i see it is not perfect, but still good
Patrick
@Nick: On Windows it even depends on the status of "show window content while moving it".
Steve Schnepp