Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <section class="toc-demo">
<h2>見出し1</h2>  
  <p>
    見出し1のコンテンツ。<br>
    見出し1のコンテンツ。<br>
    見出し1のコンテンツ。<br>
    見出し1のコンテンツ。<br>  
    見出し1のコンテンツ。<br>
    見出し1のコンテンツ。<br>  
    見出し1のコンテンツ。<br>
    見出し1のコンテンツ。<br>  
    見出し1のコンテンツ。<br>
    見出し1のコンテンツ。<br>  
  </p>
<h2>見出し2</h2>  
  <p>
    見出し2のコンテンツ。<br>
    見出し2のコンテンツ。<br>  
    見出し2のコンテンツ。<br>
    見出し2のコンテンツ。<br>  
    見出し2のコンテンツ。<br>
    見出し2のコンテンツ。<br>  
    見出し2のコンテンツ。<br>
    見出し2のコンテンツ。<br>  
    見出し2のコンテンツ。<br>
    見出し2のコンテンツ。 <br> 
  </p>
<h2>見出し3</h2>
  <p>
    見出し3のコンテンツ。<br>
    見出し3のコンテンツ。<br>  
    見出し3のコンテンツ。<br>
    見出し3のコンテンツ。<br>  
    見出し3のコンテンツ。<br>
    見出し3のコンテンツ。<br>  
    見出し3のコンテンツ。<br>
    見出し3のコンテンツ。<br>  
    見出し3のコンテンツ。<br>
    見出し3のコンテンツ。<br>  
  </p>
<h2>見出し4</h2>  
  <p>
    見出し4のコンテンツ。<br>
    見出し4のコンテンツ。<br>  
    見出し4のコンテンツ。<br>
    見出し4のコンテンツ。<br>  
    見出し4のコンテンツ。<br>
    見出し4のコンテンツ。<br>  
    見出し4のコンテンツ。<br>
    見出し4のコンテンツ。<br>  
    見出し4のコンテンツ。<br>
    見出し4のコンテンツ。<br>  
  </p>
<h2>見出し5</h2>  
  <p>
    見出し5のコンテンツ。<br>
    見出し5のコンテンツ。<br>  
    見出し5のコンテンツ。<br>
    見出し5のコンテンツ。<br>  
    見出し5のコンテンツ。<br>
    見出し5のコンテンツ。<br>  
    見出し5のコンテンツ。<br>
    見出し5のコンテンツ。<br>  
    見出し5のコンテンツ。<br>
    見出し5のコンテンツ。<br>  
  </p>  
</section>
              
            
!

CSS

              
                section.toc-demo{
  width: 800px;
  margin: 0 auto;
}

h2{
  font-size: 20px;
  background-color: #f3f3f3;
  border-bottom: 3px solid #333;
  padding: 5px 0 5px 10px;
}
p{
  padding-left: 10px;
}
ol{
  width: 150px;
  position: fixed;
  top: 0;
  right: 0;
}
li{
  list-style-type:none;
}
a{
  display: block;
  width: 100%;
  background-color: rgba(70 70 70 / 70%);
  color: #fff;
  margin-bottom: 1px;
  text-decoration: none;
  padding: 3px 5px 3px 10px;
}
a.current{
  background-color: #fff;
  color: #000;
}

              
            
!

JS

              
                const headings = document.querySelectorAll('h2');
let toc = '<ol id="fixed-toc">';

headings.forEach( function(heading, index) {
  heading.id = 'heading-0' + (index+1);
  let id = heading.id;
  toc = toc + '<li><a href="#' + id + '">' + heading.textContent + '</a></li>';
});

toc = toc + '</ol>';
document.querySelector('.toc-demo').insertAdjacentHTML('afterbegin', toc);

window.addEventListener('scroll', function() {
    let scroll = window.scrollY;//スクロール量を取得
    let hight = window.innerHeight;//画面の高さを取得
    let offset = 200;
    const toc_completed = document.getElementById('fixed-toc');

    headings.forEach( function( heading , index) {
        let i = index + 1;
        let target = document.querySelector('#fixed-toc li:nth-of-type(' + i + ') > a');
        let pos = heading.getBoundingClientRect().top + scroll;//見出しの位置

        if ( scroll > pos - hight + offset ) {//スクロール量が見出しを超えた

            if (headings[index + 1] !== undefined){// 次の見出しがある=最後の見出しではない

                let next_pos = headings[index + 1].getBoundingClientRect().top + scroll;//次の見出しの位置
                if ( scroll > next_pos - hight + offset ) { // スクロール量が次の見出しも超えている
                    target.classList.remove('current');
                } else if (target.classList.contains('current') == true) { // すでにcurrentがついている
                    return;
                } else if ( i == 1 && toc_completed.classList.contains('active') == false ){// 1つ目
                    target.classList.add('current')
                    toc_completed.classList.add('active')
                } else { // 次の見出しは見えてない
                    target.classList.add('current');
                }

            } else { //最後の見出しの時
                target.classList.add('current');
            }
        } else { //スクロール量が見出しを超えてない
            target.classList.remove('current');
            if ( i == 1 && toc_completed.classList.contains('active')){ //1つ目に到達していない場合
                toc_completed.classList.remove('active')
            }
        }
    });
});
              
            
!
999px

Console