123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="toc">
  <div class="label">目次
    <span class="icon-a"></span>
  </div>
  <ol class="toc-contents">
    <li><a href="#">見出し</a></li>
    <li><a href="#">見出し</a></li>
    <li><a href="#">見出し</a></li>
    <li><a href="#">見出し</a></li>
  </ol>
</div>
            
          
!
            
              /*----------------------------------------
目次のスタイル
----------------------------------------*/
#toc{
  margin: 30px 0;
  padding: 0;
  border-radius: 3px;
  display: table;
}
.label,.toc-contents{
  width: 100%;
  margin: 0;
  border-radius: 3px;
}
.label{
  text-align: center;
    font-weight: bold;
  background-color: #e9e9e9;
    background-image: -ms-linear-gradient(bottom,#ddd,#e9e9e9);
    background-image: -moz-linear-gradient(bottom,#ddd,#e9e9e9);
    background-image: -o-linear-gradient(bottom,#ddd,#e9e9e9);
    background-image: -webkit-linear-gradient(bottom,#ddd,#e9e9e9);
    background-image: linear-gradient(bottom,#ddd,#e9e9e9);
  font-size: 16px;
  padding: 8px 22px;;
  position: relative;
  color: #333;
  min-width: 250px;
  box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.29);
  -moz-box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.29);
  -webkit-box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.29);
}
.icon-a,.icon-b{
  width: 60px;
  height: 22px;
  margin: auto 0;
  position: absolute;
  cursor: pointer;
  top: 0;
  bottom: 0;
  right: 10px;
  background: #fff;
  border-radius: 5px;
  background-color: #eee;
    background-image: -ms-linear-gradient(bottom,#e3e3e3,#fff);
    background-image: -moz-linear-gradient(bottom,#e3e3e3,#fff);
    background-image: -o-linear-gradient(bottom,#e3e3e3,#fff);
    background-image: -webkit-linear-gradient(bottom,#e3e3e3,#fff);
    background-image: linear-gradient(bottom,#e3e3e3,#fff);

  box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.29);
    -moz-box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.29);
    -webkit-box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.29);
}
.icon-a::before,.icon-b::before{
  display: block;
  position: absolute;
  color: #555;
  font-size: 14px;
  width: 60px;
  height: 22px;
  top: 0;
  right: 0;

}
.icon-a::before{
  content: "非表示";
}
.icon-b::before{
  content: "表示";
}

.toc-contents{
  display: none;
  background: #fff;
  font-size: 14px;
  padding: 22px;
  min-width: 250px;
  box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.29);
  -moz-box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.29);
  -webkit-box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.29);
}

#toc li,#toc li a{
  font-size: 15px;
}
#toc li a{
  text-decoration: underline;
}

/*-------------------------------------
480以下
---------------------------------------*/
@media (max-width: 599px) {
  #toc {
  margin: 30px 0;
  margin-top: 0;
  }
}

            
          
!
            
              jQuery(function($) {
  var idcount = 1; // IDのカウント
  var toc = '';// 目次のHTML格納場所
  var currentlevel = 0; // 見出しのレベル初期値
// 見出しを回してリストに格納
jQuery("article h2", this).each(function() {
  this.id = "toc-" + idcount;// 見出しごとにIDを保存
  idcount++;

// 見出しのレベル設定
var level = 0;
if (this.nodeName.toLowerCase() == "h2") {
level = 1;
}

// 見出しのレベルが現在のレベルよりも数値が大きければ
            // <ol>を追加して入れ子にする
while (currentlevel < level) {
toc += "<ol class='toc-contents'>";
currentlevel++;
}

// そうでなければ</ol>で閉じて入れ子を終了する
while (currentlevel > level) {
toc += "</ol>";
currentlevel--;
}

// リストを生成
toc += '<li><a href="#' + this.id + '">' + jQuery(this).html() + "</a></li>\n";
});

// 現在のレベルが0より上ならリストを閉じる
while (currentlevel > 0) {
toc += "</ol>";
currentlevel--;
}

// HTML出力
if (jQuery("article h2")[0]) {
jQuery("#toc").html('<div class="label">目次<span class="icon-a"></span></div>'+ toc);
}
});


// 目次表示切替
jQuery(function() {
    jQuery('.icon-a').click(function(){
        jQuery('.toc-contents').slideToggle('400');// slow表示 (slow)は600ミリ秒(fast)は200ミリ秒 任意の数値でも。
        // 表示非表示のクラス切替
        jQuery('.icon-a').toggleClass('icon-b');//「非表示」を「表示」になるように変更
    });
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console