[英]How to get a click function to trigger function every time it is clicked
我有一个运行动画的点击功能。 单击后我遇到的问题是,如果我再次尝试单击该按钮,该功能将永远无法运行。
我使用addClass
方法来显示动画。 然后,我使用fadeOut
将其删除。 我尝试取出fadeOut
并将其替换为removeClass
,但这甚至不允许动画显示。
有谁知道我该怎么做才能解决此问题?
<button id="trigger">Trigger</button>
<div id="wrap">
<div id="checkmark-text">All Templates Selected</div>
<svg class="checkmark" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 52 52">
<circle class="checkmark-circle" cx="26" cy="26" r="25" fill="none"/>
<path class="checkmark-check" fill="none" d="M14.1 27.2l7.1 7.2 16.7-16.8"/>
</svg>
</div>
#wrap {
opacity: 0;
}
.checkmark {
width: 200px;
height: 200px;
border-radius: 50%;
display: block;
stroke-width: 5;
stroke: #fff;
stroke-miterlimit: 10;
margin: 10% auto;
box-shadow: inset 0px 0px 0px #0783a7;
z-index: 2;
}
.checkmark-circle {
stroke-dasharray: 166;
stroke-dashoffset: 166;
stroke-width: 5;
stroke-miterlimit: 10;
stroke: #0783a7;
fill: none;
}
.checkmark-check {
transform-origin: 50% 50%;
stroke-dasharray: 70;
stroke-dashoffset: 70;
}
@keyframes stroke {
100% {
stroke-dashoffset: 0;
}
}
@keyframes scale {
0%, 100% {
transform: none;
}
50% {
transform: scale3d(1.1, 1.1, 1);
}
}
@keyframes fill {
100% {
box-shadow: inset 0px 0px 0px 100px #0783a7;
}
}
#wrap.fadeIn {
opacity: 1;
transition: all 0.8s ease;
}
#wrap.fadeIn .checkmark {
animation: fill .4s ease-in-out .4s forwards, scale .3s ease-in-out .9s both;
}
#wrap.fadeIn .checkmark-circle {
animation: stroke 0.6s cubic-bezier(0.65, 0, 0.45, 1) forwards;
}
#wrap.fadeIn .checkmark-check {
animation: stroke 0.3s cubic-bezier(0.65, 0, 0.45, 1) 0.8s forwards;
}
$('#trigger').on('click', function () {
$('#wrap').addClass('fadeIn').delay(2000).removeClass('fadeIn');
});
在你的例子中
$('#wrap').addClass('fadeIn').delay(2000).fadeOut();
当您使用faseOut时,它将隐藏该元素。 您需要调用show()使其再次出现。
使用setTimeout
$('#trigger').on('click', function () { $('#wrap').addClass('fadeIn'); setTimeout(function(){ $('#wrap').removeClass('fadeIn'); },2000) });
#wrap { opacity: 0; } .checkmark { width: 200px; height: 200px; border-radius: 50%; display: block; stroke-width: 5; stroke: #fff; stroke-miterlimit: 10; margin: 10% auto; box-shadow: inset 0px 0px 0px #0783a7; z-index: 2; } .checkmark-circle { stroke-dasharray: 166; stroke-dashoffset: 166; stroke-width: 5; stroke-miterlimit: 10; stroke: #0783a7; fill: none; } .checkmark-check { transform-origin: 50% 50%; stroke-dasharray: 70; stroke-dashoffset: 70; } @keyframes stroke { 100% { stroke-dashoffset: 0; } } @keyframes scale { 0%, 100% { transform: none; } 50% { transform: scale3d(1.1, 1.1, 1); } } @keyframes fill { 100% { box-shadow: inset 0px 0px 0px 100px #0783a7; } } #wrap.fadeIn { opacity: 1; transition: all 0.8s ease; } #wrap.fadeIn .checkmark { animation: fill .4s ease-in-out .4s forwards, scale .3s ease-in-out .9s both; } #wrap.fadeIn .checkmark-circle { animation: stroke 0.6s cubic-bezier(0.65, 0, 0.45, 1) forwards; } #wrap.fadeIn .checkmark-check { animation: stroke 0.3s cubic-bezier(0.65, 0, 0.45, 1) 0.8s forwards; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="trigger">Trigger</button> <div id="wrap"> <div id="checkmark-text">All Templates Selected</div> <svg class="checkmark" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 52 52"> <circle class="checkmark-circle" cx="26" cy="26" r="25" fill="none"/> <path class="checkmark-check" fill="none" d="M14.1 27.2l7.1 7.2 16.7-16.8"/> </svg> </div>
您的问题是,一旦它消失并变得不可见,再也看不到它。
这是一个使用done
选项再次显示的解决方案:
$('#trigger').on('click', function () {
$('#wrap').addClass('fadeIn').delay(2000).fadeOut(0,done=function(){
$('#wrap').removeClass('fadeIn').show();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.