gpt4 book ai didi

javascript - .animate 不重复 .toggle 点击事件

转载 作者:太空宇宙 更新时间:2023-11-04 08:58:05 25 4
gpt4 key购买 nike

我有一个最初隐藏的菜单部分,但只要点击汉堡包/按钮,它就会使用 jquery 的 .animate 函数从其原始位置 70px 动画到 90px,是的,它在第一次运行时用于切换,但是当我通过单击按钮隐藏菜单,然后再次尝试单击按钮再次显示它时,.animate 功能不会启动。

这是 DEMO

代码

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>

<button class="menu-ham hamburger hamburger--squeeze" type="button" style="color:#fff">
<span class="hamburger-box">
<span class="hamburger-inner">click</span>
</span>
</button>

<nav class="nav" role="navigation">
<ul><li id="menu-item-147" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-147"><a href="#app_feature">Features</a></li>
<li id="menu-item-148" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-148"><a href="#app_why">Why</a></li>
<li id="menu-item-149" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-149"><a href="#app_faq">FAQ</a></li>
<li id="menu-item-150" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-150"><a href="#app_team">Team</a></li>
<li id="menu-item-136" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-136"><a href="#app_footer">Contact</a></li>
</ul> </nav>
</body>
</html>


.nav {
width: 80%;
float: left;
position: absolute;
top: 62px;
left: 70px;
display: none;
transition: all 0.3s; }
@media screen and (max-width: 992px) {
.nav {
top: 40px;
left: 70px; } }
.nav ul {
list-style: none; }
.nav ul li {
list-style: none;
display: inline-block;
padding: 0 18px; }
.nav ul li a {
font-size: 18px;
color: #000;
font-weight: 400; }
.nav ul li a:hover {
text-decoration: none; }
.nav ul li a:active {
text-decoration: none; }
.nav ul li a:visited {
text-decoration: none; }
.nav ul li a:focus {
text-decoration: none; }



.hamburger:hover {
color: #000;
}

Javascript

$(function () {
var $hamburger = $(".hamburger");
$hamburger.on("click", function(e) {
e.preventDefault();
$hamburger.toggleClass("is-active");
$(".nav").toggle().animate({
left: "90px"
}, 300 ).addClass("animated fadeIn");
});
});

最佳答案

您可以使用 boolean 变量检查它是否已设置动画,然后根据该变量重新定位导航。请阅读下面的工作片段:

$(function() {
var isAnimated = false;
var $hamburger = $(".hamburger");
$hamburger.on("click", function(e) {
e.preventDefault();
$hamburger.toggleClass("is-active");
if (!isAnimated) {
$(".nav").show().animate({
left: "90px"
}, 300).addClass("animated fadeIn");
isAnimated = true;
} else {
$(".nav").hide().animate({
left: "70px"
}, 300).removeClass("animated fadeIn");
isAnimated = false;
}
});
});
.nav {
width: 80%;
float: left;
position: absolute;
top: 62px;
left: 70px;
display: none;
transition: all 0.3s;
}

@media screen and (max-width: 992px) {
.nav {
top: 40px;
left: 70px;
}
}

.nav ul {
list-style: none;
}

.nav ul li {
list-style: none;
display: inline-block;
padding: 0 18px;
}

.nav ul li a {
font-size: 18px;
color: #000;
font-weight: 400;
}

.nav ul li a:hover {
text-decoration: none;
}

.nav ul li a:active {
text-decoration: none;
}

.nav ul li a:visited {
text-decoration: none;
}

.nav ul li a:focus {
text-decoration: none;
}

.hamburger:hover {
color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="menu-ham hamburger hamburger--squeeze" type="button" style="color:#fff">
<span class="hamburger-box">
<span class="hamburger-inner">click</span>
</span>
</button>

<nav class="nav" role="navigation">
<ul>
<li id="menu-item-147" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-147"><a href="#app_feature">Features</a></li>
<li id="menu-item-148" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-148"><a href="#app_why">Why</a></li>
<li id="menu-item-149" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-149"><a href="#app_faq">FAQ</a></li>
<li id="menu-item-150" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-150"><a href="#app_team">Team</a></li>
<li id="menu-item-136" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-136"><a href="#app_footer">Contact</a></li>
</ul>
</nav>

关于javascript - .animate 不重复 .toggle 点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42973918/

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