---
title: "Change background color while scrolling the page via jQuery"  
description: "Change background color while scrolling the page via jQuery"  
author: "Ethan Karla"  
published: 2021-07-29  
updated: 2023-06-13  
canonical: https://www.mindstick.com/forum/156676/change-background-color-while-scrolling-the-page-via-jquery  
category: "jquery"  
tags: ["jquery"]  
reading_time: 3 minutes  

---

# Change background color while scrolling the page via jQuery

How to change [background color](https://www.mindstick.com/forum/12937/how-to-style-togglebutton-so-that-the-background-color-is-white) while scrolling the [page](https://www.mindstick.com/articles/13031/why-to-make-a-wikipedia-page) via jQuery?

\

## Replies

### Reply by Ravi Vishwakarma

To change the background color of a page while scrolling using jQuery, you can use the scroll event and modify the CSS properties of the body element. Here's an example:

```html
<!DOCTYPE html>
<html>
<head>
  <title>Change Background Color on Scroll</title>
  <style>
    body {
      height: 2000px; /* To create a scrollable area */
      transition: background-color 0.5s ease; /* Smooth transition effect */
    }
    .scrolling {
      background-color: #ff0000; /* Background color while scrolling */
    }
    .top {
      background-color: #ffffff; /* Default background color */
    }
  </style>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script>
    $(document).ready(function() {
      $(window).scroll(function() {
        var scrollTop = $(window).scrollTop();
        var triggerOffset = 200; // Change this value as per your requirement

        if (scrollTop > triggerOffset) {
          $('body').addClass('scrolling');
        } else {
          $('body').removeClass('scrolling');
        }
      });
    });
  </script>
</head>
<body class="top">
  <!-- Your page content goes here -->
</body>
</html>
```

### Reply by Aryan Kumar

Sure, you can use the `scroll` event to change the [background](https://www.mindstick.com/articles/65/how-to-change-background-color-of-tab-control-in-c-sharp) [color](https://www.mindstick.com/articles/77/how-to-split-form-background-color-in-c-sharp) of an element while scrolling the page via jQuery. The `scroll` event is triggered when the user scrolls the page. You can use this event to get the current scroll position of the page and then use this position to change the background color of the element.

Here is an example of how to change the background color of an element while scrolling the page via jQuery:

Code snippet

```plaintext
$(document).on('scroll', function() {
  // Get the current scroll position of the page
  var scrollPosition = $(document).scrollTop();

  // Change the background color of the element based on the scroll position
  if (scrollPosition < 500) {
    $('element').css('background-color', 'red');
  } else if (scrollPosition < 1000) {
    $('element').css('background-color', 'green');
  } else {
    $('element').css('background-color', 'blue');
  }
});
```

In this example, we are using the `on()` method to attach a listener to the `scroll` event. The `scroll` event is triggered when the user scrolls the page. When the `scroll` event is triggered, the `scrollPosition` variable will contain the current scroll position of the page. We can then use this value to change the background color of the element.

In this example, we are changing the background color of the element to red if the scroll position is less than 500 pixels. We are changing the background color of the element to green if the scroll position is between 500 and 1000 pixels. We are changing the background color of the element to blue if the scroll position is greater than 1000 pixels.

You can change the background color to any color you want. You can also use a gradient or a pattern for the background color. The possibilities are endless.


---

Original Source: https://www.mindstick.com/forum/156676/change-background-color-while-scrolling-the-page-via-jquery

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
