-
Notifications
You must be signed in to change notification settings - Fork 0
/
cardList.js
53 lines (44 loc) · 1.56 KB
/
cardList.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
import { Ease24, Tween24 } from "tween24";
/**
* 交差時のコールバック関数
* @param entries
*/
const intersectionCallback = (entries) => {
entries.forEach((entry, index) => {
if (entry.isIntersecting) {
// 交差した要素のdata属性からtweenIdを取得
const tweenId = entry.target.dataset.tweenId;
// 200ミリ秒ずつ遅延させて実行
setTimeout(() => {
Tween24.playById(tweenId);
}, 200 * index);
}
});
};
/**
* Intersection Observerを作成
* @type {IntersectionObserver}
*/
const observer = new IntersectionObserver(intersectionCallback, {
// 見えてから実行したいので、画面下よりも少し上に範囲を設定
rootMargin: "0px 0px -15%",
});
// カードコンポーネントの全要素
const cardElements = document.querySelectorAll(".card");
// Tweenアニメーションを作成
cardElements.forEach((cardElement, index) => {
const children = cardElement.querySelectorAll("*");
Tween24.prop(Array.from(children)).opacity(0).y(40).play();
// カード内フェードTweenアニメーション
const fadeTween = Tween24.tween(Array.from(children), 1, Ease24._4_QuartOut)
.opacity(1)
.y(0);
// 個々のフェードTweenを連続的に実行
const cardTween = Tween24.lag(0.1, fadeTween);
// Tweenアニメーションにidを付与
cardTween.id(`${index}`);
// Intersection Observer側から読み出せるようにdata属性にセット
cardElement.dataset.tweenId = `${index}`;
// オブザーバーで監視
observer.observe(cardElement);
});