Using jQuery’s .resize() Method
jQuery's .resize() method is a cool way to have an event triggered or have a function executed when something is resized. For the purposes of this tutorial, we're going to show you how to use the .resize() method to trigger an alert method whenever the window is resized. You might be surprised at how easy this functionality is to add to any of your code.
In terms of syntax, the .resize() method is used in much the same was as any other jQuery method. Take a look at the snippet below to get a look for yourself:
$(window).resize(function(){ alert("The window has been resized!"); })
That's all it takes to use the resize method to trigger an alert message once the window has been resized. Feel free to use the code and change the alert message to say whatever you like. Just be aware that if you are going to use this code, the alert messages that will appear will likely be very annoying for the user, as an alert will pop up whenever the window changes size, even if it happens in one fluid motion. Making the window 50px less wide will send 50 alert messages. Still, the concept behind the .resize() method is pretty neat, and a lot of different functionalities can be achieved by using it.