javascript change background color on scroll

How we can change background color of the webpage on scrolling? Solution: See this JavaScript Change Background Color On Scroll, CSS Changing BG Color.

Previously I have shared gradient background animation using CSS, but this program changing background color on scroll. Basically, a website contains many sections on a single page and this type of program help to change the background color on section change. Suppose you scrolling up or down when a section will end and start a new, then the color will change.

Today you will learn to create CSS Changing BG Color. Basically there are 5 different sections when you will scroll down sections will be changed. When you will reach another section by scrolling there background color will change. In short, this program is changing the background color on scroll up or down.

So, Today I am sharing JavaScript Change Background Color On Scroll. There I have used jQuery, and as we know jQuery is a JS library that’s why I am putting this post in the JavaScript category. And in the program JS part changing the color on the scroll, CSS has very little part which is only for positioning. This program will very useful for you, and also you can use it on your website.

If you are thinking now how this BG color changing program actually is, then see the preview given below.

Preview Of CSS Changing BG Color

See this video preview to getting an idea of how this program looks like.

Live Demo

Now you can see this visually, you also can see it live by pressing the button given above. If you like this, then get the source code of its.

You May Also Like:

JavaScript Change Background Color On Scroll Source Code

Before sharing source code, let’s talk about it. First, I have created 5 article sections using HTML <article> tag. Inside every article tag I have placed a unique id name serial-wise like one, two, three. Also there I have used HTML Data-* element to store color values, The Data-* attribute is used for storing custom data (info).

Now using CSS I have placed all the elements in the right place, as you can see in the preview. There I have linked a google font using CSS @import command. In the CSS file, I have placed a background color which is also on the first article tag and put a transition delay for good visual. And I gave the height of each article 100vh, it means 100% height of device screen. Other basic works I have done using CSS like margin, padding, align, etc.

jQuery handling the main feature of this program. JS detecting the scroll position by identifying the article’s ID name and put the color value which I have stored in the HTML file (using Data-* attribute). For detecting the position according to ID I have used $(document).scrollTop() command. Left all other things you will understand after getting the codes, I can’t explain all in writing.

For creating this program you have to create 3 files. First for HTML, second for CSS, and the third for JavaScript. Follow the steps to creating this without any error.

index.html

Create an HTML file named ‘index.html‘ and put these codes given below.

style.css

Now create a CSS file named ‘style.css‘ and put these codes given here.

function.js

The last step, create a JavaScript file named ‘function.js‘ and put the codes.

That’s It. Now you have successfully created JavaScript Change Background Color On Scroll, CSS Changing BG Color. If you have any doubt or question comment down below.

Thanks For Visiting, Keep Visiting.

3 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here