gpt4 book ai didi

javascript - 为什么 GSAP 中的动画不反转?

转载 作者:行者123 更新时间:2023-12-02 01:35:02 25 4
gpt4 key购买 nike

我有问题,我想要反向动画 showMenuList (这是粉红色背景),但这不起作用,错误在哪里?我尝试了暂停,和很多方法,但这不起作用,请帮助

我知道这主要是代码,但我不知道还要写什么。在下面找到我的代码和 here Codepen 链接。

const menuToggler = document.querySelector(".toggler");
const menuTogglerHamburger = document.querySelector(".toggler .hamburger");

menuToggler.addEventListener("click", function () {
//checking if there is a class animationToggler
if (menuTogglerHamburger.classList.contains("animationToggler")) {
//restart animation rotate
menuTogglerHamburger.classList.remove("animationToggler");
void menuTogglerHamburger.offsetWidth;
menuTogglerHamburger.classList.add("animationToggler");
}

menuTogglerHamburger.classList.add("animationToggler");

menuToggler.classList.toggle("show");
menuToggler.classList.contains("show")
? showMenuList(true)
: showMenuList(false);

setTimeout(() => {
//animation line on the cross
menuTogglerHamburger.classList.toggle("active");
}, 200);
});

//---------------------- Show menu list ----------------------//

const showMenuList = (e) => {
console.log(e);
const showMenuTl = gsap.timeline({ pause: true });

e ? showMenuTl.play() : showMenuTl.reverse();

showMenuTl
.to(".nav-menu", 0.8, { css: { top: "50%" } })
.to(".nav-menu", 0.4, { css: { width: "140vw", height: "140vw" } });
};
body {
overflow: hidden;
}

nav {
display: flex;
width: 100%;
align-items: center;
justify-content: space-around;
padding-top: 1em;
}

.toggler {
display: flex;
align-items: center;
cursor: pointer;


}

.animationToggler {
animation: animationTogglerMenu .8s ease;
}


.toggler p {
margin: 0;
text-transform: uppercase;
font-size: 1.65rem;
margin: 0 0 0 10px;
z-index: 3;
}

.hamburger {
z-index: 3;
}

.hamburger .line {
height: 4px;
width: 2.5em;
background: #000;
margin: .45em 0;
border-radius: 50px;
transition: .3s;
}

.active .one {
transform: rotate(45deg) translateY(15px);
}

.active .two {
background-color: transparent;
transition: none;
}

.active .three {
transform: rotate(-45deg) translateY(-15px);
}

.nav-menu {
position: absolute;
top: -100%;
left: 50%;
transform: translate(-50%, -50%);
width: 3em;
height: 3em;
background-color: rgb(255, 117, 117);
border-radius: 50%;
/* opacity: 0.4; */
z-index: 2;
}

.nav-menu ul {
display: none;
}


@keyframes animationTogglerMenu {

100% {
transform: rotate(360deg);
}
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>

<body>
<div class="toggler">
<div class="hamburger">
<div class="line one"></div>
<div class="line two"></div>
<div class="line three"></div>
</div>
<p>Menu</p>
</div>
<div class="nav-menu">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Projekty</a></li>
<li><a href="#">O nas</a></li>
<li><a href="#">Kontakt</a></li>
</ul>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.10.4/gsap.min.js"></script>
<script src="main.js"></script>
</body>

</html>

最佳答案

代码的一个问题是您需要将 paused: true 而不是 pause:true 传递给时间线函数。

改进代码的一种方法(在我看来)是将时间线对象的创建移动到代码块的顶层。就像这样...

const menuToggler = document.querySelector(".toggler");
const menuTogglerHamburger = document.querySelector(".toggler .hamburger");

// Add it here -----------------------
const showMenuTl = gsap.timeline({ paused: true });
showMenuTl
.to(".nav-menu", 0.8, { css: { top: "50%" } })
.to(".nav-menu", 0.4, { css: { width: "140vw", height: "140vw" } });

然后你就可以摆脱showMenuList函数并直接调用playreverse

  menuToggler.classList.toggle("show");
menuToggler.classList.contains("show")
? showMenuTl.play()
: showMenuTl.reverse()

关于javascript - 为什么 GSAP 中的动画不反转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72550748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com