Open In App

How to run a code on scroll event in jQuery?

Last Updated : 20 Dec, 2021
Like Article

In this article, we will discuss how to run the code when users scroll the content using jQuery. To run the code on scrolling the content, the scroll() method is used. The scroll() method is used to run the code when the user scrolls in the specified element.



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.

Approach: In the below example, we have created a div element that contains the content of the element and we have used some CSS styles on the div element. We have added an overflow-y property to scroll to make content scrollable. When the user scrolls the div content, the jQuery scroll() method is called and this method contains css() method that applies some CSS styles on the div element.



<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
        How to run a code on scroll event using jQuery?
    <script src=
        h3 {
            text-align: center;
        .GFG {
            width: 350px;
            height: 150px;
            border: 1px solid black;
            overflow-x: hidden;
            overflow-y: scroll;
            text-align: justify;
            display: flex;
            margin: 0 auto;
    <h1 style="color: green;">
        How to run a code on scroll event using jQuery?
    <div class="GFG">
        HTML stands for HyperText Markup Language. It
        is used to design web pages using a markup
        language. HTML is the combination of Hypertext
        and Markup language. Hypertext defines the link
        between the web pages. A markup language is
        used to define the text document within tag
        which defines the structure of web pages. This
        language is used to annotate (make notes for
        the computer) text so that a machine can
        understand it and manipulate text accordingly.
        Most markup languages (e.g. HTML) are
        human-readable. The language uses tags to
        define what manipulation has to be done on
        the text.
        $(document).ready(function () {
            $(".GFG").scroll(function () {
                    "fontSize": "18px", 
                    "color": "green"


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads