アコーディオンUI

details & summary & css
- name属性でdetails要素制御
- 複数のdetails要素に同じ値を持つname属性を指定すると、該当箇所だけが開いて他は自動で閉じる
坊っちゃん
親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて、一週間ほど腰を抜かした事がある。
ポラーノの広場
あのイーハトーヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモリーオ市、郊外のぎらぎらひかる草の波。
徒然草
つれづれなるまゝに、日暮らし、硯にむかひて、心にうつりゆくよしなし事を、そこはかとなく書きつくれば、あやしうこそものぐるほしけれ。
HTML
<div class="accordion">
<details name="title">
<summary>坊っちゃん</summary>
<p>親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて、一週間ほど腰を抜かした事がある。</p>
</details>
<details name="title">
<summary>ポラーノの広場</summary>
<p>あのイーハトーヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモリーオ市、郊外のぎらぎらひかる草の波。</p>
</details>
<details name="title">
<summary>徒然草</summary>
<p>つれづれなるまゝに、日暮らし、硯にむかひて、心にうつりゆくよしなし事を、そこはかとなく書きつくれば、あやしうこそものぐるほしけれ。</p>
</details>
</div>
CSS
.accordion {
& summary {
display: grid;
grid-template-columns: 1fr auto;
cursor: pointer;
&::-webkit-details-marker {
display: none; /* safari */
}
&::after {
content: "";
display: inline-block;
background: #000;
height: 18px;
width: 12px;
clip-path: polygon(10% 0, 100% 50%, 10% 100%, 0 100%, 90% 50%, 0 0);
transform: rotate(90deg);
transition: 0.3s transform ease-out;
}
}
& details {
&[open] {
background: #f5f5f5;
color: #c00;
}
&[open] summary::after {
transform: rotate(-90deg);
}
}
}
details & summary & css & js
- jsで動きを制御する
坊ちゃん
親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて一週間ほど腰を抜かした事がある。親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて一週間ほど腰を抜かした事がある。
徒然草
つれづれなるまゝに、日暮らし、硯にむかひて、心にうつりゆくよしなし事を、 そこはかとなく書きつくれば、あやしうこそものぐるほしけれ。
ポラーノの広場
あのイーハトーヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモリーオ市、郊外のぎらぎらひかる草の波。
HTML
<div class="accordion2">
<details class="accordion-item">
<summary class="ac_head">坊ちゃん</summary>
<div class="ac_cont">親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて一週間ほど腰を抜かした事がある。親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて一週間ほど腰を抜かした事がある。</div>
</details>
<details class="accordion-item">
<summary class="ac_head">徒然草</summary>
<div class="ac_cont">つれづれなるまゝに、日暮らし、硯にむかひて、心にうつりゆくよしなし事を、 そこはかとなく書きつくれば、あやしうこそものぐるほしけれ。</div>
</details>
<details class="accordion-item">
<summary class="ac_head">ポラーノの広場</summary>
<div class="ac_cont">あのイーハトーヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモリーオ市、郊外のぎらぎらひかる草の波。</div>
</details>
</div>
CSS
.accordion2 {
& details {
&:last-of-type {
border-bottom: #bbb 1px solid;
}
& .ac_head {
padding: 2em;
display: block;
position: relative;
border-top: #bbb 1px solid;
cursor: pointer;
&::-webkit-details-marker {
display: none; /* safari */
}
&::before,
&::after {
content: "";
position: absolute;
top: 50%;
right: 1rem;
width: 1rem;
height: 1px;
background: #555;
transform: translateY(-50%);
}
&::after {
rotate: 90deg;
transition: all 0.2s ease-in-out;
}
}
& .ac_cont {
border-top: #bbb 1px solid;
display: grid;
grid-template-rows: 0fr; /* height: 0 の代わり */
overflow: hidden;
}
&:open {
& summary {
&::after {
rotate: 0deg;
opacity: 0.2;
}
}
& .ac_cont {
grid-template-rows: 1fr;
padding: 2rem;
}
}
}
}
js
class Accordion {
constructor(el) {
this.el = el;
this.summary = el.querySelector(".ac_head");
this.content = el.querySelector(".ac_cont");
this.animation = null;
this.isClosing = false;
this.isExpanding = false;
this.summary.addEventListener("click", (e) => this.onClick(e));
}
onClick(e) {
e.preventDefault();
this.el.style.overflow = "hidden";
if (this.isClosing || !this.el.open) {
this.open();
} else if (this.isExpanding || this.el.open) {
this.shrink();
}
}
shrink() {
this.isClosing = true;
const startHeight = `${this.el.offsetHeight}px`;
const endHeight = `${this.summary.offsetHeight}px`;
if (this.animation) this.animation.cancel();
this.animation = this.el.animate(
{
height: [startHeight, endHeight],
},
{
duration: 300,
easing: "ease-out",
}
);
this.animation.onfinish = () => this.onAnimationFinish(false);
this.animation.oncancel = () => (this.isClosing = false);
}
open() {
this.el.style.height = `${this.el.offsetHeight}px`;
this.el.open = true;
window.requestAnimationFrame(() => this.expand());
}
expand() {
this.isExpanding = true;
const startHeight = `${this.el.offsetHeight}px`;
const endHeight = `${this.summary.offsetHeight + this.content.offsetHeight}px`;
if (this.animation) this.animation.cancel();
this.animation = this.el.animate(
{
height: [startHeight, endHeight],
},
{
duration: 300,
easing: "ease-out",
}
);
this.animation.onfinish = () => this.onAnimationFinish(true);
this.animation.oncancel = () => (this.isExpanding = false);
}
onAnimationFinish(open) {
this.el.open = open;
this.animation = null;
this.isClosing = false;
this.isExpanding = false;
this.el.style.height = this.el.style.overflow = "";
}
}
document.querySelectorAll(".accordion-item").forEach((el) => {
new Accordion(el);
});
pure css
cssとhtmlで作るアコーディオン
- inputのid属性で動きを制御
- 複数の場合idが増える
親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて一週間ほど腰を抜かした事がある。親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて一週間ほど腰を抜かした事がある。
つれづれなるまゝに、日暮らし、硯にむかひて、心にうつりゆくよしなし事を、 そこはかとなく書きつくれば、あやしうこそものぐるほしけれ。
あのイーハトーヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモリーオ市、郊外のぎらぎらひかる草の波。
HTML
<div class="purecss">
<input id="acd_1" type="checkbox" />
<label for="acd_1"><span>坊ちゃん</span></label>
<div class="ac_cont">親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて一週間ほど腰を抜かした事がある。親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて一週間ほど腰を抜かした事がある。</div>
<input id="acd_2" type="checkbox" />
<label for="acd_2"><span>徒然草</span></label>
<div class="ac_cont">つれづれなるまゝに、日暮らし、硯にむかひて、心にうつりゆくよしなし事を、 そこはかとなく書きつくれば、あやしうこそものぐるほしけれ。</div>
<input id="acd_3" type="checkbox" />
<label for="acd_3"><span>ポラーノの広場</span></label>
<div class="ac_cont">あのイーハトーヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモリーオ市、郊外のぎらぎらひかる草の波。</div>
</div>
CSS
.purecss {
& label {
display: grid;
grid-template-columns: 1fr 1.6em;
column-gap: 1em;
padding: 2rem 0 2rem 2rem;
cursor: pointer;
position: relative;
transition: all 0.3s ease-in-out;
&::after {
content: "";
background-color: #888;
width: 1.6em;
height: 1.6em;
}
& span {
&::before,
&::after {
content: "";
position: absolute;
top: 50%;
right: 0.35rem;
width: 0.9rem;
height: 1px;
background: #fff;
transform: translateY(-50%);
}
&::after {
rotate: 90deg;
transition: all 0.2s ease-in-out;
}
}
}
& .ac_cont {
border-top: #bbb 1px solid;
padding-inline: 2rem;
height: 0;
overflow: hidden;
transition: all 0.3s ease-in-out;
}
& input[type="checkbox"] {
display: none;
&:checked {
& + label {
& + .ac_cont {
min-block-size: fit-content;
padding-block: 2rem;
border-bottom: #bbb 1px solid;
}
& span::after {
rotate: 0deg;
}
&::after {
background-color: rgb(60, 106, 83);
}
}
}
}
}
js
jQuery版
坊ちゃん
親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて一週間ほど腰を抜かした事がある。親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて一週間ほど腰を抜かした事がある。
Close
Close
徒然草
つれづれなるまゝに、日暮らし、硯にむかひて、心にうつりゆくよしなし事を、 そこはかとなく書きつくれば、あやしうこそものぐるほしけれ。
Close
Close
ポラーノの広場
つれづれなるまゝに、日暮らし、硯にむかひて、心にうつりゆくよしなし事を、 そこはかとなく書きつくれば、あやしうこそものぐるほしけれ。
Close
Close
HTML
<section class="accordion3">
<div class="htl_acd">
<div class="acd_head">坊ちゃん</div>
<div class="acd_cont">
親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて一週間ほど腰を抜かした事がある。親譲りの無鉄砲で小供の時から損ばかりしている。小学校に居る時分学校の二階から飛び降りて一週間ほど腰を抜かした事がある。<br />
<a href="#Close_point" class="close_btn">Close</a>
</div>
</div>
</section>
<section class="accordion3">
<div class="htl_acd">
<div class="acd_head">徒然草</div>
<div class="acd_cont">
つれづれなるまゝに、日暮らし、硯にむかひて、心にうつりゆくよしなし事を、 そこはかとなく書きつくれば、あやしうこそものぐるほしけれ。<br />
<a href="#Close_point" class="close_btn">Close</a>
</div>
</div>
</section>
<section class="accordion3">
<div class="htl_acd">
<div class="acd_head">ポラーノの広場</div>
<div class="acd_cont">
つれづれなるまゝに、日暮らし、硯にむかひて、心にうつりゆくよしなし事を、 そこはかとなく書きつくれば、あやしうこそものぐるほしけれ。<br />
<a href="#Close_point" class="close_btn">Close</a>
</div>
</div>
</section>
CSS
.accordion3 {
& .acd_head {
padding: 2rem;
display: grid;
grid-template-columns: 1fr 1em;
column-gap: 1em;
border-top: #bbb 1px solid;
cursor: pointer;
transition: all 0.3s ease;
&::after {
content: "";
background: #333;
height: 1em;
width: 1em;
clip-path: polygon(0 45%, 45% 45%, 45% 0, 55% 0, 55% 45%, 100% 45%, 100% 55%, 55% 55%, 55% 100%, 45% 100%, 44% 55%, 0 55%);
transition: all 0.3s ease-in-out;
}
&.active {
border-bottom: #ccc 1px solid;
&::after {
transform: rotate(45deg);
}
}
}
& .acd_cont {
display: none;
padding: 2rem;
& .close_btn {
border: 1px solid #aaa;
line-height: 1;
margin: 2em auto 0;
width: fit-content;
padding: 0.5em 1em 0.6em;
cursor: pointer;
display: grid;
grid-template-columns: 0.9em 1fr;
column-gap: 0.5em;
align-items: center;
&::before {
content: "";
background: #111;
height: 0.9em;
width: 0.9em;
clip-path: polygon(0 45%, 45% 45%, 45% 0, 55% 0, 55% 45%, 100% 45%, 100% 55%, 55% 55%, 55% 100%, 45% 100%, 44% 55%, 0 55%);
rotate: 45deg;
}
}
}
}
js
$(function () {
$(".accordion3 .acd_head").on("click", function () {
$(this).next(".acd_cont").slideToggle(400);
$(this).toggleClass("active");
});
$(".close_btn").on("click", function () {
$(this).parent(".acd_cont").slideUp(400);
$(this).parent().prev().removeClass("active");
var id = $(this).attr("href");
var position = $(id).offset().top;
$("html, body").animate(
{
scrollTop: position,
},
0 //固定Header数値
);
return false;
});
});

