Count the number of times the browser window is resized:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>/*from w w w . ja v a 2s . c om*/ <script> let x = 0; $(document).ready(function(){ $(window).resize(function(){ $("span").text(x += 1); }); }); </script> </head> <body> <p>Window resized <span>0</span> times.</p> <p>Try resizing your browser window.</p> </body> </html>
The resize event occurs when the browser window changes size.
The resize()
method triggers the resize event.
The resize()
method runs a function when a resize event occurs.
Trigger the resize event for the selected elements:
$(selector).resize()
Attach a function to the resize event:
$(selector).resize(function)
Parameter | Optional | Description |
---|---|---|
function | Optional. | the function to run when the resize event is triggered |