[英]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.