Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

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

              
                <div class="wrapper">

    <div class="accordion">
        <div class="accordion__title">
            <button type="button" aria-expanded="false" aria-controls="acc01">
                アコーディオンその1
                <span class="accordion__icon" aria-hidden="true"></span>
            </button>
        </div>
        <div id="acc01" class="accordion__content" aria-hidden="true">
          <p>アコーディオンその1のコンテンツだよ。</p>
          <p>アコーディオンその1のコンテンツだよ。</p>
          <p>アコーディオンその1のコンテンツだよ。</p>
        </div>
    </div>

    <div class="accordion">
        <div class="accordion__title">
          <button type="button" aria-expanded="false" aria-controls="acc02">
            アコーディオンその2
            <span class="accordion__icon" aria-hidden="true"></span>
          </button>
        </div>
        <div id="acc02" class="accordion__content" aria-hidden="true">
          <p>アコーディオンその2のコンテンツだよ。</p>
          <p>アコーディオンその2のコンテンツだよ。</p>
          <p>アコーディオンその2のコンテンツだよ。</p>
        </div>
    </div>

    <div class="accordion is-opened">
      <div class="accordion__title">
        <button type="button" aria-expanded="true" aria-controls="acc03">
          アコーディオン3(初期状態で開いてる)
          <span class="accordion__icon" aria-hidden="true"></span>
        </button>
      </div>
      <div id="acc03" class="accordion__content" aria-hidden="false">
        <p>アコーディオン3のコンテンツだよ。</p>
        <p>アコーディオン3のコンテンツだよ。</p>
        <p>アコーディオン3のコンテンツだよ。</p>
      </div>
    </div>
  <div class="blogLink" >
    <a href="https://wemo.tech/4066" target="_blank" rel="noopener noreferre">by WEMO.</a>
  </div>
</div>
              
            
!

CSS

              
                // ※ 閉じた時の高さ = タイトル部分の高さ で計算するので、borderを使うとその分ずれることに注意
.accordion {
  position: relative;
  box-sizing: border-box;
  box-shadow: 0 0 0 1px #aaa; // ※
  overflow: hidden;
}
.accordion + .accordion {
		margin-top: -1px;
}

.accordion__title {
  background: #f5f5f5;
}

.accordion__title > button{
  display: flex;
  background: none;
	align-items: center;
	justify-content: space-between;
  width: 100%;
  height: auto;
  border: none;
  font-size: inherit;
  color: inherit;
  padding: 1em;
  cursor: pointer;
  margin: 0;
}

.accordion__icon {
    display: block;
    width: 1em;
    height: 1em;
    margin: 0 .5em;
    background-color: currentColor;
    clip-path: polygon(50% 80%, 0 20%, 100% 20%);
    transition: transform .25s;
}

.is-opened > .accordion__title .accordion__icon{
  transform: rotate(180deg);
}

.accordion__content {
  padding: 1em;
}

// 高さ取得できるように、height:0 にはせずabsoluteで飛ばしておく
.accordion__content[aria-hidden="true"] {
  position: absolute;
  opacity: 0;
  visibility: hidden;
}

/* アコーディオンに無関係 */
.wrapper{
  font-size: min(3.6vw, 16px);
  padding: 1em;
  max-width: 800px;
  line-height: 1.8;
    
  *{
    box-sizing: border-box;
  }
}

.blogLink{
  margin-top: 4em;
  font-size: 12px;
}
              
            
!

JS

              
                const ANIMATION_TIMES = {
	duration: 250,
	easing: 'ease-out',
};

document.addEventListener('DOMContentLoaded', function () {
	const accordions = document.querySelectorAll('.accordion');
	accordions.forEach((accordion) => {
		// button 取得
		const trigger = accordion.querySelector('.accordion__title button');

		// title取得
		const title = accordion.querySelector('.accordion__title');

		// __content 取得
		const content = accordion.querySelector('.accordion__content');

		// アニメーションを保持しておくための変数
		let animation = null;

		// 現在のアニメーション状況を保持する変数
		let nowAnimation = '';

		// 各種リセット
		const resetAnimation = () => {
			animation = null;
			nowAnimation = '';
			accordion.style.height = '';
		};

		trigger.addEventListener('click', function (e) {
			e.preventDefault();

			const isExpanded = trigger.getAttribute('aria-expanded');

			// アニメーション中であればキャンセルさせる
			if (animation) {
				return;
			}

			if (nowAnimation === 'closing' || 'false' === isExpanded) {
				// アニメーション状態の記憶
				nowAnimation = 'opening';

				// 現在の高さをセット
				accordion.style.height = `${accordion.offsetHeight}px`;

				// ariaセット: animation開始前に。
				trigger.setAttribute('aria-expanded', 'true');
				content.setAttribute('aria-hidden', 'false');

				// 現在の高さから __title + コンテンツ の高さまで広げる
				animation = accordion.animate(
					{
						height: `${title.offsetHeight + content.offsetHeight}px`,
					},
					ANIMATION_TIMES
				);

				// アニメーション完了時の処理を登録
				animation.onfinish = () => {
					// accordion.open = true;
					resetAnimation();
				};

				accordion.classList.add('is-opened'); // クラスの削除
			} else if (nowAnimation === 'opening' || 'true' === isExpanded) {
				// アニメーション状態の記憶
				nowAnimation = 'closing';

				// 現在の高さで一旦固定させる
				accordion.style.height = `${accordion.offsetHeight}px`;

				// 現在の高さから __title の高さまで縮める
				animation = accordion.animate(
					{
						height: `${title.offsetHeight}px`,
					},
					ANIMATION_TIMES
				);

				// アニメーション完了時の処理を登録
				animation.onfinish = () => {
					resetAnimation();
					// ariaセット: animation終了後
					trigger.setAttribute('aria-expanded', 'false');
					content.setAttribute('aria-hidden', 'true');
				};

				// アイコンのアニメーション用クラスを削除
				accordion.classList.remove('is-opened');
			}
		});
	});
});

              
            
!
999px

Console