Open In App

How to Create a Change Background on Scroll using HTML CSS and JavaScript ?

Last Updated : 22 Jul, 2024
Comments
Improve
Suggest changes
1 Like
Like
Report

In this article, we will try to change the background color of the container once the scroll occurs. If a user scrolls down the page the background color will be changed. We will use the below approach to accomplish this task.

Using the scroll event on the window object

In this approach, we will use the scroll event on the window object and pass a callback function to add a new class with a different background color.

Syntax:

$(window).scroll(function(){});

Example: The below example will explain the use of the scroll event to change the background color on scrolling the page.

Output:

Example 2: The below code example uses the above approach to add a background image on window scroll.

Output:


Next Article

Similar Reads