Last updated: Apr 4, 2024
Reading time·4 min

To change the background color on scroll using JavaScript:
window.scrollY property to get the number of pixels that the
document is currently scrolled vertically.Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> body { margin: 100px; height: 400vh; transition: all 0.5s ease; } h2 { position: fixed; } </style> </head> <body> <h2>bobbyhadz.com</h2> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
window.addEventListener('scroll', () => { const verticalScrollPx = window.scrollY || window.pageYOffset; if (verticalScrollPx < 500) { document.body.style.backgroundColor = 'red'; } else if (verticalScrollPx > 500 && verticalScrollPx < 1000) { document.body.style.backgroundColor = 'green'; } else { document.body.style.backgroundColor = 'lightblue'; } });

We
added a scroll event listener
to the window object.
window.addEventListener('scroll', () => { // ... })
The scroll event is triggered when the document view has been scrolled.
We used the window.scrollY property to get the number of pixels that the document is scrolled vertically.
const verticalScrollPx = window.scrollY || window.pageYOffset;
The
window.pageYOffset
property is an alias for the scrollY property that is supported in older
browsers.
The if statement checks if the user scrolled less than 500 pixels vertically.
If the condition is met, the background color of the body element gets set to
red.
if (verticalScrollPx < 500) { document.body.style.backgroundColor = 'red'; }
The else if statement checks if the user scrolled more than 500 pixels and
less than 1000 pixels.
else if (verticalScrollPx > 500 && verticalScrollPx < 1000) { document.body.style.backgroundColor = 'green'; }
Notice that we used the logical AND (&&) operator.
For the else if code block to run, both conditions have to be met.
Finally, the else block is run if the conditions in the if and else if
blocks aren't met.
else { document.body.style.backgroundColor = 'lightblue'; }
Note that you can add as many else if blocks as necessary.
window.addEventListener('scroll', () => { const verticalScrollPx = window.scrollY || window.pageYOffset; if (verticalScrollPx < 500) { document.body.style.backgroundColor = 'red'; } else if (verticalScrollPx > 500 && verticalScrollPx < 1000) { document.body.style.backgroundColor = 'green'; } else if ( (verticalScrollPx > 1000) & (verticalScrollPx < 1500) ) { document.body.style.backgroundColor = 'yellow'; } else { document.body.style.backgroundColor = 'lightblue'; } });

You might also have to change the background color on scroll based on sections of the page.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> body { margin: 100px; transition: all 0.5s ease; } .colored-box { height: 100vh; } h2 { position: fixed; } </style> </head> <body> <h2>bobbyhadz.com</h2> <div class="colored-box" data-color="red"></div> <div class="colored-box" data-color="green"></div> <div class="colored-box" data-color="yellow"></div> <div class="colored-box" data-color="lightblue"></div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const coloredBoxes = document.querySelectorAll('.colored-box'); window.addEventListener('scroll', () => { coloredBoxes.forEach(box => { if ( box.getBoundingClientRect().top <= document.body.scrollTop ) { document.body.style.backgroundColor = box.dataset.color; } }); });

We used the document.querySelectorAll()
method to select the div elements that have a class of colored-box.
In the scroll event handler function, we used the NodeList.forEach() method
to iterate over the collection of elements.
coloredBoxes.forEach(box => { if ( box.getBoundingClientRect().top <= document.body.scrollTop ) { document.body.style.backgroundColor = box.dataset.color; } });
The
getBoundingClientRect()
method returns a DOMRect object that provides information about the size of an
element and its position relative to the viewport.
The top property on the DOMRect object returns the top coordinate value.
If the top coordinate value of the div is less than or equal to the number of
pixels that the document is scrolled vertically, then the div is in the
viewport, so we set the background color of the body element.
Notice that each div has a data-color attribute.
<div class="colored-box" data-color="red"></div> <div class="colored-box" data-color="green"></div> <div class="colored-box" data-color="yellow"></div> <div class="colored-box" data-color="lightblue"></div>
You can access these attributes on the dataset object in your JavaScript code.
document.body.style.backgroundColor = box.dataset.color;
The data- part is omitted when accessing properties via the dataset object.
Another common thing you might have to do is to change the background color of a fixed navigation bar after scrolling.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> body { height: 500vh; } #navbar-fixed { position: fixed; font-size: 2em; border: 2px solid black; width: 100%; height: 50px; } #navbar-fixed.scrolled { background-color: yellowgreen; transition: background-color 0.5 ease; } </style> </head> <body> <div id="navbar-fixed">Navbar - bobbyhadz.com</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const navbar = document.getElementById('navbar-fixed'); const navbarHeightPx = 50; window.addEventListener('scroll', () => { if ( document.body.scrollTop > navbarHeightPx || document.documentElement.scrollTop > navbarHeightPx ) { navbar.classList.add('scrolled'); } else { navbar.classList.remove('scrolled'); } });

We set the position CSS property of the navbar to fixed, so the element
follows the user as they scroll.
#navbar-fixed { position: fixed; font-size: 2em; border: 2px solid black; width: 100%; height: 50px; }
If the document's content is scrolled vertically more than the navbar's height,
then we use the
classList.add() method to add
the scrolled class to the element.
Otherwise, we use the classList.remove() method to remove the class from the element.
The scrolled class simply sets a background color with a transition.
#navbar-fixed.scrolled { background-color: yellowgreen; transition: background-color 0.5 ease; }
You can learn more about the related topics by checking out the following tutorials: