Background Color Change on Scroll

Share
Background color change on scroll

This effect changes the background color of the <body> when the user scrolls to different sections of a page — perfect for creating a fluid and immersive storytelling experience.

Add Classes and Attributes in Elementor

Step 1: Add the Class .panel

For each section where you want to change the background color, as well as the ones preceding and following it:

  1. Select your section in Elementor
  2. Go to Advanced → CSS Classes
  3. Add: panel

Step 2: Define the Color via an Attribute

Still in your section:

  1. Go to Advanced → Attributes
  2. Add a new attribute: Key: data-color Value: gray or blue or purple

Example: data-color|blue will apply the class .color-blue to the body when the section is visible.

Add Color Styles to your Site

Go to Appearance → Customize → Additional CSS, or in Elementor → Custom Code → Global CSS.

Add this code:

body {
    background-color: #101012;
    transition: background-color 1s ease;
}

.color-blue {
    background-color: #7dc4ea!important;
}

.color-gray {
    background-color: #222225!important;
}

.color-purple {
    background-color: #7300FF!important;
}

You can create as many colors as you want:
Declare a .color-xxx then use data-color|xxx in your section.

Add the jQuery Script in Elementor

  1. Go to Elementor → Custom Code
  2. Click Add New Code
  3. Choose position Footer – End of </body>
  4. Paste this code:
<script>
jQuery(function($){
    $(window).scroll(function() {

        var $window = $(window),
            $body = $('body'),
            $panel = $('.panel'); // Chaque section à détecter
        
        var scroll = $window.scrollTop() + ($window.height() / 2);

        $panel.each(function () {
            var $this = $(this);

            // Vérifie si la section est au milieu de l'écran
            if ($this.position().top <= scroll && $this.position().top + $this.height() > scroll) {

                // Supprime toutes les classes color-*
                $body.removeClass(function (index, css) {
                    return (css.match(/(^|\s)color-\S+/g) || []).join(' ');
                });

                // Ajoute la couleur liée à la section active
                $body.addClass('color-' + $(this).data('color'));
            }
        });

    }).scroll(); // Exécute au chargement
});
</script>

How Does it Work?

  • Each section with .panel is monitored.
  • When the scroll reaches the middle of this section, the script:
    • removes all current color-* classes from the body
    • adds color-gray, color-purple, etc.
  • The CSS then changes the background color of the site.

With the transition:

transition: background-color 1s ease;

The color changes smoothly, for a storytelling effect.

Tips & Customization

Add more Colors

Declare a new class:

.color-green {
    background-color: #1abc9c!important;
}

Then in Elementor:

data-color|green

Make the Change Faster or Slower

Modify in body:

transition: background-color 0.4s ease;

Change the Detection Area (Middle of Section)

In the JS:

var scroll = $window.scrollTop() + ($window.height() / 2);

Replace / 2 with:

  • / 3 → triggers earlier
  • / 1.5 → triggers later
Category
Compatible
WordPress, Elementor
Updated
14 Nov 2025

Built with Elementor

Host, create, and grow your dream website with the leading platform for WordPress.

Inspire your Elementor projects with 1 animation per week.

Thank you for subscribing!