<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>JavaScript on Deux</title><link>https://deux.top/en/tags/javascript/</link><description>Recent content in JavaScript on Deux</description><image><title>Deux</title><url>https://deux.top/branding/og-default.jpg</url><link>https://deux.top/branding/og-default.jpg</link></image><generator>Hugo</generator><language>en-US</language><copyright>2026 二重奏</copyright><lastBuildDate>Thu, 05 Nov 2026 11:34:56 +0000</lastBuildDate><atom:link href="https://deux.top/en/tags/javascript/index.xml" rel="self" type="application/rss+xml"/><item><title>Adding a Non-Intrusive Reading Progress Bar to a Static Blog</title><link>https://deux.top/en/posts/zz-s05/</link><pubDate>Thu, 05 Nov 2026 00:00:00 +0900</pubDate><guid>https://deux.top/en/posts/zz-s05/</guid><description>Add a lightweight reading progress bar to your article pages using less than 40 lines of CSS and a tiny snippet of JS. This method avoids third-party libraries, doesn&amp;#39;t modify the theme core, and includes a fallback for when JS is disabled.</description><content:encoded><![CDATA[<blockquote>
<p>This article was machine-translated by AI and may contain inaccuracies. See the <a href="/zh/posts/zz-s05/">Chinese original</a> for the authoritative version.</p>
</blockquote>
<h2 id="i-the-requirement-is-just-one-sentence">I. The requirement is just one sentence</h2>
<p>**As the reader scrolls, the line at the top follows them. ** No numbers, no percentages, no pop-up prompts, and no thickening on hover. That&rsquo;s it.</p>
<p>The blog uses Hugo + PaperMod, and the theme is updated weekly, so I set a rule for myself before starting: ** Never modify the submodule itself **. Anything that can be placed at the site level should stay at the site level.</p>
<blockquote>
<p>The theme is a volatile dependency; if logic is embedded within it, every weekly PR requires a manual diff review.</p>
</blockquote>
<h2 id="ii-where-to-place-it">II. Where to place it</h2>
<table>
	<thead>
			<tr>
					<th>Method 案</th>
					<th>Change Location 置</th>
					<th>Upgrade to 本</th>
					<th>Conclusion</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>Modifying theme header template 板</td>
					<td>submodule 内</td>
					<td>Weekly rush 突</td>
					<td>✗ Exclude directly</td>
			</tr>
			<tr>
					<td>Overriding <code>extend_head.html</code></td>
					<td>Site level <code>layouts/_partials/</code></td>
					<td>低</td>
					<td>✓ Inject CSS/JS</td>
			</tr>
			<tr>
					<td>Custom shortcodes e</td>
					<td>Inside every article 文</td>
					<td>高</td>
					<td>✗ CMS will break syntax</td>
			</tr>
			<tr>
					<td>Pure CSS <code>scroll-timeline</code></td>
					<td>Site CSS S</td>
					<td>Best 低</td>
					<td>△ Insufficient compatibility</td>
			</tr>
	</tbody>
</table>
<p>In the end, I chose the second option + a small snippet of vanilla JS. CSS goes through <code>assets/css/extended/</code>, and JS goes through <code>assets/js/site.js</code>, both loaded via the theme&rsquo;s <code>extend_head</code> hooks with fingerprints.</p>
<ul>
<li><input checked="" disabled="" type="checkbox"> Uses site-level extension points</li>
<li><input checked="" disabled="" type="checkbox"> Zero third-party dependencies</li>
<li><input disabled="" type="checkbox"> No IE compatibility (stopped doing that long ago)</li>
<li><input disabled="" type="checkbox"> No prefers-reduced-motion toggle (note to self)</li>
</ul>
<h2 id="iii-those-thirty-odd-lines">III. Those thirty-odd lines</h2>
<p>The core is actually just one sentence: <code>scrollY / (scrollHeight - innerHeight)</code>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-js" data-lang="js"><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">updateProgress</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="kr">const</span> <span class="nx">h</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">documentElement</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">  <span class="kr">const</span> <span class="nx">max</span> <span class="o">=</span> <span class="nx">h</span><span class="p">.</span><span class="nx">scrollHeight</span> <span class="o">-</span> <span class="nx">h</span><span class="p">.</span><span class="nx">clientHeight</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">  <span class="kr">const</span> <span class="nx">p</span> <span class="o">=</span> <span class="nx">max</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">?</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">min</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nx">h</span><span class="p">.</span><span class="nx">scrollTop</span> <span class="o">/</span> <span class="nx">max</span><span class="p">)</span> <span class="o">:</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">  <span class="nb">document</span><span class="p">.</span><span class="nx">querySelector</span><span class="p">(</span><span class="s1">&#39;.reading-progress&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="o">?</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">setProperty</span><span class="p">(</span><span class="s1">&#39;--p&#39;</span><span class="p">,</span> <span class="nb">String</span><span class="p">(</span><span class="nx">p</span><span class="p">));</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>Using CSS to control <code>--p</code> with <code>scaleX</code> requires one less reflow than modifying <code>width</code>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-css" data-lang="css"><span class="line"><span class="cl"><span class="p">.</span><span class="nc">reading-progress</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="k">position</span><span class="p">:</span> <span class="kc">fixed</span><span class="p">;</span> <span class="k">inset</span><span class="p">:</span> <span class="mi">0</span> <span class="mi">0</span> <span class="kc">auto</span><span class="p">;</span> <span class="k">height</span><span class="p">:</span> <span class="mi">2</span><span class="kt">px</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">  <span class="k">transform</span><span class="p">:</span> <span class="nb">scaleX</span><span class="p">(</span><span class="nf">var</span><span class="p">(</span><span class="o">--</span><span class="n">p</span><span class="p">,</span> <span class="mi">0</span><span class="p">));</span> <span class="k">transform-origin</span><span class="p">:</span> <span class="kc">left</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">  <span class="k">background</span><span class="p">:</span> <span class="nf">var</span><span class="p">(</span><span class="o">--</span><span class="n">primary</span><span class="p">);</span> <span class="n">pointer-events</span><span class="p">:</span> <span class="kc">none</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>**Watch out for <code>pointer-events: none</code>. ** I forgot to add it in the first version, which caused the progress bar to sit at the very top of the header, covering the theme toggle button&rsquo;s click area by one pixel. It took me half an hour of troubleshooting to find it.</p>
<h2 id="iv-what-to-do-when-js-is-disabled">IV. What to do when JS is disabled</h2>
<p>The progress bar itself is an &ldquo;enhancement&rdquo; rather than &ldquo;content,&rdquo; so if JS doesn&rsquo;t run, an empty line shouldn&rsquo;t appear. The approach is to ** default to <code>display: none</code>, and only show ** when JS adds a class:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-css" data-lang="css"><span class="line"><span class="cl"><span class="p">.</span><span class="nc">reading-progress</span> <span class="p">{</span> <span class="k">display</span><span class="p">:</span> <span class="kc">none</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="nt">html</span><span class="p">.</span><span class="nc">has-js</span> <span class="p">.</span><span class="nc">reading-progress</span> <span class="p">{</span> <span class="k">display</span><span class="p">:</span> <span class="kc">block</span><span class="p">;</span> <span class="p">}</span>
</span></span></code></pre></div><p><code>site.js</code> Add <code>document.documentElement.classList.add('has-js')</code> to the first line. This way, if the script fails to load, is blocked, or exits midway due to an error, the page remains unchanged.</p>
<blockquote>
<p>Progressive enhancements must ** prove their own usability **; don&rsquo;t rely on the reader to bail you out.</p>
</blockquote>
<h2 id="v-two-small-pitfalls">V. Two small pitfalls</h2>
<ol>
<li><strong>There is no <code>.post-single</code></strong> on the homepage, so don&rsquo;t render it on the list page either. In <code>site.js</code>, check for this selector first; if it&rsquo;s not found, just return.</li>
<li>**A long directory will increase the page height **, causing the scroll denominator to change, which makes the progress bar slightly backtrack when it reaches the middle. Finally, a baseline is taken according to <code>document.body.scrollHeight</code> without dynamic recalculation—<strong>it&rsquo;s good enough.</strong></li>
</ol>
<table>
	<thead>
			<tr>
					<th>坑</th>
					<th>Current 象</th>
					<th>Fix</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>Missing pointer-events</td>
					<td>Buttons are unclickable 到</td>
					<td>Add a line of CSS</td>
			</tr>
			<tr>
					<td>Also showing on list page 示</td>
					<td>Extra 线  at the top of the homepage</td>
					<td>Selector gating</td>
			</tr>
			<tr>
					<td>Directory stretches 高</td>
					<td>Slight backtrack in the middle 退</td>
					<td>Accepted</td>
			</tr>
	</tbody>
</table>
<h2 id="vi-wrap-up">VI. Wrap-up</h2>
<p>The entire change involves only two files and about thirty lines of code; no themes were touched, and no packages were installed. The line after <code>hugo --gc --minify</code> is almost invisible, which is exactly what was intended.</p>
<blockquote>
<p><strong>&ldquo;Almost invisible&rdquo;</strong> is the sole design goal of this feature. Once achieved, do not add anything else to it.</p>
</blockquote>
]]></content:encoded></item></channel></rss>