jQuery Document/Window Events

When the user resizes or scrolls the browser window then the events triggers. Here are some jQuery methods to handle the document/ window events:

1) The ready() Method

JQuery ready() method is used to specify when a ready event occur. Ready even occurs after the document is ready. Syntax:
$(document).ready(function)

Example:

<!DOCTYPE html>  
<html>  
<head>  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
<script>  
$(document).ready(function(){  
    $("button").click(function(){  
        $("h1").slideToggle();  
    });  
});  
</script>  
</head>  
<body>  
<h1> This is the heading</h1>  
<p>This is the paragraph.</p>  
<button>Toggle between slide down and slide up for a p element.</button>  
</body>  
</html>
Try Now

2) The resize() Method

An event handler function is attached to an window element by the resize() method and that function is executed whenever the size of browser window changes. Syntax:
$(selector).resize()

Example:

<!DOCTYPE html>  
<html>  
<head>  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
<script>  
 x = 0;  
$(document).ready(function(){  
    $(window).resize(function(){  
        $("span").text(x += 1);  
    });  
});  
</script>  
</head>  
<body>  
<h2>Heading 1</h2>  
<p>This is the first paragraph.<span>0</span> times.</p>  
<p>Window resized <span>0</span> times.</p>  
<p>Try resizing your browser window.</p>  
</body>  
</html>
Try Now

3) The scroll() Method

The scroll event of jQuery occurs when the user scrolls in the specified element and then the scroll() method triggers the scroll event, or attaches a function to run.
$(selector).scroll()

Example:

<!DOCTYPE html>  
<html>  
<head>  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
<script>  
x = 0;  
$(document).ready(function(){  
    $("div").scroll(function(){  
        $("span").text( x+= 1);  
    });  
});  
</script>  
</head>  
<body>  
<p>Try the scrollbar in the div</p>  
<div style="border:1px solid black;width:200px;height:100px;overflow:scroll;">   
If you cannot do great things, do small things in a great way.
Love has no age, no limit; and no death.</div>  
<p>Scrolled <span>0</span> times </p>  
</body>  
</html> 
Try Now

Related Topics

jQuery Tutorial

jQuery tutorial provides deep knowledge of jQuery technology for both beginners and professionals. In this jQuery tutorial you will learn about example, selectors, traversing, events, effects, jQuery fundamentals, CSS and attributes....

3 minutes read.

Introduction to DOM manipulation

Manipulation of DOM is one of the most important aspects of jQuery and thereby JavaScript. DOM stands for Document Object Model. It is a manipulation of interacting and representing with...

1 minute read.

jQuery Stop Effect

The jQuery stop() method is used to stop the jQuery animation or effect before it is finished. The jQuery stop() method works for all jQuery effects like: custom animations, fading and...

1 minute read.

jQuery Get()/Post() Methods

The jQuery get() and post() methods is used to send a HTTP request to a page and get the result back from the web server. GET and POST are the two...

3 minutes read.

jQuery Events

jQuery event is used to show a moment when something happens. jQuery is used to set up event-driven responses on the HTML page elements. These events have often triggered by the...

1 minute read.

jQuery Example

Hello, world! Example Here is the jQuery first example: <div id="divTest1"></div>   <script type="type/javascript">   $("#divTest1").text("Hello, world!");   </script> The above code have a div tag with the id of "divTest1" and $ shortcut to access jQuery. In this code select all...

2 minutes read.

jQuery Syntax

jQuery syntax is made by using HTML elements. You can perform some action on those elements. jQuery starts with the dollar sign ($) and ends with a semicolon (;). Basic syntax...

1 minute read.

jQuery Animation Effect

The jQuery animate() method is a way to create custom animations. The jQuery animate() method is used to animate numeric properties of CSS. Syntax: $(selector).animate({params}, speed, callback); speed: Speed is an optional parameter. It is used...

1 minute read.

jQuery Mouse Events

When the users move the mouse pointer or click some element then the mouse event fires. To handle the mouse events here are some jQuery methods: 1) The click() Method An event...

2 minutes read.

jQuery hide/show Effect

jQuery provides a interface for doing various kind of amazing effects like Slide, Fade, Hide, Show, Toggle Effects and Animate. jQuery hide() and show() Using the jQuery show() and hide() methods, you...

2 minutes read.

How to use jQuery

If you want to use jQuery then firstly include it on the pages where you wish to take advantage of it. To do this you have to download the jQuery....

2 minutes read.

jQuery CSS() Method

Using the jQuery css() method, you can set or return the one or more style properties for the selected elements. Here are the two ways to provide jQuery css() method: Set a...

2 minutes read.

jQuery Selectors

When you use the JavaScript then read and modify the content of the page is the very common task. While doing this you want to find the elements that you...

3 minutes read.

jQuery each() method

jQuery each() method: jQuery contains many processes that make coding a much better and faster work for professionals.  The each() method is one of the earliest method of jQuery. It...

3 minutes read.

jQuery Keyboard Events

When the user press or release a key on the keyboard then the keyboard event fires. Here are some jQuery methods to handle the keyboard events. 1) The keypress() Method: When a keyboard...

2 minutes read.

jQuery Document/Window Events

When the user resizes or scrolls the browser window then the events triggers. Here are some jQuery methods to handle the document/ window events: 1) The ready() Method JQuery ready() method is...

2 minutes read.

jQuery load() Method

jQuery load() method is a powerful AJAX method used for loading data asynchronously. The load() method loads HTML or text content from a server and added into a DOM element. Syntax: $(selector).load(URL,data,callback); Here is...

2 minutes read.

jQuery Add

Using the jQuery, it is easy to add new content. Here are four jQuery methods to add new content: append() prepend() after() before() jQuery append() Method The jQuery append() method is used to insert the content...

4 minutes read.

Getting and setting content

Retrieving and setting text, values and HTML is the simplest aspect of manipulation of DOM. It seems same but they’re not. There are text(), html() and val() methods. jQuery html() method Using the...

4 minutes read.

jQuery Fading Effect

jQuery Fade Using jQuery, you can fade elemnts in and out of visibility. You can change the opacity level of elements from invisible state to visible state or visible state to...

6 minutes read.