点击触发器不保持完整动画

时间:2017-03-27 15:18:33

标签: javascript jquery animation onclick css-transitions

我用按钮触发svg checmark。我这样做的方法是将复选标记设置为opacity:0;,然后点击该按钮后,添加一个类fadeIn并将不透明度设置为opacity:1;

我的问题是当不透明度设置为0时,动画会被更改。完整的动画未显示。你可以通过将opacity:0从chechmark类中取出并再次运行小提琴来看到这一点。

我的问题是,在保持完整的动画序列的同时,如何使用按钮触发/显示此复选标记。

Here is a fiddle.该代码段未生成复选标记,不确定原因。

$('#trigger').on('click', function () {
	$('.checkmark').addClass('fadeIn');
});
.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;
  animation: fill .4s ease-in-out .4s forwards, scale .3s ease-in-out .9s both;
  opacity: 0;
  z-index: 2;
}
.checkmark-circle {
  stroke-dasharray: 166;
  stroke-dashoffset: 166;
  stroke-width: 5;
  stroke-miterlimit: 10;
  stroke: #0783a7;
  fill: none;
  animation: stroke 0.6s cubic-bezier(0.65, 0, 0.45, 1) forwards;
}
.checkmark-check {
  transform-origin: 50% 50%;
  stroke-dasharray: 70;
  stroke-dashoffset: 70;
  animation: stroke 0.3s cubic-bezier(0.65, 0, 0.45, 1) 0.8s forwards;
}

@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;
  }
}


.fadeIn {
    opacity: 1;
    transition: all 0.8s ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.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>

1 个答案:

答案 0 :(得分:1)

您可以通过基于添加animation类对所需元素运行.fadeIn规则来解决此问题,而不是将其放在元素加载时所具有的类上。试试这个:

.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;
    opacity: 0;
}
.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;
}

.checkmark.fadeIn {
    opacity: 1;
    transition: all 0.8s ease;
    animation: fill .4s ease-in-out .4s forwards, scale .3s ease-in-out .9s both;
}
.checkmark.fadeIn .checkmark-circle {
    animation: stroke 0.6s cubic-bezier(0.65, 0, 0.45, 1) forwards;
}
.checkmark.fadeIn .checkmark-check {
    animation: stroke 0.3s cubic-bezier(0.65, 0, 0.45, 1) 0.8s forwards;
}

@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;
    }
}

Updated fiddle