简体   繁体   English

如何获得单击功能以在每次单击时触发功能

[英]How to get a click function to trigger function every time it is clicked

I have a click function which runs an animation. 我有一个运行动画的点击功能。 The issue I am having is after it is clicked, if I try to click the button again the function never runs. 单击后我遇到的问题是,如果我再次尝试单击该按钮,该功能将永远无法运行。

Here is a fiddle for it. 这是一个小玩意儿。

I use the addClass method to get the animation to show. 我使用addClass方法来显示动画。 I then use fadeOut to remove it. 然后,我使用fadeOut将其删除。 I tried taking out the fadeOut and replacing it with removeClass , but that doesn't even allow the animation to show. 我尝试取出fadeOut并将其替换为removeClass ,但这甚至不允许动画显示。

Does anyone know what I have to do to fix this? 有谁知道我该怎么做才能解决此问题?

<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');
});

In your example you have 在你的例子中

$('#wrap').addClass('fadeIn').delay(2000).fadeOut();

when you use faseOut it will hide the element. 当您使用faseOut时,它将隐藏该元素。 You would need to call show() for it to appear again. 您需要调用show()使其再次出现。

Use setTimeout 使用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> 

Your issue is, once it fades out and becomes invisible nothing ever makes it visible again. 您的问题是,一旦它消失并变得不可见,再也看不到它。

Here's a solution that uses the done option to show it again: 这是一个使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM