jqueryHow do I use jQuery to detect window resize events?
jQuery provides a resize()
event for detecting when the browser window has been resized. The resize()
event can be used on any element, but is most often used on the window
object.
Below is an example of using the resize()
event on the window
object.
$(window).resize(function() {
console.log('Window was resized');
});
When this code is run, the message Window was resized
will be logged to the console every time the browser window is resized.
Code explanation
$(window)
- This is a jQuery selector used to select the window object..resize()
- This is a jQuery method used to bind an event handler to theresize
event.function()
- This is the event handler function that will be called when theresize
event is triggered.console.log('Window was resized')
- This is the code that will be executed when theresize
event is triggered.
Helpful links
More of Jquery
- How do I use jQuery UI draggable?
- How can I use jQuery to manipulate HTML elements?
- How can I use jQuery to zoom an image when the user hovers over it?
- How can I get the y position of an element using jQuery?
- How can I use jQuery to check if an element is visible?
- How do I use jQuery to validate an email address?
- How do I use jQuery to zoom in on an image?
- How do I use the jQuery prop() method?
- How do I use jQuery ZTree to create a hierarchical tree structure?
See more codes...