每次单击时如何使点击功能触发功能

时间:2017-03-27 17:16:57

标签: javascript jquery onclick

我有一个运行动画的点击功能。我遇到的问题是点击后,如果我再次尝试再次点击该按钮,该功能永远不会运行。

Here is a fiddle for it.

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

3 个答案:

答案 0 :(得分:2)

在您的示例中,您有

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

当你使用faseOut时,它会隐藏元素。您需要调用show()才能再次显示。

答案 1 :(得分:1)

使用setTimeout

&#13;
&#13;
$('#trigger').on('click', function () {
	$('#wrap').addClass('fadeIn');
  setTimeout(function(){
  $('#wrap').removeClass('fadeIn');
  },2000)
});
&#13;
#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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

你的问题是,一旦它消失并变得不可见,就再也看不到它了。

以下是使用done选项再次显示的解决方案:

$('#trigger').on('click', function () {
$('#wrap').addClass('fadeIn').delay(2000).fadeOut(0,done=function(){
  $('#wrap').removeClass('fadeIn').show();
  });
});