{"id":10195,"date":"2022-02-18T19:40:26","date_gmt":"2022-02-18T19:40:26","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/markokrstic.com\/?post_type=code&#038;p=10195"},"modified":"2026-10-10T20:48:43","modified_gmt":"2026-10-10T20:48:43","slug":"animate-alpha","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/animate-alpha\/","title":{"rendered":"Fade In Headings with GSAP in WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A short fade-in when the page loads can make a WordPress hero heading feel less abrupt. The old version of this snippet faded in every <code>h1<\/code> with GSAP, but it hid all of them with CSS first. When GSAP did not load (a blocked CDN, a script error, a slow connection), the headings stayed invisible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This version only animates elements you mark with the class <code>dpc-fade-in<\/code>, one after another. It loads GSAP itself from a pinned CDN URL, keeps the content visible without JavaScript, and skips the animation for visitors who turned on reduced motion.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Fade in elements with the class \"dpc-fade-in\" one after another with GSAP.\n *\/\n\n\/\/ ===== Config =====\nconst DPC_GSAP_VERSION = '3.12.5';\n\/\/ ===== End of config =====\n\nadd_action( 'wp_enqueue_scripts', 'dpc_fade_in_assets' );\n\nfunction dpc_fade_in_assets() {\n\twp_enqueue_script(\n\t\t'dpc-gsap',\n\t\t'https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/cdnjs.cloudflare.com\/ajax\/libs\/gsap\/' . DPC_GSAP_VERSION . '\/gsap.min.js',\n\t\tarray(),\n\t\tnull, \/\/ The version is already in the URL.\n\t\tarray( 'in_footer' =&gt; true )\n\t);\n\n\t\/\/ Runs after GSAP. If GSAP did not load, it shows the content instead of animating it.\n\twp_add_inline_script( 'dpc-gsap', &lt;&lt;&lt;'JS'\n(function (root) {\n\tvar items = document.querySelectorAll('.dpc-fade-in');\n\tif (!root.classList.contains('dpc-fade-pending') || !items.length || typeof window.gsap === 'undefined') {\n\t\troot.classList.remove('dpc-fade-pending');\n\t\treturn;\n\t}\n\tgsap.set(items, { autoAlpha: 0 });\n\troot.classList.remove('dpc-fade-pending');\n\tgsap.to(items, { autoAlpha: 1, duration: 0.8, stagger: 0.5, ease: 'power1.out', clearProps: 'opacity,visibility' });\n})(document.documentElement);\nJS\n\t);\n\n\t\/\/ Hide the elements only while the animation is pending, never without JavaScript.\n\twp_register_style( 'dpc-fade-in', false, array(), '1.0' );\n\twp_enqueue_style( 'dpc-fade-in' );\n\twp_add_inline_style( 'dpc-fade-in', '.dpc-fade-pending .dpc-fade-in { visibility: hidden; }' );\n}\n\nadd_action( 'wp_head', 'dpc_fade_in_head_script', 1 );\n\nfunction dpc_fade_in_head_script() {\n\twp_print_inline_script_tag( &lt;&lt;&lt;'JS'\n(function (root) {\n\tif (window.matchMedia &amp;&amp; window.matchMedia('(prefers-reduced-motion: reduce)').matches) {\n\t\treturn;\n\t}\n\troot.classList.add('dpc-fade-pending');\n\t\/\/ Safety net: show the content after 3 seconds, whatever happens to GSAP.\n\tsetTimeout(function () {\n\t\troot.classList.remove('dpc-fade-pending');\n\t}, 3000);\n})(document.documentElement);\nJS\n\t);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then add the class <code>dpc-fade-in<\/code> to the elements to animate: in the block editor under <strong>Advanced \u2192 Additional CSS class(es)<\/strong>, or in your builder&#8217;s class field.<\/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>Everywhere<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor and publish. Leave <strong>Action\/Hook<\/strong> empty.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Everything is in one PHP block on purpose: the CSS and both scripts are added through WordPress&#8217;s own enqueue functions, in the right order. The code only adds hooks; <code>wp_enqueue_scripts<\/code> runs on the front end only.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same code also works unchanged in a small plugin or an mu-plugin.<\/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>A tiny script at the top of <code>&lt;head&gt;<\/code> adds the class <code>dpc-fade-pending<\/code> to <code>&lt;html&gt;<\/code>. The CSS hides <code>.dpc-fade-in<\/code> elements only under that class, so without JavaScript nothing is ever hidden.<\/li>\n\n\n<li>GSAP loads in the footer from cdnjs with an exact version in the URL. After it, an inline script sets the elements to <code>autoAlpha: 0<\/code>, removes the pending class and fades them in with a 0.5 second stagger. <code>clearProps<\/code> removes GSAP&#8217;s inline styles at the end.<\/li>\n\n\n<li>If GSAP is missing when the inline script runs, it removes the pending class at once. A three-second timer in the head script does the same as a last resort.<\/li>\n\n\n<li>With <strong>reduce motion<\/strong> turned on, the head script does nothing and the content simply shows.<\/li>\n\n\n<li>The older version used <code>gsap.from(\"h1\")<\/code> on the window <code>load<\/code> event with <code>h1 { visibility: hidden; }<\/code>, and did not load GSAP at all.<\/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>Other GSAP version: change <code>DPC_GSAP_VERSION<\/code>. Pin an exact version; never load a <code>latest<\/code> URL.<\/li>\n\n\n<li>Speed and order: change <code>duration<\/code>, <code>stagger<\/code> and <code>ease<\/code> in <code>gsap.to()<\/code>. Add <code>y: 20<\/code> to <code>gsap.set()<\/code> and <code>y: 0<\/code> to <code>gsap.to()<\/code> for a slide-up, and add <code>transform<\/code> to <code>clearProps<\/code>.<\/li>\n\n\n<li>Only on some pages: return early in <code>dpc_fade_in_assets()<\/code> and <code>dpc_fade_in_head_script()<\/code>, for example <code>if ( ! is_front_page() ) { return; }<\/code>.<\/li>\n\n\n<li>Animations on scroll instead of on load: see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/add-remove-class-on-scroll\/\">Add and Remove a Class on Scroll in WordPress<\/a>.<\/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>The page loads GSAP from cdnjs. If you already load GSAP from your theme or another plugin, delete the <code>wp_enqueue_script()<\/code> call and attach the inline script to that handle instead, so GSAP is not loaded twice.<\/li>\n\n\n<li>Loading from a CDN sends the visitor&#8217;s IP address to Cloudflare. If that matters for your privacy policy, host <code>gsap.min.js<\/code> yourself and change the URL.<\/li>\n\n\n<li>Above-the-fold elements are hidden until the footer scripts run. Keep the class on a few elements, not on the main content.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Fade in WordPress headings and other elements with GSAP autoAlpha, loaded from a pinned CDN, visible without JavaScript and with reduced motion.<\/p>\n","protected":false},"author":1,"featured_media":46798,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Fade In Headings with GSAP in WordPress - Code DPlugins","description":"Fade in WordPress headings and other elements with GSAP autoAlpha, loaded from a pinned CDN, visible without JavaScript and with reduced motion."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[23],"tags":[],"dpc_library":[204],"class_list":["post-10195","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-gsap"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/10195","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=10195"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/10195\/revisions"}],"predecessor-version":[{"id":46797,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/10195\/revisions\/46797"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46798"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=10195"}],"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=10195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=10195"},{"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=10195"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}