{"id":99,"date":"2019-12-07T21:38:40","date_gmt":"2019-12-07T21:38:40","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.oxyset.com\/?p=99"},"modified":"2026-10-10T20:05:39","modified_gmt":"2026-10-10T20:05:39","slug":"add-remove-class-on-scroll","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/add-remove-class-on-scroll\/","title":{"rendered":"Add and Remove a Class on Scroll in WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Fade-in and slide-up effects in WordPress usually work the same way: an element gets a class when it scrolls into view, and CSS animates the change. This snippet adds the class <code>is-visible<\/code> to every element with the class <code>show-on-scroll<\/code> while it is on screen, and removes it again when it leaves.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It uses <code>IntersectionObserver<\/code>, so the browser tells the script when an element enters or leaves the screen. No jQuery and no loop that runs on every frame.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The JavaScript:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"js\" class=\"language-js\">(function () {\n\tvar selector = '.show-on-scroll'; \/\/ Elements to watch.\n\tvar visibleClass = 'is-visible'; \/\/ Class added while an element is on screen.\n\tvar once = false; \/\/ true = keep the class after the first time.\n\n\tvar elements = document.querySelectorAll(selector);\n\tif (!elements.length) {\n\t\treturn;\n\t}\n\n\tif (!('IntersectionObserver' in window)) {\n\t\telements.forEach(function (el) {\n\t\t\tel.classList.add(visibleClass);\n\t\t});\n\t\treturn;\n\t}\n\n\tdocument.documentElement.classList.add('dpc-scroll-ready');\n\n\tvar observer = new IntersectionObserver(function (entries) {\n\t\tentries.forEach(function (entry) {\n\t\t\tif (entry.isIntersecting) {\n\t\t\t\tentry.target.classList.add(visibleClass);\n\t\t\t\tif (once) {\n\t\t\t\t\tobserver.unobserve(entry.target);\n\t\t\t\t}\n\t\t\t} else if (!once) {\n\t\t\t\tentry.target.classList.remove(visibleClass);\n\t\t\t}\n\t\t});\n\t});\n\n\telements.forEach(function (el) {\n\t\tobserver.observe(el);\n\t});\n})();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example CSS for a fade-and-slide effect:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"css\" class=\"language-css\">.dpc-scroll-ready .show-on-scroll {\n\topacity: 0;\n\ttransform: translateY(1.5rem);\n\ttransition: opacity 0.6s ease, transform 0.6s ease;\n}\n\n.dpc-scroll-ready .show-on-scroll.is-visible {\n\topacity: 1;\n\ttransform: none;\n}\n\n@media (prefers-reduced-motion: reduce) {\n\t.dpc-scroll-ready .show-on-scroll {\n\t\ttransform: none;\n\t\ttransition: none;\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then add the class <code>show-on-scroll<\/code> to any block (<strong>Advanced \u2192 Additional CSS class(es)<\/strong>) or builder element.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Conditions<\/strong> and set <strong>Template<\/strong> to <strong>All<\/strong> (or only the pages that use the effect). Under <strong>Script location<\/strong> tick <strong>Header<\/strong> and <strong>Footer<\/strong>. Put the CSS in the header with <strong>Header Mode<\/strong> <strong>CSS<\/strong>, and the JavaScript in the footer with <strong>Footer Mode<\/strong> <strong>Javascript<\/strong>, then publish.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Scripts Organizer prints header and footer code with the <strong>Conditions<\/strong> trigger on the front end, not with <strong>Everywhere<\/strong>, and adds the <code>&lt;style&gt;<\/code> and <code>&lt;script&gt;<\/code> tags itself.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The script finds all <code>.show-on-scroll<\/code> elements once and observes them. The callback runs only when an element crosses the edge of the screen, which is much cheaper than the old <code>requestAnimationFrame<\/code> loop that measured every element 60 times a second.<\/li>\n\n\n<li><code>isIntersecting<\/code> is true as soon as any part of the element is visible, the same rule as the old version.<\/li>\n\n\n<li>The script adds <code>dpc-scroll-ready<\/code> to <code>&lt;html&gt;<\/code> before it starts. The CSS only hides elements under that class, so if JavaScript fails or is off, the content stays visible.<\/li>\n\n\n<li>Browsers without <code>IntersectionObserver<\/code> get <code>is-visible<\/code> on everything right away.<\/li>\n\n\n<li>The CSS respects <code>prefers-reduced-motion<\/code>: no slide, only the fade.<\/li>\n\n\n<li>The earlier version wrapped the code in <code>jQuery<\/code>, which block themes do not load on the front end, so it failed with &#8220;jQuery is not defined&#8221;.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Animate only once: set <code>var once = true;<\/code>. The class stays after the first time.<\/li>\n\n\n<li>Start a bit later: pass options to the observer, for example <code>new IntersectionObserver(callback, { rootMargin: '0px 0px -15% 0px' })<\/code>.<\/li>\n\n\n<li>Other class names: change <code>selector<\/code> and <code>visibleClass<\/code> at the top, and the CSS to match.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Elements added later (infinite scroll, AJAX filters) are not observed; call <code>observer.observe(el)<\/code> for them.<\/li>\n\n\n<li>On very long pages the browser may paint before the footer script runs, so hidden elements can flash briefly. Keep the hidden state subtle.<\/li>\n\n\n<li>For a header that reacts to scrolling, see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/show-navigation-on-scroll-up\/\">Show navigation on scroll up<\/a>.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Add an is-visible class to WordPress elements while they are on screen and remove it when they leave, with IntersectionObserver and no jQuery.<\/p>\n","protected":false},"author":1,"featured_media":46781,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Add and Remove a Class on Scroll in WordPress","description":"Add an is-visible class to WordPress elements while they are on screen and remove it when they leave, with IntersectionObserver and no jQuery."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[73],"tags":[],"dpc_library":[204],"class_list":["post-99","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":46780,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/99\/revisions\/46780"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46781"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=99"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=99"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}