对同一CSS动画的多个实例进行动画处理

时间:2020-03-22 08:00:20

标签: javascript html css

这里尝试为单个十字标记动画的三个实例设置动画。 如果我只有一个实例,则交叉动画效果很好,但是当我尝试创建多个实例时,我无法使其生效。另外,我找不到控制十字动画大小和位置的解决方案。我想念的是什么?

这是我的尝试:(比方说,我想为第二个交叉动画-TriggerB)

$("button").click(function(){
    $(".triggerB").toggleClass("drawn")
});
.container {
  width: 50%;
  max-width: 250px;
  margin: 0 auto;
}

.cross1{
    stroke-dasharray: 50;
    stroke-dashoffset: 50;
    -webkit-transition: stroke-dashoffset 1s 0.8s ease-out;
    -moz-transition: stroke-dashoffset 1s 0.8s ease-out;
    -ms-transition: stroke-dashoffset 1s 0.8s ease-out;
    -o-transition: stroke-dashoffset 1s 0.8s ease-out;
    transition: stroke-dashoffset 1s 0.8s ease-out;
}
.cross2{
    stroke-dasharray: 50;
    stroke-dashoffset: 50;
    -webkit-transition: stroke-dashoffset 1s 0.5s ease-out;
    -moz-transition: stroke-dashoffset 1s 0.5s ease-out;
    -ms-transition: stroke-dashoffset 1s 0.5s ease-out;
    -o-transition: stroke-dashoffset 1s 0.5s ease-out;
    transition: stroke-dashoffset 1s 0.5s ease-out;
}
.drawn + svg .path{
    opacity: 1;
    stroke-dashoffset: 0;
}

.triggerA {
  left:100px;
}

.triggerB {
  left:400px;
}
.triggerC {
  left:700px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="triggerA"></div>
<div class="triggerB"></div>
<div class="triggerC"></div>
  
<svg version="1.1" id="tick" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 37 37" style="enable-background:new 0 0 37 37;" xml:space="preserve">

<polyline class="cross1 path" style="fill:none;stroke:#fc1c03;stroke-width:3;stroke-linejoin:round;stroke-miterlimit:10;" points="
	 12.5,24.5 24.5,12.5 "/>
  
  <polyline class="cross2 path" style="fill:none;stroke:#fc0303;stroke-width:3;stroke-linejoin:round;stroke-miterlimit:10;" points="
	 12.5,12.5 24.5,24.5 "/>
</svg>
  
</div> 
<button>go/reset</button>

我想使用这个:

$("button").click(function(){
    $(".triggerA").toggleClass("drawn")
    $(".triggerB").toggleClass("drawn")
    $(".triggerC").toggleClass("drawn")
});

1 个答案:

答案 0 :(得分:1)

$("button").click(function(){
    $(".triggerB").toggleClass("drawn")
});
.container {
  width: 50%;
  max-width: 250px;
  margin: 0 auto;
}

.cross1{
    stroke-dasharray: 50;
    stroke-dashoffset: 50;
    -webkit-transition: stroke-dashoffset 1s 0.8s ease-out;
    -moz-transition: stroke-dashoffset 1s 0.8s ease-out;
    -ms-transition: stroke-dashoffset 1s 0.8s ease-out;
    -o-transition: stroke-dashoffset 1s 0.8s ease-out;
    transition: stroke-dashoffset 1s 0.8s ease-out;
}
.cross2{
    stroke-dasharray: 50;
    stroke-dashoffset: 50;
    -webkit-transition: stroke-dashoffset 1s 0.5s ease-out;
    -moz-transition: stroke-dashoffset 1s 0.5s ease-out;
    -ms-transition: stroke-dashoffset 1s 0.5s ease-out;
    -o-transition: stroke-dashoffset 1s 0.5s ease-out;
    transition: stroke-dashoffset 1s 0.5s ease-out;
}
.drawn ~ svg .path{
    opacity: 1;
    stroke-dashoffset: 0;
}

.triggerA {
  left:100px;
}

.triggerB {
  left:400px;
}
.triggerC {
  left:700px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="triggerA"></div>
<div class="triggerB"></div>
<div class="triggerC"></div>
  
<svg version="1.1" id="tick" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 37 37" style="enable-background:new 0 0 37 37;" xml:space="preserve">

<polyline class="cross1 path" style="fill:none;stroke:#fc1c03;stroke-width:3;stroke-linejoin:round;stroke-miterlimit:10;" points="
	 12.5,24.5 24.5,12.5 "/>
  
  <polyline class="cross2 path" style="fill:none;stroke:#fc0303;stroke-width:3;stroke-linejoin:round;stroke-miterlimit:10;" points="
	 12.5,12.5 24.5,24.5 "/>
</svg>
  
</div> 
<button>go/reset</button>

这是CSS问题。尝试使用~代替+

.drawn ~ svg .path{
    opacity: 1;
    stroke-dashoffset: 0;
}