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="accordion">
  <!-- ボタン -->
  <span class="accordion-btn js-accordion-btn"></span>
  <div class="accordion-text js-accordion-text">
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
      eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
      ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
      aliquip ex ea commodo consequat. Duis aute irure dolor in
      reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
      pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
      culpa qui officia deserunt mollit anim id est laborum.
    </p>
  </div>
</div>
<div class="accordion">
  <span class="accordion-btn js-accordion-btn"></span>
  <div class="accordion-text js-accordion-text">
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
      eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
      ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
      aliquip ex ea commodo consequat. Duis aute irure dolor in
      reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
      pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
      culpa qui officia deserunt mollit anim id est laborum.
    </p>
  </div>
</div>
              
            
!

CSS

              
                .accordion {
  position: relative;
}
/* ボタン */
.accordion-btn {
  background-color: #6495ed;
  border: 1px solid #6495ed;
  bottom: 0;
  color: #fff;
  cursor: pointer;
  display: inline-block;
  font-size: 18px;
  left: 0;
  margin: auto;
  padding: 5px 15px;
  position: absolute;
  right: 0;
  text-align: center;
  transition: all .2s;
  width: fit-content;
  z-index: 1;
}
/* 続きを読むボタンの表記 */
.accordion-btn::after {
  content: "続きを読む";
}
/* ボタンhover時のスタイル */
.accordion-btn:hover {
  background-color: #fff;
  color: #6495ed;
}
.accordion-text {
  overflow: hidden; /* テキストを隠す */
  position: relative;
}
/* 最初に見えてるテキストエリアの高さ */
.accordion-text.is-hide {
  height: 100px;
}
/* テキストをグラデーションで隠す */
.accordion-text::before {
  background: -webkit-linear-gradient(top, rgba(255,255,255,0) 0%, rgba(255,255,255,.9) 50%, rgba(255,255,255,.9) 50%, #fff 100%);
  background:         linear-gradient(top, rgba(255,255,255,0) 0%, rgba(255,255,255,.9) 50%, rgba(255,255,255,.9) 50%, #fff 100%);
  bottom: 0;
  content: "";
  height: 60px; /* グラデーションで隠す高さ */
  position: absolute;
  width: 100%;
}
/* 閉じるボタンの位置 */
.accordion-btn.is-show {
  bottom: -3em;
}
/* 閉じるボタンの表記 */
.accordion-btn.is-show::after {
  content: "閉じる";
}
/* 続きを見るボタンをクリックしたらテキストを隠しているグラデーションを消す */
.accordion-btn.is-show + .accordion-text::before {
  display: none;
}
/* レイアウトの為のスタイル */
.accordion {
  margin-left: auto;
  margin-right: auto;
  margin-top: 20px;
  width: 300px;
}
.accordion:not(:first-of-type) {
  margin-bottom: 100px;
  margin-top: 100px;
}
.accordion-text p {
  line-height: 1.3;
}
              
            
!

JS

              
                const itemHeights = [];
let returnHeight;

$(function () {
  $(".js-accordion-text").each(function () {
    // 隠す要素
    const thisHeight = $(this).height(); // 隠す要素の高さを取得
    itemHeights.push(thisHeight); // それぞれの高さを配列に入れる
    $(this).addClass("is-hide"); // CSSで指定した高さにする(見えてる高さ)
    returnHeight = $(this).height(); // 見えてる高さを取得
  });
});

$(".js-accordion-btn").click(function () {
  // ボタンをクリックしたら
  if (!$(this).hasClass("is-show")) {
    const index = $(this).index(".js-accordion-btn");
    const addHeight = itemHeights[index]; // 要素の高さを取得
    $(this)
      .addClass("is-show") // 閉じるボタンに表示変更
      .next()
      .animate({ height: addHeight }, 300) // 隠されてる要素をアニメーションで全て表示
  } else {
    $(this)
      .removeClass("is-show") // 閉じるボタン表示解除
      .next()
      .animate({ height: returnHeight }, 300) // 要素を初期の高さにアニメーションで戻す
  }
});

              
            
!
999px

Console