<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>目次 | ペガブロ</title>
	<atom:link href="https://pegasus-note.com/tag/%e7%9b%ae%e6%ac%a1/feed/" rel="self" type="application/rss+xml" />
	<link>https://pegasus-note.com</link>
	<description></description>
	<lastBuildDate>Wed, 17 Jun 2026 07:19:50 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://pegasus-note.com/wp-content/uploads/2026/03/pegaicon-150x150.png</url>
	<title>目次 | ペガブロ</title>
	<link>https://pegasus-note.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Cocoonでサイドバー目次を「現在地ハイライト＋自動追従」にする方法【長文記事対策】</title>
		<link>https://pegasus-note.com/cocoon-toc-scroll-highlight/</link>
					<comments>https://pegasus-note.com/cocoon-toc-scroll-highlight/#respond</comments>
		
		<dc:creator><![CDATA[hitopega]]></dc:creator>
		<pubDate>Fri, 20 Feb 2026 08:07:34 +0000</pubDate>
				<category><![CDATA[WordPressブログを始める]]></category>
		<category><![CDATA[サイドバー]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[長文記事]]></category>
		<category><![CDATA[カスタマイズ]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[エックスサーバー]]></category>
		<category><![CDATA[Cocoon]]></category>
		<category><![CDATA[UX改善]]></category>
		<category><![CDATA[目次]]></category>
		<guid isPermaLink="false">https://pegasus-note.com/?p=192</guid>

					<description><![CDATA[長文記事で「今どこを読んでいるかわからない」という読者のストレスを解消する方法です。サイドバー目次に現在地ハイライトと自動スクロール追従を追加することで、離脱を防ぎ読者体験を大幅に改善できます。エックスサーバー × Wo [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">長文記事で「今どこを読んでいるかわからない」という読者のストレスを解消する方法です。サイドバー目次に現在地ハイライトと自動スクロール追従を追加することで、離脱を防ぎ読者体験を大幅に改善できます。エックスサーバー × WordPress × Cocoonの環境で実装した手順をそのまま公開します。</p>



<ul class="wp-block-list"><li><strong>現在地ハイライト＋自動追従の実装</strong> → 4ステップのコピペ作業で完結</li><li><strong>CSS追加</strong> → 追加CSSに貼るだけでOK</li><li><strong>JSの設置場所</strong> → footer.phpの正しい位置に追記</li></ul>



<h2 class="wp-block-heading"><span id="toc1">実装するとどうなる？</span></h2>



<p class="wp-block-paragraph"><strong>読んでいるセクションが目次でリアルタイムにハイライトされ、目次が長くても自動でスクロール追従します。</strong></p>



<p class="wp-block-paragraph">長文記事の読者が感じるストレスの多くは、「今どこを読んでいるのか」「あとどれくらい残っているのか」という現在地の見えなさにあります。現在地が可視化されると、読者は安心して読み進めやすくなり、離脱率の低下にもつながります。</p>



<p class="wp-block-paragraph">実装後に変わることは次の3点です。</p>



<ul class="wp-block-list"><li>読んでいるセクションの目次項目がハイライト表示される</li><li>目次が長い場合、アクティブ項目が常に表示範囲内に入るよう自動追従する</li><li>読者が「今どこにいるか」を常に把握できる</li></ul>



<p class="wp-block-paragraph">実装はコードのコピペが中心です。難しい作業はありませんが、テーマファイルを編集するため、<strong>必ず事前にバックアップを取ってから</strong>進めてください。</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube"><div class="wp-block-embed__wrapper">
<iframe title="Cocoonでサイドバー目次を「現在地ハイライト＋自動追従」にする方法" width="1256" height="942" src="https://www.youtube.com/embed/lN3DK_0I2Is?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<h2 class="wp-block-heading"><span id="toc2">手順の全体像</span></h2>



<p class="wp-block-paragraph">作業は4ステップです。コードはすべてコピペで完結します。</p>



<ol class="wp-block-list"><li>サイドバーに目次ウィジェットを設置</li><li>追加CSSを設定する</li><li>Cocoon本体のfooter.phpを子テーマへコピー</li><li>footer.phpにJSを追加する</li></ol>



<h2 class="wp-block-heading"><span id="toc3">① サイドバーに目次を追加する</span></h2>



<p class="wp-block-paragraph">WordPress管理画面の「外観 → ウィジェット」から、目次ウィジェットをサイドバーに追加します。「サイドバースクロール追従」エリアに<strong>目次ウィジェット</strong>を追加してください。</p>



<figure class="wp-block-image size-large"><img decoding="async" src="https://pegasus-note.com/wp-content/uploads/2026/02/image-17-1024x447.png" alt="サイドバーに目次ウィジェットを追加する画面"/></figure>



<p class="wp-block-paragraph">Cocoonの大元設定と目次の表示深さを揃えてください。たとえば大元でh3まで表示する設定にしているのに、ウィジェット側でh2までにするとミスマッチが起き、ハイライトが正しく動作しません。</p>



<h2 class="wp-block-heading"><span id="toc4">② 追加CSSを設定する</span></h2>



<p class="wp-block-paragraph">WordPress管理画面の「外観 → カスタマイズ → 追加CSS」に以下のコードを貼り付けます。</p>



<figure class="wp-block-image size-large"><img decoding="async" src="https://pegasus-note.com/wp-content/uploads/2026/02/image-20-1024x496.png" alt="追加CSSの設定画面"/></figure>



<p class="wp-block-paragraph">目次が長い場合もスクロールできるようにするCSSです。アクティブ項目は青い左ボーダーとハイライト背景で強調されます。</p>



<pre class="wp-block-code"><code>#sidebar-scroll .widget.widget_toc .toc-content{
  max-height: 55vh;
  overflow: auto;
}
#sidebar-scroll .widget.widget_toc a{
  display: block;
  padding: 6px 0 6px 12px;
  text-decoration: none;
  border-left: 3px solid transparent;
  transition: background .2s ease, border-color .2s ease;
}
#sidebar-scroll .widget.widget_toc a.is-active{
  background: rgba(59,130,246,0.12);
  border-left: 3px solid #3b82f6;
  font-weight: 600;
}</code></pre>



<p class="wp-block-paragraph">CSSはここまでで完結です。見た目の調整はこのコードだけで対応できます。</p>



<h2 class="wp-block-heading"><span id="toc5">③ Cocoon本体のfooter.phpを子テーマへコピーする</span></h2>



<p class="wp-block-paragraph">テーマ本体を直接編集するとアップデート時に上書きされます。必ず子テーマに追加してください。</p>



<p class="wp-block-paragraph">すでに子テーマ側にfooter.phpがある場合、またはJSを追記できるファイルがある場合はこの手順をスキップして④へ進んでください。</p>



<h3 class="wp-block-heading"><span id="toc6">エックスサーバーでの作業手順</span></h3>



<figure class="wp-block-image size-large"><img decoding="async" src="https://pegasus-note.com/wp-content/uploads/2026/02/image-21-1024x411.png" alt="エックスサーバーのファイルマネージャ画面"/></figure>



<ol class="wp-block-list"><li>エックスサーバーにログインしてサーバーパネルを開く</li><li>ファイルマネージャから該当ドメインを開く</li><li><code>/public_html/wp-content/themes/cocoon-master/footer.php</code> をコピー</li><li><code>/public_html/wp-content/themes/cocoon-child-master/footer.php</code> として貼り付け</li></ol>



<p class="wp-block-paragraph">サーバーパネルの操作に慣れていない方は、先に全体像を把握しておくとスムーズです。<a href="https://pegasus-note.com/xserver-serverpanel-full-review/" class="pega-internal-link">エックスサーバーのサーバーパネル完全ガイド｜各機能の使い方を解説</a></p>



<h2 class="wp-block-heading"><span id="toc7">④ footer.phpにJSを追加する</span></h2>



<p class="wp-block-paragraph">WordPress管理画面の「外観 → テーマファイルエディター → Theme Footer（footer.php）」を開き、<code>&lt;/div&gt;&lt;!-- #container --&gt;</code>の直前に以下のJSコードを追加します。</p>



<figure class="wp-block-image size-full"><img decoding="async" src="https://pegasus-note.com/wp-content/uploads/2026/02/image-22.png" alt="footer.phpへのJS追記位置"/></figure>



<h3 class="wp-block-heading"><span id="toc8">追加するJSコード</span></h3>



<pre class="wp-block-code"><code>&lt;script&gt;
document.addEventListener("DOMContentLoaded", () =&gt; {
  const toc = document.querySelector("#sidebar-scroll .widget.widget_toc");
  if (!toc) return;

  const tocScroll = toc.querySelector(".toc-content") || toc;
  const links = Array.from(toc.querySelectorAll('a[href^="#"]'))
    .filter(a =&gt; a.getAttribute("href").length &gt; 1);

  const items = links.map(a =&gt; {
    const id = decodeURIComponent(a.getAttribute("href").slice(1));
    const el = document.getElementById(id);
    return el ? { id, el, a } : null;
  }).filter(Boolean);

  if (!items.length) return;

  const OFFSET = 160;
  let currentId = null;

  function clearActive(){
    links.forEach(a =&gt; a.classList.remove("is-active"));
  }

  function keepActiveVisible(activeLink){
    if (tocScroll.scrollHeight &lt;= tocScroll.clientHeight) return;
    const cRect = tocScroll.getBoundingClientRect();
    const aRect = activeLink.getBoundingClientRect();
    const padding = 14;
    if (aRect.top &lt; cRect.top + padding ||
        aRect.bottom &gt; cRect.bottom - padding) {
      activeLink.scrollIntoView({
        block: "center",
        inline: "nearest",
        behavior: "smooth"
      });
    }
  }

  function setActive(id){
    if (id === currentId) return;
    currentId = id;
    clearActive();
    const hit = items.find(x =&gt; x.id === id);
    if (!hit) return;
    hit.a.classList.add("is-active");
    keepActiveVisible(hit.a);
  }

  function update(){
    let candidate = items[0];
    for (const it of items) {
      const top = it.el.getBoundingClientRect().top;
      if (top &lt;= OFFSET) candidate = it;
      else break;
    }
    setActive(candidate.id);
  }

  let ticking = false;
  function onScroll(){
    if (ticking) return;
    ticking = true;
    requestAnimationFrame(() =&gt; {
      update();
      ticking = false;
    });
  }

  window.addEventListener("scroll", onScroll, { passive: true });
  window.addEventListener("resize", onScroll, { passive: true });
  window.addEventListener("load", onScroll);
  update();
});
&lt;/script&gt;</code></pre>



<h3 class="wp-block-heading"><span id="toc9">固定ヘッダーがある場合の調整</span></h3>



<p class="wp-block-paragraph">固定ヘッダーがある環境では、本文の表示タイミングとハイライトのタイミングがずれることがあります。その場合は以下の数値を調整してください。一般的な固定ヘッダーなら240前後が目安です。</p>



<pre class="wp-block-code"><code>const OFFSET = 160;</code></pre>



<p class="wp-block-paragraph">ここまでで実装は完了です。ページを読み込んでスクロールし、目次のハイライトが動いていれば成功です。</p>



<h2 class="wp-block-heading"><span id="toc10">なぜこの実装が重要なのか？</span></h2>



<p class="wp-block-paragraph">長文記事で離脱が増える大きな原因は「現在地が見えないこと」です。5,000〜1万文字の記事は情報量で価値を出せますが、読者にとっては読むこと自体が負荷です。「今どこにいるかわからない」「終わりが見えない」というストレスが重なると、離脱率は確実に上がります。</p>



<p class="wp-block-paragraph">現在地が可視化されると、次の変化が期待できます。</p>



<ul class="wp-block-list"><li>読者が安心して読み進められる</li><li>「あとこれだけ」が見えることで読了率が上がる</li><li>離脱が減り、UX向上がSEOにも好影響をもたらす</li></ul>



<p class="wp-block-paragraph"><strong>長文コンテンツを書くブログほど、この改善の効果は大きくなります。</strong></p>



<h2 class="wp-block-heading"><span id="toc11">よくある質問</span></h2>



<h3 class="wp-block-heading"><span id="toc12">子テーマがない場合はどうすればいいですか？</span></h3>



<p class="wp-block-paragraph">子テーマなしにCocoon本体のfooter.phpを直接編集すると、テーマアップデート時に変更内容が上書きされます。必ずCocoon Child（子テーマ）を有効化してから作業してください。子テーマはCocoon公式サイトで配布されています。</p>



<h3 class="wp-block-heading"><span id="toc13">ハイライトが動作しない場合の確認ポイントは？</span></h3>



<p class="wp-block-paragraph">最も多い原因はCocoon大元設定とウィジェットの目次表示深さのミスマッチです。たとえば大元でh3まで表示している場合は、ウィジェット側も同じ設定にしてください。また、JSがfooter.phpの正しい位置（<code>&lt;/div&gt;&lt;!-- #container --&gt;</code>の直前）に追記されているかも確認してください。</p>



<h3 class="wp-block-heading"><span id="toc14">固定ヘッダーがある場合の調整方法は？</span></h3>



<p class="wp-block-paragraph">JSコード内の<code>const OFFSET = 160;</code>の数値を変更してください。固定ヘッダーの高さに合わせて200〜240程度に増やすと、ハイライトのタイミングが本文表示と合いやすくなります。</p>



<h3 class="wp-block-heading"><span id="toc15">Cocoon以外のテーマでも使えますか？</span></h3>



<p class="wp-block-paragraph">JSとCSSはCocoon専用のクラス名（<code>widget_toc</code>など）を参照しているため、他テーマでは目次ウィジェットのHTML構造に合わせてセレクタを書き換える必要があります。汎用性はありますが、そのままのコピペでは動作しない場合があります。</p>



<h2 class="wp-block-heading"><span id="toc16">まとめ</span></h2>



<p class="wp-block-paragraph">今回やったことは4つです。ウィジェットの設置、CSS追加、footer.phpの子テーマへのコピー、JSの追記。コードはすべてコピペで完結します。</p>



<p class="wp-block-paragraph"><strong>長文記事を書くブログほど、この改善の効果は大きくなります。</strong>読者の「今どこ？」を解消することが、離脱対策の第一歩です。</p>



<p class="wp-block-paragraph">なお、この記事はエックスサーバー上での手順を前提にしています。定期的にキャンペーンを実施しているため、最新の料金は公式サイトで確認してください。<a rel="nofollow sponsored noopener" href="https://px.a8.net/svt/ejp?a8mat=35HN36+5XQSCQ+CO4+609HU" class="pega-external-link" target="_blank">エックスサーバーの公式サイトを確認する</a><img decoding="async" border="0" width="1" height="1" src="https://www12.a8.net/0.gif?a8mat=35HN36+5XQSCQ+CO4+609HU" alt=""></p>

]]></content:encoded>
					
					<wfw:commentRss>https://pegasus-note.com/cocoon-toc-scroll-highlight/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
