gpt4 book ai didi

javascript - 页面加载后,如何让我的弹跳汉堡菜单在经过一定秒数后停止弹跳?

转载 作者:行者123 更新时间:2023-12-01 08:29:39 24 4
gpt4 key购买 nike

我是网络开发新手,我正在使用不同的在线平台(YouTube、Udemy、StackSkills 等)自学。

现在我正努力专注于学习 HTML、CSS 和 JavaScript/JQuery 的基础知识。

我为一个自定义网站创建了这个汉堡菜单,我正在开发这个网站来帮助我学习,我想尝试让弹跳汉堡菜单在经过一定时间阈值后停止。

我尝试使用 JQuery 创建一个类,然后我可以使用 CSS 动画持续时间属性,但它完全停止了反弹。

这就是我使用 JQuery 和 CSS 尝试获得我想要的效果,完全停止弹跳动画效果,而不是让它在 5 秒后停止:

JQuery

function bounceDuration() {
document.querySelector('.hamburger-menu').classList.toggle('bounce-duration');

};

CSS

.hamburger-menu.bounce-duration {
animation-duration: 5s;

}

下面您将找到我当前的完整工作代码(HTML、CSS 和 JQuery)。正如您所看到的,汉堡菜单无限期地弹跳,我想以某种方式给它一个超时或某种持续时间。非常感谢任何对此的帮助。

function sidebarToggle() {
document.querySelector(".hamburger-menu").addEventListener("click", () => {
document.querySelector('.hamburger-menu').classList.toggle('bounce-stop');
document.querySelector(".container").classList.toggle("sidebar-toggle");
});
}

sidebarToggle()
* {
margin: 0;
padding: 0;
outline: none;
box-sizing: border-box;
list-style: none;
text-decoration: none;
}

.hamburger-menu {
width: 3rem;
height: 3rem;
position: fixed;
top: 5rem;
right: 5rem;
z-index: 200;
display: flex;
flex-direction: column;
justify-content: space-evenly;
cursor: pointer;
transition: 0.7s;
}

.hamburger-menu.bounce-stop {
animation-name: none;
}

.line {
width: 100%;
height: 0.2rem;
background-color: #fff;
box-shadow: 0 0.1rem 0.2rem rgba(0, 0, 0, 0.2);
}

/*
Hamburger Menu Bounce
---------------------
Description: - Up/Down animation
*/

.hamburger-menu {
-moz-animation: bounce 1s infinite alternate;
-o-animation: bounce 1s infinite alternate;
-webkit-animation: bounce 1s infinite alternate;
animation: bounce 1s infinite alternate;
animation-duration: 0.5s;
}

@-moz-keyframes bounce {
0% {
transform: translateY(0);
}

100% {
transform: translateY(-10px);
}
}

@-o-keyframes bounce {
0% {
transform: translateY(0);
}

100% {
transform: translateY(-10px);
}
}

@-webkit-keyframes bounce {
0% {
transform: translateY(0);
}

100% {
transform: translateY(-10px);
}
}

@keyframes bounce {
0% {
transform: translateY(0);
}

100% {
transform: translateY(-10px);
}
}

.sidebar-toggle .hamburger-menu {
right: 33rem;
background-color: #555;
}

.header {
width: 100%;
height: 100vh;
position: relative;
perspective: 100rem;
overflow: hidden;
background-color: rgba(0, 0, 0, .8);
}

.sidebar {
width: 40rem;
height: 100vh;
position: fixed;
top: 0;
right: -40rem;
background-color: #ffffff;
transition: right 0.5s;
}

.sidebar-toggle .sidebar {
right: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<body>
<div class="container">
<div class="hamburger-menu">
<div class="line line-1"></div>
<div class="line line-2"></div>
<div class="line line-3"></div>
</div>
<header class="header"></header>
<section class="sidebar"></section>
</div>
</body>
</html>

最佳答案

将迭代计数设置为2(或任何其他数字)而不是无限:

function sidebarToggle() {
document.querySelector(".hamburger-menu").addEventListener("click", () => {
document.querySelector('.hamburger-menu').classList.toggle('bounce-stop');
document.querySelector(".container").classList.toggle("sidebar-toggle");
});
}

sidebarToggle()
* {
margin: 0;
padding: 0;
outline: none;
box-sizing: border-box;
list-style: none;
text-decoration: none;
}

.hamburger-menu {
width: 3rem;
height: 3rem;
position: fixed;
top: 5rem;
right: 5rem;
z-index: 200;
display: flex;
flex-direction: column;
justify-content: space-evenly;
cursor: pointer;
transition: 0.7s;
}

.hamburger-menu.bounce-stop {
animation-name: none;
}

.line {
width: 100%;
height: 0.2rem;
background-color: #fff;
box-shadow: 0 0.1rem 0.2rem rgba(0, 0, 0, 0.2);
}

/*
Hamburger Menu Bounce
---------------------
Description: - Up/Down animation
*/

.hamburger-menu {
-moz-animation: bounce 1s 2 alternate;
-o-animation: bounce 1s 2 alternate;
-webkit-animation: bounce 1s 2 alternate;
animation: bounce 1s 2 alternate;
animation-duration: 0.5s;
}

@-moz-keyframes bounce {
0% {
transform: translateY(0);
}

100% {
transform: translateY(-10px);
}
}

@-o-keyframes bounce {
0% {
transform: translateY(0);
}

100% {
transform: translateY(-10px);
}
}

@-webkit-keyframes bounce {
0% {
transform: translateY(0);
}

100% {
transform: translateY(-10px);
}
}

@keyframes bounce {
0% {
transform: translateY(0);
}

100% {
transform: translateY(-10px);
}
}

.sidebar-toggle .hamburger-menu {
right: 33rem;
background-color: #555;
}

.header {
width: 100%;
height: 100vh;
position: relative;
perspective: 100rem;
overflow: hidden;
background-color: rgba(0, 0, 0, .8);
}

.sidebar {
width: 40rem;
height: 100vh;
position: fixed;
top: 0;
right: -40rem;
background-color: #ffffff;
transition: right 0.5s;
}

.sidebar-toggle .sidebar {
right: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<body>
<div class="container">
<div class="hamburger-menu">
<div class="line line-1"></div>
<div class="line line-2"></div>
<div class="line line-3"></div>
</div>
<header class="header"></header>
<section class="sidebar"></section>
</div>
</body>
</html>

关于javascript - 页面加载后,如何让我的弹跳汉堡菜单在经过一定秒数后停止弹跳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61902107/

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