{"id":10197,"date":"2022-02-19T13:38:18","date_gmt":"2022-02-19T13:38:18","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/markokrstic.com\/?post_type=code&#038;p=10197"},"modified":"2026-10-10T20:48:58","modified_gmt":"2026-10-10T20:48:58","slug":"on-load-and-resize","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/on-load-and-resize\/","title":{"rendered":"Size an Element on Load and Resize in WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Some layout values can&#8217;t be done with CSS alone and have to be measured in the browser: when the page loads and again whenever the window is resized. A common case on WordPress sites is a decorative element that starts at the left edge of the window and ends exactly at the right edge of the centred content box.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This snippet shows the pattern for running JavaScript on load and resize, without jQuery, using that example. It measures the container once on load and again on resize, and sets the width of the target element to reach from the left edge of the window to the right edge of the container.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"js\" class=\"language-js\">(function () {\n\t\/\/ ===== Config =====\n\tvar CONTAINER = '.dpc-container'; \/\/ The centred box the element lines up with.\n\tvar TARGET = '.dpc-to-left-edge'; \/\/ The element(s) that should reach the left edge of the window.\n\t\/\/ ===== End of config =====\n\n\tvar container = document.querySelector(CONTAINER);\n\tvar targets = document.querySelectorAll(TARGET);\n\tif (!container || !targets.length) {\n\t\treturn;\n\t}\n\n\tvar frame = 0;\n\n\tfunction update() {\n\t\tframe = 0;\n\t\t\/\/ Distance from the left edge of the window to the right edge of the container.\n\t\tvar width = Math.round(container.getBoundingClientRect().right);\n\t\ttargets.forEach(function (el) {\n\t\t\tel.style.width = width + 'px';\n\t\t});\n\t}\n\n\t\/\/ Run at most once per frame, however many resize events arrive.\n\tfunction schedule() {\n\t\tif (!frame) {\n\t\t\tframe = window.requestAnimationFrame(update);\n\t\t}\n\t}\n\n\tupdate(); \/\/ On load.\n\twindow.addEventListener('resize', schedule);\n\n\t\/\/ Also when the container changes size without a window resize (fonts, sidebar, accordion).\n\tif ('ResizeObserver' in window) {\n\t\tnew ResizeObserver(schedule).observe(container);\n\t}\n})();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the class <code>dpc-container<\/code> to the centred box and <code>dpc-to-left-edge<\/code> to the element that should stretch. In the block editor that is <strong>Advanced \u2192 Additional CSS class(es)<\/strong>.<\/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 narrow it to the pages that need the script. Under <strong>Script location<\/strong> tick <strong>Footer<\/strong>, set <strong>Footer Mode<\/strong> to <strong>Javascript<\/strong>, paste the code into the footer editor and publish.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Scripts Organizer prints header and footer code on the front end only with the <strong>Conditions<\/strong> trigger, not with <strong>Everywhere<\/strong>, and it adds the <code>&lt;script&gt;<\/code> tag itself, so paste the code without one.<\/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><code>update()<\/code> runs once as soon as the script loads. It is printed in the footer, so the elements already exist.<\/li>\n\n\n<li><code>getBoundingClientRect().right<\/code> is the distance from the left edge of the window to the right edge of the container. That is the width the element needs.<\/li>\n\n\n<li>A resize fires many events per second. <code>schedule()<\/code> uses <code>requestAnimationFrame()<\/code>, so <code>update()<\/code> runs at most once per frame.<\/li>\n\n\n<li><code>ResizeObserver<\/code> also calls it when the container changes size on its own, for example after web fonts load or a sidebar opens.<\/li>\n\n\n<li>The older version used jQuery, which block themes do not load on the front end, and Oxygen element IDs.<\/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 measurements: put your own code in <code>update()<\/code>. Keep the <code>schedule()<\/code> and listener part as it is; it is the reusable bit.<\/li>\n\n\n<li>Several independent elements: run the snippet once per pair, or loop over containers with <code>querySelectorAll()<\/code>.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Variant: CSS only<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When the element sits directly inside a centred container, CSS can do the same without JavaScript:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"css\" class=\"language-css\">\/* The element sits directly inside a centred container. *\/\n.dpc-to-left-edge {\n\twidth: calc(50vw + 50%);\n\tmargin-left: calc(50% - 50vw);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>100vw<\/code> includes the vertical scrollbar on Windows, so this version can be a few pixels too wide there; the JavaScript version measures the real position. For a similar layout see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/resize-image\/\">Resize image from inside container to the one edge of the page<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Width changes made by the script cause a layout shift on load. Give the element a sensible width in CSS first so the jump is small.<\/li>\n\n\n<li>On narrow screens the container is usually full width, so the element simply gets the full width too.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Run JavaScript once on page load and again on every resize in WordPress, throttled to one run per frame and without jQuery.<\/p>\n","protected":false},"author":1,"featured_media":46800,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Size an Element on Load and Resize in WordPress","description":"Run JavaScript once on page load and again on every resize in WordPress, throttled to one run per frame and without jQuery."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[73],"tags":[],"dpc_library":[204],"class_list":["post-10197","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\/10197","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=10197"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/10197\/revisions"}],"predecessor-version":[{"id":46799,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/10197\/revisions\/46799"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46800"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=10197"}],"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=10197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=10197"},{"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=10197"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}