🐈
【GSAP】GSAP Practice【#12 GSAP Sample3 Drower Menu Animation2】
【#12 GSAP Sample3 Drower Menu Animation2】
YouTube: https://youtu.be/KhjZQdOJpn8
今回も引き続きGSAPを使用して、
ドロワーメニューを作成していきます。
今回は、JSの部分を実装します。
まずは、CSSを変更して、
メニューを閉じた状態にします。
css/main.css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
}
.section-1,
.section-2,
.section-3 {
width: 100%;
height: 100vh;
}
.section-1 {
background-color: salmon;
display: flex;
flex-direction: column;
gap: 20px;
align-items: center;
justify-content: center;
}
#loading {
color: white;
font-size: 60px;
display: flex;
}
#loading span {
display: block;
}
.section-2 {
background-color: skyblue;
display: flex;
flex-direction: column;
gap: 20px;
align-items: center;
justify-content: center;
}
#stroke-wrapper {
width: 1000px;
margin: 0 auto;
background-color: gray;
}
.section-3 {
background-color: violet;
display: flex;
flex-direction: column;
position: relative;
}
.sec3-nav {
width: 100%;
max-width: 1560px;
margin: 0 auto;
padding: 20px;
display: flex;
align-items: center;
justify-content: space-between;
}
.sec3-logo {
font-weight: bold;
font-size: 24px;
}
.ri-menu-line,
.ri-close-line {
font-size: 24px;
cursor: pointer;
font-weight: 500;
}
.menu-wrapper {
height: 100%;
width: 320px;
position: absolute;
top: 0;
right: -320px;
background-color: rgba(255, 255, 255, 0.8);
padding: 20px;
}
.menu-header {
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
font-size: 30px;
}
.menu-line {
width: 100%;
height: 1px;
margin: 14px 0;
background-color: rgba(255, 255, 255, 1);
}
.menu-list-wrapper {
list-style-type: none;
display: flex;
flex-direction: column;
gap: 20px;
font-size: 22px;
}
.menu-bg {
width: 100%;
height: 100%;
position: absolute;
top: 0;
right: -100%;
background-color: rgba(255, 77, 0, 0.6);
}
.box {
width: 160px;
height: 160px;
display: flex;
align-items: center;
justify-content: center;
color: white;
}
.box-1 {
background-color: red;
}
.box-2 {
background-color: blue;
}
.box-3 {
background-color: green;
}
.box-4 {
background-color: purple;
}
動画内138行目の「dutarion」の部分が誤りで、
正しくは「duration」になります。
js/main.js
// use a script tag or an external JS file
document.addEventListener("DOMContentLoaded", (event) => {
gsap.registerPlugin(ScrollTrigger);
new App();
});
class App {
constructor() {
this._initialize();
this._renderLenis();
}
_initialize() {
this._createLenis();
this._sec1Anime();
this._sec2Anime();
this._sec3Anime();
}
_createLenis() {
this.lenis = new Lenis();
}
_renderLenis(time) {
this.lenis.raf(time);
requestAnimationFrame(this._renderLenis.bind(this));
}
_sec1Anime() {
const loadingEl = document.querySelector("#loading");
const loadingTexts = loadingEl.textContent.split("");
let innerText = "";
loadingTexts.forEach((text) => {
innerText += `<span>${text}</span>`;
});
// console.log(loadingTexts);
// console.log(innerText);
loadingEl.innerHTML = innerText;
const tl = gsap.timeline({ repeat: -1 });
tl.to("#loading span", {
duration: 1,
y: -50,
stagger: {
each: 0.2,
from: "left",
},
}).to("#loading span", {
duration: 1,
y: 0,
stagger: {
each: 0.2,
from: "left",
},
ease: "bounce.out",
});
}
_sec2Anime() {
const strokeEl = document.querySelector("#stroke-wrapper");
const strokeElHeight = strokeEl.clientHeight;
const rectX = strokeEl.getBoundingClientRect().left;
const rectY = strokeEl.getBoundingClientRect().top;
let orgPath = `M 20 ${strokeElHeight / 2} Q 480 ${strokeElHeight / 2} 980 ${
strokeElHeight / 2
}`;
gsap.set("svg path", {
attr: {
d: orgPath,
},
});
strokeEl.addEventListener("mousemove", (e) => {
let strokeQY = strokeElHeight / 2;
let strokeQX = e.x - rectX;
let strokeRectY = e.y - rectY;
if (strokeRectY > strokeElHeight / 2) {
strokeQY = strokeRectY - strokeElHeight / 2;
} else {
strokeQY = strokeRectY + strokeElHeight / 2;
}
let path = `M 20 ${strokeElHeight / 2} Q ${strokeQX} ${strokeQY} 980 ${
strokeElHeight / 2
}`;
gsap.to("svg path", {
attr: {
d: path,
},
duration: 0.3,
ease: "power3.out",
});
});
strokeEl.addEventListener("mouseleave", () => {
gsap.to("svg path", {
attr: {
d: orgPath,
},
duration: 1.5,
ease: "elastic.out(1,0.3)",
});
});
}
_sec3Anime() {
const menuOpen = document.querySelector("#menu-open");
const menuClose = document.querySelector("#menu-close");
const menuBg = document.querySelector(".menu-bg");
const tl = gsap.timeline();
tl.to(".menu-wrapper", {
right: 0,
duration: 0.7,
ease: "power3.out",
});
tl.to(
".menu-bg",
{
right: 0,
duration: 0.7,
ease: "power3.out",
},
0.5
);
tl.from(
".menu-list",
{
x: 150,
opacity: 0,
duration: 0.2,
stagger: {
each: 0.2,
},
},
0.5
);
tl.pause();
menuOpen.addEventListener("click", () => {
tl.play();
});
menuClose.addEventListener("click", () => {
tl.reverse();
});
menuBg.addEventListener("click", () => {
tl.reverse();
});
}
}
Discussion