gpt4 book ai didi

每次点击时 Jquery 触发动画(问题)

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

我需要在每次单击按钮时触发动画,而不是像下面的代码片段那样切换按钮类:

我尝试使用 addClass()removeClass() 类似的方法,但它没有按预期工作,代码如下:

$(document).ready(function () {
$("button[class*='navbar-toggler']").click(function () {
$(this).toggleClass("my-animation")
});
})
@keyframes my-animation {
16.65% {
transform: translateY(8px);
}

33.3% {
transform: translateY(-6px);
}

49.95% {
transform: translateY(4px);
}

66.6% {
transform: translateY(-2px);
}

83.25% {
transform: translateY(1px);
}

100% {
transform: translateY(0);
}
}

.my-animation {
animation-name: my-animation;
animation-duration: 1s;
animation-timing-function: ease-in-out;
animation-iteration-count: 1;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navToggler" aria-controls="navToggler" aria-expanded="false" aria-label="Toggle navigation">
<i class="fa fa-bars"></i>
</button>

最佳答案

使用 setTimeout 函数删除 my-animation 类并使用

.navbar-toggler{
outline:0!important;
}

$(document).ready(function () {
if(!$("button[class*='navbar-toggler']").hasClass('my-animation')){
$("button[class*='navbar-toggler']").click(function () {
$(this).addClass("my-animation");
setTimeout(function(){ $("button[class*='navbar-toggler']").removeClass("my-animation"); }, 1000);
});
}
})
@keyframes my-animation {
16.65% {
transform: translateY(8px);
}

33.3% {
transform: translateY(-6px);
}

49.95% {
transform: translateY(4px);
}

66.6% {
transform: translateY(-2px);
}

83.25% {
transform: translateY(1px);
}

100% {
transform: translateY(0);
}
}

.my-animation {
animation-name: my-animation;
animation-duration: 1s;
animation-timing-function: ease-in-out;
animation-iteration-count: 1;
}

.navbar-toggler{
outline:0!important;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navToggler" aria-controls="navToggler" aria-expanded="false" aria-label="Toggle navigation">
<i class="fa fa-bars"></i>
</button>

关于每次点击时 Jquery 触发动画(问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46561954/

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