Paul
Paul

Reputation: 3368

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. I then use fadeOut to remove it. I tried taking out the fadeOut and replacing it with removeClass, but that doesn't even allow the animation to show.

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

Upvotes: 0

Views: 331

Answers (3)

Aaron Brock
Aaron Brock

Reputation: 4536

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:

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

Upvotes: 0

Dan Philip Bejoy
Dan Philip Bejoy

Reputation: 4391

Use 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>

Upvotes: 1

epascarello
epascarello

Reputation: 207531

In your example you have

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

when you use faseOut it will hide the element. You would need to call show() for it to appear again.

Upvotes: 2

Related Questions