{"id":45727,"date":"2022-07-11T18:02:06","date_gmt":"2022-07-11T18:02:06","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=45727"},"modified":"2026-10-10T20:56:53","modified_gmt":"2026-10-10T20:56:53","slug":"animate-header-logo-on-scroll","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/animate-header-logo-on-scroll\/","title":{"rendered":"Shrink the Header Logo on Scroll in WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A big logo looks good at the top of a WordPress page, but once the visitor scrolls, a tall sticky header eats the screen. This snippet shrinks the header logo on scroll: as soon as the page moves, the header gets the class <code>header-alt<\/code> and the logo animates to a smaller height. Scroll back to the top and it grows again.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is plain JavaScript and CSS. No jQuery and no library.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The markup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add the class <code>header<\/code> to your header and <code>brand<\/code> to the logo. In the block editor or Site Editor, use <strong>Advanced \u2192 Additional CSS class(es)<\/strong> on the header Group and on the Site Logo or Image block; the CSS then sizes the image inside that block. In hand-written markup, put <code>brand<\/code> on the <code>&lt;img&gt;<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"html\" class=\"language-html\">&lt;header class=\"header\"&gt;\n\t&lt;a href=\"\/https\/code.dplugins.com\/\"&gt;\n\t\t&lt;img class=\"brand\" src=\"\/wp-content\/uploads\/logo.svg\" alt=\"Site name\" width=\"400\" height=\"120\"&gt;\n\t&lt;\/a&gt;\n&lt;\/header&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">The CSS<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"css\" class=\"language-css\">\/* img.brand: a plain image; .brand img: a Site Logo or Image block with the class. *\/\n.header img.brand,\n.header .brand img {\n\tdisplay: block;\n\twidth: auto;\n\theight: auto;\n\tmax-height: 120px;\n\ttransition: max-height 0.4s ease;\n}\n\n.header.header-alt img.brand,\n.header.header-alt .brand img {\n\tmax-height: 50px;\n}\n\n@media (prefers-reduced-motion: reduce) {\n\t.header img.brand,\n\t.header .brand img {\n\t\ttransition: none;\n\t}\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">The JavaScript<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"js\" class=\"language-js\">(function () {\n\tvar offset = 1; \/\/ Pixels scrolled before the header gets the class.\n\tvar headers = document.querySelectorAll('.header');\n\tif (!headers.length) {\n\t\treturn;\n\t}\n\n\tvar ticking = false;\n\n\tfunction update() {\n\t\tvar scrolled = window.scrollY &gt;= offset;\n\t\theaders.forEach(function (header) {\n\t\t\theader.classList.toggle('header-alt', scrolled);\n\t\t});\n\t\tticking = false;\n\t}\n\n\twindow.addEventListener('scroll', function () {\n\t\tif (!ticking) {\n\t\t\tticking = true;\n\t\t\twindow.requestAnimationFrame(update);\n\t\t}\n\t}, { passive: true });\n\n\tupdate();\n})();<\/code><\/pre>\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 leave <strong>Template<\/strong> on <strong>All<\/strong>. Under <strong>Script location<\/strong> tick <strong>Header<\/strong> and <strong>Footer<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Header<\/strong>: set <strong>Header Mode<\/strong> to <strong>CSS<\/strong> and paste the CSS.<\/li>\n\n\n<li><strong>Footer<\/strong>: set <strong>Footer Mode<\/strong> to <strong>Javascript<\/strong> and paste the JavaScript.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Publish and enable the block. Scripts Organizer prints Header and Footer code on the front end only with the <strong>Conditions<\/strong> trigger, not with <strong>Everywhere<\/strong>.<\/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 adds <code>header-alt<\/code> to every <code>.header<\/code> when <code>window.scrollY<\/code> is at least <code>offset<\/code> pixels, and removes it again near the top.<\/li>\n\n\n<li>The scroll listener is passive and runs the class update at most once per frame with <code>requestAnimationFrame<\/code>, so scrolling stays smooth.<\/li>\n\n\n<li><code>update()<\/code> also runs once on load. A visitor who reloads halfway down the page gets the small logo right away.<\/li>\n\n\n<li>The CSS animates <code>max-height<\/code>, so the logo keeps its aspect ratio. With <strong>reduce motion<\/strong> turned on in the operating system, the logo switches size without animating.<\/li>\n\n\n<li>The older version used <code>$(function () { \u2026 })<\/code>. WordPress loads jQuery in noConflict mode, so <code>$<\/code> is undefined and the script stopped with an error.<\/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>Shrink later: set <code>var offset = 100;<\/code> so the logo changes after 100px of scrolling.<\/li>\n\n\n<li>Sizes and speed: change <code>120px<\/code>, <code>50px<\/code> and <code>0.4s<\/code> in the CSS.<\/li>\n\n\n<li>Make the header itself sticky with <code>.header { position: sticky; top: 0; z-index: 10; }<\/code>. With the admin bar, add <code>top: var(--wp-admin--admin-bar--height, 0px)<\/code>.<\/li>\n\n\n<li>More effects on the same class: change the header background or padding under <code>.header.header-alt<\/code>. For a header that hides on scroll down, 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\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>.header<\/code> is a common class name. If your theme already uses it for something else, rename it in the markup, the CSS and the <code>querySelectorAll()<\/code> call.<\/li>\n\n\n<li>An SVG logo without <code>width<\/code> and <code>height<\/code> attributes can collapse to zero width with <code>width: auto<\/code>. Give the image its real dimensions.<\/li>\n\n\n<li>Animating height moves the content under a non-sticky header. With a sticky header it does not.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Shrink the WordPress header logo when the visitor scrolls, with a small plain JavaScript class toggle and a CSS transition. No jQuery.<\/p>\n","protected":false},"author":1,"featured_media":46858,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Shrink the Header Logo on Scroll in WordPress","description":"Shrink the WordPress header logo when the visitor scrolls, with a small plain JavaScript class toggle and a CSS transition. 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-45727","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\/45727","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=45727"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45727\/revisions"}],"predecessor-version":[{"id":46857,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45727\/revisions\/46857"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46858"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=45727"}],"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=45727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=45727"},{"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=45727"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}