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

              
                <ul class="filter">
  <li data-filter="all" class="is-active">全て表示</li>
  <li data-filter="vegetable">野菜</li>
  <li data-filter="drink">飲み物</li>
  <li data-filter="fruit">フルーツ</li>
 </ul>
 
<ul class="category">
  <li class="is-show" data-category="vegetable">ニンジン</li>
  <li class="is-show" data-category="vegetable">トマト</li>
  <li class="is-show" data-category="drink">コーラ</li>
  <li class="is-show" data-category="drink">牛乳</li>
  <li class="is-show" data-category="fruit">リンゴ</li>
  <li class="is-show" data-category="fruit">マンゴー</li>
</ul>
  
              
            
!

CSS

              
                .filter li {
  cursor: pointer;
}

.filter li.is-active {
  color: red;
}

.category li {
  display: none;
}

.category li.is-show {
  display: block;
}
              
            
!

JS

              
                // 「data-filter」を持つ要素(ボタン)を全て取得
const filterButtons = document.querySelectorAll('[data-filter]');

// 「data-category」を持つ要素(コンテンツ)をすべて取得
const categoryContents = document.querySelectorAll('[data-category]');

// 「data-filter」を持つ要素(ボタン)がクリックされた時に下記の2つ関数を実行
filterButtons.forEach((filterButton) => {
  filterButton.addEventListener('click', buttonSwitch);
  filterButton.addEventListener('click', categoryFilter);
});

// ★関数ボタン部分の関数
function buttonSwitch() {
  // ボタンをクリックしたら全てのアクティブを解除して、クリックした要素をアクティブに変更
  filterButtons.forEach((filterButton) => {
    filterButton.classList.remove("is-active");
    this.classList.add('is-active');
  });
}

// ★コンテンツのフィルタの関数
function categoryFilter() {
  // クリックしたボタンのデータ属性の値を格納
  const buttonCategory = this.dataset.filter;
  // クリックしたボタンと同じ値のデータ属性をを持つコンテンツを格納
  const targetContents = document.querySelectorAll('[data-category="' + buttonCategory + '"]');

  // 「data-category」を持つ要素(コンテンツ)に対してイベント
  categoryContents.forEach((categoryContent) => {

    // ボタンクリック時のアニメーション(.is-activeが付いている要素にも適用させるためJavaScript内でcssを記述)
    categoryContent.animate([
      { opacity: 0 },
      { opacity: 1 }
    ],
      { duration: 600, fill: 'forwards' }
    );

    // クリックしたボタンがallの場合全てを表示
    if (buttonCategory == 'all') {
      categoryContent.classList.add('is-show');
    }

    // all以外の場合はクリックしたボタンと同じデータ属性の値を持つ要素をアクティブ
    else {
      // 一旦すべてアクティブ解除
      categoryContent.classList.remove("is-show");

      targetContents.forEach((targetContent) => {
        targetContent.classList.add('is-show');
      });
    }
  });
}
              
            
!
999px

Console