Add a Color Cycling Background on Scroll in Divi

Written by Dan Mossop

One way to create an eye-catching design is by incorporating color cycling backgrounds into your Divi sections. This effect allows the background color of a section to change in response to the user's scroll, cycling through a spectrum of colors akin to a rainbow. Here's how to set up this effect.

Implement Color Cycling in Divi using jQuery and CSS

To apply this effect, we'll use a combination of jQuery and CSS. The setup includes adding a specific CSS class to your section, and then integrating some custom code into the Divi theme.

Step 1: Add a CSS Class

Begin by assigning a CSS class to the section you want to animate. In the Divi Builder, open the section settings and enter the class name db-color-cycle-on-scroll in the CSS Class field.

Step 2: Insert Custom CSS and jQuery

Next we'll incorporate a small piece of code into your Divi theme to handle the color cycling. Navigate to "Divi > Theme Options > Integration" and paste the following code into the "Add code to the <head> of your blog" section:

<style>
   .db-color-cycle-on-scroll {
       transition: background-color 700ms ease;
   }
</style>

<script>
   jQuery(document).ready(function($) {
       var colors = ['#ff0000', '#ff7f00', '#ffff00', '#00ff00', '#0000ff', '#4b0082', '#9400d3'];
       var currentColor = 0;
       var $section = $('.db-color-cycle-on-scroll');
       var isScrolling;

       $(window).on('wheel', function(event) {
           clearTimeout(isScrolling);

           isScrolling = setTimeout(function() {
               if(event.originalEvent.deltaY > 0) {
                   // Scroll down
                   currentColor = (currentColor + 1) % colors.length;
               } else {
                   // Scroll up
                   currentColor = (currentColor - 1 + colors.length) % colors.length;
               }
               $section.css('background-color', colors[currentColor]);
           }, 150);
       });
   });
</script>

Step 3: Test the Effect

After inserting the code, publish your changes and view the page. As you scroll up and down, you should notice a smooth transition between colors. 

For best results, apply this effect on an element that remains visible on the page at all times – otherwise the act of scrolling to cycle colors may also scroll the element itself out of view.

Do More with Divi Dynamic Content!

Enhance your Divi site even further with advanced dynamic capabilities. The Divi Dynamic Content Extended plugin lets you unlock powerful dynamic content features, perfect for adding vibrant elements like color-cycling backgrounds and more.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

4 Comments

  1. Hello,

    What should be added as a variable so that when the sector appears and we enter the sector from the top we display one color and when we exit from the top we display another color.
    here there would only be two colors.

    Thanks

    Reply
    • Hey AlainG, are you able to clarify what you mean by enter the section from the top? Do you mean when the mouse enters it, or when it scrolls past a certain point in the screen, for example? Thanks!

      Reply
  2. Wow! Thanks!

    Reply
    • You're welcome, A Z :)

      Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

Your email address will not be published. Required fields are marked *.

We may earn a commission when you visit links on our website.

Latest From Divi Booster

Enable Automatic Looping for YouTube Videos in the Divi Video Module

Enabling automatic looping for YouTube videos in your Divi video module ensures that content plays seamlessly and continuously without interruption. This feature can enhance user engagement, highlight key information, or create dynamic visual effects on your website....

Hide Video Controls in Divi Video Module

Disabling YouTube video controls in the Divi Video Module helps create a seamless, distraction-free viewing experience for your site visitors. This approach is useful when you want full control over how your video content appears and is interacted with on the page, or...

Mute YouTube Videos by Default in the Divi Video Module

In the Divi Video Module, starting your YouTube videos muted by default can create a more user-friendly browsing experience on your webpage. It is particularly beneficial for reducing distractions, enhancing visitor engagement, and providing a seamless content preview...

Autoplay Videos in Divi Video Module

Enabling videos in your Divi video module to start playing automatically can enhance both the site's design and user engagement. To ensure full compatibility with browser requirements, such as Chrome's autoplay policy, it's often necessary to start your videos muted...

How to Add the Post Status in Divi's Dynamic Content

Divi's Dynamic Content feature is great for enhancing your post/page templates with post-specific information. While Divi includes an option to show information such as the Post Created Date using Dynamic Content, there is no equivalent option for the Post Status....

Random Divi Posts