{"id":1041,"date":"2025-11-14T10:51:47","date_gmt":"2025-11-14T10:51:47","guid":{"rendered":"https:\/\/laywow.com\/animation\/background-color-change-on-scroll\/"},"modified":"2025-11-14T11:10:29","modified_gmt":"2025-11-14T11:10:29","slug":"background-color-change-on-scroll","status":"publish","type":"post","link":"https:\/\/laywow.com\/en\/animations\/background-color-change-on-scroll\/","title":{"rendered":"Background Color Change on Scroll"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This effect changes <strong>the background color of the <code>&lt;body&gt;<\/code><\/strong> when the user scrolls to different sections of a page \u2014 perfect for creating a fluid and immersive storytelling experience.<\/p>\n\n<h2 class=\"wp-block-heading\">Add Classes and Attributes in Elementor<\/h2>\n\n<h3 class=\"wp-block-heading\">Step 1: Add the Class <code>.panel<\/code><\/h3>\n\n<p class=\"wp-block-paragraph\">For each section where you want to change the background color, as well as the ones preceding and following it:<\/p>\n\n<ol class=\"wp-block-list\">\n<li>Select your <strong>section<\/strong> in Elementor<\/li>\n\n\n\n<li>Go to <strong>Advanced \u2192 CSS Classes<\/strong><\/li>\n\n\n\n<li>Add: <code>panel<\/code> <img decoding=\"async\" width=\"150\" height=\"23\" class=\"wp-image-1030\" style=\"width: 150px;\" src=\"https:\/\/laywow.com\/wp-content\/uploads\/2025\/11\/Capture-decran-2025-11-14-115526.png\" alt=\"\" srcset=\"https:\/\/laywow.com\/wp-content\/uploads\/2025\/11\/Capture-decran-2025-11-14-115526.png 351w, https:\/\/laywow.com\/wp-content\/uploads\/2025\/11\/Capture-decran-2025-11-14-115526-300x45.png 300w\" sizes=\"(max-width: 150px) 100vw, 150px\" \/><\/li>\n<\/ol>\n\n<h3 class=\"wp-block-heading\">Step 2: Define the Color via an Attribute<\/h3>\n\n<p class=\"wp-block-paragraph\">Still in your section:<\/p>\n\n<ol class=\"wp-block-list\">\n<li>Go to <strong>Advanced \u2192 Attributes<\/strong><\/li>\n\n\n\n<li>Add a new attribute: <strong>Key:<\/strong> <code>data-color<\/code> <strong>Value:<\/strong> <code>gray<\/code> or <code>blue<\/code> or <code>purple<\/code><\/li>\n<\/ol>\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"356\" height=\"226\" src=\"https:\/\/laywow.com\/wp-content\/uploads\/2025\/11\/image.png\" alt=\"\" class=\"wp-image-1031\" srcset=\"https:\/\/laywow.com\/wp-content\/uploads\/2025\/11\/image.png 356w, https:\/\/laywow.com\/wp-content\/uploads\/2025\/11\/image-300x190.png 300w\" sizes=\"(max-width: 356px) 100vw, 356px\" \/><\/figure>\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Example: <code>data-color|blue<\/code> will apply the class <code>.color-blue<\/code> to the body when the section is visible.<\/p>\n<\/blockquote>\n\n<h2 class=\"wp-block-heading\">Add Color Styles to your Site<\/h2>\n\n<p class=\"wp-block-paragraph\">Go to <strong>Appearance \u2192 Customize \u2192 Additional CSS<\/strong>, or in Elementor \u2192 Custom Code \u2192 Global CSS.<\/p>\n\n<p class=\"wp-block-paragraph\">Add this code:<\/p>\n\n<pre class=\"wp-block-code\"><code>body {\n    background-color: #101012;\n    transition: background-color 1s ease;\n}\n\n.color-blue {\n    background-color: #7dc4ea!important;\n}\n\n.color-gray {\n    background-color: #222225!important;\n}\n\n.color-purple {\n    background-color: #7300FF!important;\n}\n<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\"><strong>You can create as many colors as you want<\/strong>:<br\/>Declare a <code>.color-xxx<\/code> then use <code>data-color|xxx<\/code> in your section.<\/p>\n\n<h2 class=\"wp-block-heading\">Add the jQuery Script in Elementor<\/h2>\n\n<ol class=\"wp-block-list\">\n<li>Go to <strong>Elementor \u2192 Custom Code<\/strong><\/li>\n\n\n\n<li>Click <strong>Add New Code<\/strong><\/li>\n\n\n\n<li>Choose position <strong>Footer \u2013 End of <code>&lt;\/body&gt;<\/code><\/strong> <\/li>\n\n\n\n<li>Paste this code:<\/li>\n<\/ol>\n\n<pre class=\"wp-block-code\"><code>&lt;script>\njQuery(function($){\n    $(window).scroll(function() {\n\n        var $window = $(window),\n            $body = $('body'),\n            $panel = $('.panel'); \/\/ Chaque section \u00e0 d\u00e9tecter\n        \n        var scroll = $window.scrollTop() + ($window.height() \/ 2);\n\n        $panel.each(function () {\n            var $this = $(this);\n\n            \/\/ V\u00e9rifie si la section est au milieu de l'\u00e9cran\n            if ($this.position().top &lt;= scroll &amp;&amp; $this.position().top + $this.height() > scroll) {\n\n                \/\/ Supprime toutes les classes color-*\n                $body.removeClass(function (index, css) {\n                    return (css.match(\/(^|\\s)color-\\S+\/g) || &#91;]).join(' ');\n                });\n\n                \/\/ Ajoute la couleur li\u00e9e \u00e0 la section active\n                $body.addClass('color-' + $(this).data('color'));\n            }\n        });\n\n    }).scroll(); \/\/ Ex\u00e9cute au chargement\n});\n&lt;\/script><\/code><\/pre>\n\n<h2 class=\"wp-block-heading\">How Does it Work?<\/h2>\n\n<ul class=\"wp-block-list\">\n<li>Each section with <code>.panel<\/code> is monitored.<\/li>\n\n\n\n<li>When the scroll reaches the <strong>middle of this section<\/strong>, the script:\n<ul class=\"wp-block-list\">\n<li>removes all current <code>color-*<\/code> classes from the body<\/li>\n\n\n\n<li>adds <code>color-gray<\/code>, <code>color-purple<\/code>, etc.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>The CSS then changes the background color of the site.<\/li>\n<\/ul>\n\n<p class=\"wp-block-paragraph\">With the transition:<\/p>\n\n<pre class=\"wp-block-code\"><code>transition: background-color 1s ease;\n<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">The color changes <strong>smoothly<\/strong>, for a storytelling effect.<\/p>\n\n<h2 class=\"wp-block-heading\">Tips &amp; Customization<\/h2>\n\n<h3 class=\"wp-block-heading\">Add more Colors<\/h3>\n\n<p class=\"wp-block-paragraph\">Declare a new class:<\/p>\n\n<pre class=\"wp-block-code\"><code>.color-green {\n    background-color: #1abc9c!important;\n}\n<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Then in Elementor:<\/p>\n\n<pre class=\"wp-block-code\"><code>data-color|green\n<\/code><\/pre>\n\n<h3 class=\"wp-block-heading\">Make the Change Faster or Slower<\/h3>\n\n<p class=\"wp-block-paragraph\">Modify in <code>body<\/code>:<\/p>\n\n<pre class=\"wp-block-code\"><code>transition: background-color 0.4s ease;\n<\/code><\/pre>\n\n<h3 class=\"wp-block-heading\">Change the Detection Area (Middle of Section)<\/h3>\n\n<p class=\"wp-block-paragraph\">In the JS:<\/p>\n\n<pre class=\"wp-block-code\"><code>var scroll = $window.scrollTop() + ($window.height() \/ 2);\n<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Replace <code>\/ 2<\/code> with:<\/p>\n\n<ul class=\"wp-block-list\">\n<li><code>\/ 3<\/code> \u2192 triggers earlier<\/li>\n\n\n\n<li><code>\/ 1.5<\/code> \u2192 triggers later<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This effect changes the background color of the &lt;body&gt; when the user scrolls to different sections of a page \u2014 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1040,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[25,1],"tags":[22,28],"class_list":["post-1041","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-animations","category-animation","tag-scroll","tag-transitions"],"acf":[],"_links":{"self":[{"href":"https:\/\/laywow.com\/en\/wp-json\/wp\/v2\/posts\/1041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/laywow.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laywow.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laywow.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/laywow.com\/en\/wp-json\/wp\/v2\/comments?post=1041"}],"version-history":[{"count":1,"href":"https:\/\/laywow.com\/en\/wp-json\/wp\/v2\/posts\/1041\/revisions"}],"predecessor-version":[{"id":1042,"href":"https:\/\/laywow.com\/en\/wp-json\/wp\/v2\/posts\/1041\/revisions\/1042"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/laywow.com\/en\/wp-json\/wp\/v2\/media\/1040"}],"wp:attachment":[{"href":"https:\/\/laywow.com\/en\/wp-json\/wp\/v2\/media?parent=1041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laywow.com\/en\/wp-json\/wp\/v2\/categories?post=1041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laywow.com\/en\/wp-json\/wp\/v2\/tags?post=1041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}