ããããã¬ãã£ã¬ãã£åããããã¨ããè¦æã®ãã¼ã ãã¼ã¸å¶ä½(é React)ã§åã㦠GSAP ã¨ããã¢ãã¡ã¼ã·ã§ã³ã©ã¤ãã©ãªã使ã£ã¦ãã ã§ãããã¨ãããããã¦ãã¾ã ç解ãããã¦ãªããä»å㯠ScrollTrigger ã使ã£ã¦ CSS ã®ã¯ã©ã¹ãå¤æ´ãã¢ãã¡ã¼ã·ã§ã³ãçºç«ãããå¾ã«ãã¢ãã¡ã¼ã·ã§ã³çµäºãæã£ã¦ã³ã¼ã«ããã¯ãå®è¡ãããã£ãã®ã¡ã¢
ã©ã¤ãã©ãªé¸å®ã®ã¡ã¢
ãã©ã©ãã¯ã¹ã»ã¢ãã¡ã¼ã·ã§ã³ã©ã¤ãã©ãªã§æ¯è¼ããã
aos vs gsap vs scrollmagic vs t-scroll vs velocityjs vs waypoints vs wowjs | npm trends
å¤ãã®ã©ã¤ãã©ãªã DT ã§ãã¾ãã¡ã³ãããã¦ããããã»ã¼å¯ä¸ GSAP ã TypeScript åããã¦ããæ¯è¼çã¡ã³ããã³ã¹ãããã¦ããã®ã§é¸å®ãã
JavaScript ã« Intersection information ãããã®ã§ãããç¨åº¦ã®ãã¨ã¯ããã§å®è£ å¯è½ã ããæ¨ä»ã® Web ã¢ããªã±ã¼ã·ã§ã³ã§ã¯ã¬ãã£ã¬ãã£åãã®ã¯ãã¾ãæå³ããªããªãè£ é£¾ãªã®ã§ãã®åéã®éè¦ãæ¸å°ãã¦ããã®ããç¥ããªããã¨ãã£ã¦ãåºåç㪠"ã»ãããºã¼ã" ã§ã¯ã¾ã ã¾ã å¿ è¦ã¨ãããåéã ã¨ãæã (jQueryã ã£ã¦ãã¡ããã¡ã使ããã¦ãã)
ScrollTrigger ã使ã£ã¦ã¯ã©ã¹åãå¤æ´ã CSS ã¢ãã¡ã¼ã·ã§ã³ãè¨å®ãã
ã·ã³ãã«ãª CSS ã¢ãã¡ã¼ã·ã§ã³ãä½æãã
.js-fadeUp { opacity: 0; transform: translateY(20px); transition: all 0.6s ease-in-out; } .js-fadeUp.fade-in { opacity: 1; transform: translateY(0); }
.js-fadeup
ã¯ã©ã¹ãããè¦ç´ ãã¿ã¼ã²ããã«ã.fade-in
ã¯ã©ã¹ãä»ããããã¨ã¢ãã¡ã¼ã·ã§ã³ãéå§ãããæ³å®
GSAP ã§ã¯ã©ã¹åãå¤æ´ãã
import gsap from 'gsap'; import { ScrollTrigger } from 'gsap/ScrollTrigger'; // registerPlugin ããªãã¨ã¨ã©ã¼ã«ãªã gsap.registerPlugin(ScrollTrigger); const targets = document.querySelectorAll('.js-fadeUp'); targets.forEach((target, index) => { ScrollTrigger.create({ trigger: target, id: `fadeUpContent-${index + 1}`, start: 'top-=100 center', end: 'bottom top', toggleClass: { targets: target, className: 'fade-in' }, once: true, }); });
ãã㧠.js-fadeup
ã¯ã©ã¹ã®ããè¦ç´ ãã¹ã¯ãã¼ã«ã«å¿ãã¦ãã§ã¼ãã¤ã³ããããã«ãªã£ãï¼
ð note.
once
: Boolean - Iftrue
, the ScrollTrigger will kill() itself as soon as the end position is reached once. This causes it to stop listening for scroll events and it becomes eligible for garbage collection. This will only call onEnter a maximum of one time as well. It does not kill the associated animation. It's perfect for times when you only want an animation to play once when scrolling forward and never get reset or replayed. It also sets the toggleActions to "play none none none".
cf. ScrollTrigger | GSAP | Docs & Learning
once
ãä»ãã¦ããã¨ä¸åº¦ã ãçºç«ããkill()
ããã¡ã¢ãªãéæ¾ãã¦ãããï¾ï¾ï½ºï¾ã
ãã£ã¦ãã¨ããã¢ãã¡ã¼ã·ã§ã³ãä½åº¦ãè¦ããããã®ã¯ããã¾ãã¦ã¼ã¶ã¼ä½é¨ãè¯ãã¨ã¯æããªãã®ã§ once
ãè¨å®ãããã¨ã«ããã
CSS ã¢ãã¡ã¼ã·ã§ã³ãå®äºãããã³ã¼ã«ããã¯ã§å¯¾è±¡è¦ç´ ãæä½ããã
ã¢ãã¡ã¼ã·ã§ã³å®äºæã« CSS ã®ã¯ã©ã¹åãå¤æ´ãããªã©ãã¢ãã¡ã¼ã·ã§ã³å®äºã®ã³ã¼ã«ããã¯ã§è¦ç´ ãæä½ããã
ScrollTrigger: onComplete callback - GSAP - GreenSock ãã®ã¹ã¬ããã«ã¯ refresh
ã¤ãã³ããªã¹ãã¼ã使ãã°è¯ãã¨åçããã¦ããããã¾ãåä½ãããscrollEnd
ã使ã£ã¦ã¿ãããã¢ãã¡ã¼ã·ã§ã³ãçºç«ããè¦ç´ ãã¨ã«ã¤ãã³ããçºç«ãããã®ã§ã¯ç¡ãããã ã£ãã
cf. static-addEventListener | GSAP | Docs & Learning
onEnter, onLeave ã³ã¼ã«ããã¯ã使ãã°ã対象ãã¨ã«ã³ã¼ã«ããã¯ãè¨å®ã§ãã
onEnter
: Function - A callback for when the scroll position moves forward past the "start" (typically when the trigger is scrolled into view). It receives one parameteronLeave
: Function - A callback for when the scroll position moves forward past the "end" (typically when the trigger is scrolled out of view). It receives one parameter
cf. static-addEventListener | GSAP | Docs & Learning
CSS ã¢ãã¡ã¼ã·ã§ã³ã®æéã測ã£ã¦å³ã³ã¼ã«ããã¯ããããªã onEnter
ã使ãã³ã¼ã«ããã¯å
㧠setTimeout
㧠CSS ã¢ãã¡ã¼ã·ã§ã³ã®æéåå¾
ã¦ã°è¯ããã
å³å¯ãããã¾ãå¿
è¦ã§ãªããªããonLeave
ã使ãæ¹ãç¡é§ã«ã¿ã¤ãã¼ã使ããã«æ¸ãã®ã§å¹ççã ã¨æã
import gsap from 'gsap'; import { ScrollTrigger } from 'gsap/ScrollTrigger'; gsap.registerPlugin(ScrollTrigger); const targets = document.querySelectorAll('.js-fadeUp'); targets.forEach((target, index) => { ScrollTrigger.create({ trigger: target, id: `fadeUpContent-${index + 1}`, start: 'top-=100 center', end: 'bottom top', toggleClass: { targets: target, className: 'fade-in' }, once: true, // css ã¢ãã¡ã¼ã·ã§ã³ãå¾ ã£ã¦å®è¡ãããå ´å㯠onEnter ã使ã onEnter: (self) => { // self 㯠globalThis.ScrollTrigger å const target = self.vars.trigger; // gsap.DOMTarget | undefined // â HTMLElement åã¨ãã¦æ±ããªã㨠target.classList ã type error ã«ãªã if (!target || target instanceof HTMLElement !== true) { return; } // css animation ãçµããã¾ã§å¾ 㤠window.setTimeout(() => { target.classList.add('fade-in-complete'); }, 600); }, // css ã¢ãã¡ã¼ã·ã§ã³ãç¹ã«èæ ®ããªããªã onLeave ã§åå onLeave: (self) => { const target = self.vars.trigger; if (!target || target instanceof HTMLElement !== true) { return; } target.classList.add('leaved'); } }); });
ãã¤ã³ãã¨ãã¦ã¯ self.vars.trigger
ã§åå¾ã§ãããã¢ãã¡ã¼ã·ã§ã³ã®ã¿ã¼ã²ããè¦ç´ ã gsap.DOMTarget | undefined
åãªã®ã§ classList
ãªã©ã使ãããå ´å㯠target instanceof HTMLElement
㧠type guard ãã¦ãããå¿
è¦ãããé¨å
Sample
See the Pen GSAP CSS animation callback by KIKIKI (@kikiki_kiki) on CodePen.
â onLeave
ã¯ã¹ã¯ãã¼ã«ã®é«ãã足ããªãã¨çºç«ããªãå ´åããã
ãªãã¨ãªããªç解ã ãã©ãGSAP ã® ScrollTrigger ã使ã£ã¦ CSS ã¢ãã¡ã¼ã·ã§ã³çºç«ããå¾ã«ã³ã¼ã«ããã¯ã§å¦çãã§ããããã«ãªã£ãï¼
ããã
[åè]
- aos vs gsap vs scrollmagic vs t-scroll vs velocityjs vs waypoints vs wowjs | npm trends
- Homepage | GSAP
- GitHub - greensock/GSAP: GSAP (GreenSock Animation Platform), a JavaScript animation library for the modern web
- GSAP | Docs & Learning
- 【2021年最新】GSAPの「ScrollTrigger」を使って爆速でスクロールアニメを実装する - to-R Media
- GSAPのScrollTriggerを使用してスクロールアニメーションを実装してみた - Onesite
- GSAPを使って複雑なオープニングアニメーションを作ってみよう | 東京のホームページ制作 / WEB制作会社 BRISK
- 【GSAP】スクロールでふわっとフェードインさせる方法【アニメーションのサンプルあり】 - じゅんぺいブログ
- 交差オブザーバー API - Web API | MDN
- JSでのスクロール連動エフェクトにはIntersection Observerが便利 - ICS MEDIA
- <easing-function> - CSS: カスケーディングスタイルシート | MDN