Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
136
rated 0 times [  142] [ 6]  / answers: 1 / hits: 154443  / 14 Years ago, tue, june 8, 2010, 12:00:00

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


More From » jquery

 Answers
31

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


[#96563] Thursday, June 3, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
matteo

Total Points: 81
Total Questions: 100
Total Answers: 96

Location: Honduras
Member since Sat, Jul 24, 2021
3 Years ago
;