jQuery | scroll() with Examples

The scroll() is an inbuilt method in jQuery which is used to user scroll in the specified element. This method works for all scrollable elements and the browser window.

Syntax:

$(selector).scroll(function)

Parameter: This method accepts single parameters function which is optional. It is used to specify the function to run when the scroll event is triggered.
Return Value: It binds an event when scroll is happen for the specified element.

Below program illustrates the scroll() method in jQuery:

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>scroll method</title>
        <script src=
        </script>
          
        <!-- jQuery code to show the working of this method -->
        <script>
            $(document).ready(function() {
                $("div").scroll(function() {
                    alert("scroll happened");
                });
            });
        </script>
        <style>
            div {
                border: 1px solid black;
                width: 500px;
                height: 100px;
                overflow: scroll;
            }
        </style>
    </head>
    <body>
       
        <!-- scroll inside this div box -->
        <div>Welcome to GeeksforGeeks!
             Welcome to GeeksforGeeks!
             Welcome to GeeksforGeeks!
             Welcome to GeeksforGeeks!
             Welcome to GeeksforGeeks!
             Welcome to GeeksforGeeks!
             Welcome to GeeksforGeeks!
             Welcome to GeeksforGeeks!
             Welcome to GeeksforGeeks!
             Welcome to GeeksforGeeks!
             Welcome to GeeksforGeeks!
             Welcome to GeeksforGeeks!.
             Welcome to GeeksforGeeks!
        </div>
    </body>
</html>

chevron_right


Output:
After scrolling inside the div box.
scroll method



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.