-
Notifications
You must be signed in to change notification settings - Fork 5
/
webAnimationsApiAccordion.js
98 lines (85 loc) · 3.13 KB
/
webAnimationsApiAccordion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
document.addEventListener("DOMContentLoaded", () => {
setUpAccordion();
});
/**
* ブラウザの標準機能(Web Animations API)を使ってアコーディオンのアニメーションを制御します
*/
const setUpAccordion = () => {
const details = document.querySelectorAll(".js-details");
const RUNNING_VALUE = "running"; // アニメーション実行中のときに付与する予定のカスタムデータ属性の値
const IS_OPENED_CLASS = "is-opened"; // アイコン操作用のクラス名
details.forEach((element) => {
const summary = element.querySelector(".js-summary");
const content = element.querySelector(".js-content");
summary.addEventListener("click", (event) => {
// デフォルトの挙動を無効化
event.preventDefault();
// 連打防止用。アニメーション中だったらクリックイベントを受け付けないでリターンする
if (element.dataset.animStatus === RUNNING_VALUE) {
return;
}
// detailsのopen属性を判定
if (element.open) {
// アコーディオンを閉じるときの処理
// アイコン操作用クラスを切り替える(クラスを取り除く)
element.classList.toggle(IS_OPENED_CLASS);
// アニメーションを実行
const closingAnim = content.animate(closingAnimKeyframes(content), animTiming);
// アニメーション実行中用の値を付与
element.dataset.animStatus = RUNNING_VALUE;
// アニメーションの完了後に
closingAnim.onfinish = () => {
// open属性を取り除く
element.removeAttribute("open");
// アニメーション実行中用の値を取り除く
element.dataset.animStatus = "";
};
} else {
// アコーディオンを開くときの処理
// open属性を付与
element.setAttribute("open", "true");
// アイコン操作用クラスを切り替える(クラスを付与)
element.classList.toggle(IS_OPENED_CLASS);
// アニメーションを実行
const openingAnim = content.animate(openingAnimKeyframes(content), animTiming);
// アニメーション実行中用の値を入れる
element.dataset.animStatus = RUNNING_VALUE;
// アニメーション完了後にアニメーション実行中用の値を取り除く
openingAnim.onfinish = () => {
element.dataset.animStatus = "";
};
}
});
});
}
/**
* アニメーションの時間とイージング
*/
const animTiming = {
duration: 400,
easing: "ease-out"
};
/**
* アコーディオンを閉じるときのキーフレーム
*/
const closingAnimKeyframes = (content) => [
{
height: content.offsetHeight + 'px', // height: "auto"だとうまく計算されないため要素の高さを指定する
opacity: 1,
}, {
height: 0,
opacity: 0,
}
];
/**
* アコーディオンを開くときのキーフレーム
*/
const openingAnimKeyframes = (content) => [
{
height: 0,
opacity: 0,
}, {
height: content.offsetHeight + 'px',
opacity: 1,
}
];