how to reverse CSS animation on second click with jQuery

对着背影说爱祢 提交于 2020-01-03 18:51:13

问题


I have made the following menu icon CSS animation that triggers when i click on it. I want to make it animate in reverse when i click on it for the second time using jQuery.

#path1 {
  stroke-dasharray: 33px;
  stroke-dashoffset: 33px;
  animation: line 1.5s linear forwards;
  animation-play-state: paused;
}

@keyframes line {
  100% {
    stroke-dashoffset: -15.5;
  }
}

#halfLineLeft {
  transform-origin: 1% 50%;
  animation: shrinkleft 1s linear forwards;
  animation-play-state: paused;
}

 #halfLineRight {
  transform-origin: 100% 1%;
  animation: shrinkleft 1s linear forwards;
  animation-play-state: paused;
}

@keyframes shrinkleft {
  100% {
    transform: scale(0,1);
  }
}
svg {
  transform: translate(350px, 200px);
}

and this is the jQuery i have so far...

$("svg").click(
  function(){
     $("#path1, #halfLineLeft, #halfLineRight").css("animation-play-state", "running");
  }
 );

I cant seem to figure out how to make it animate in reverse when i click on the SVG icon the second time. I tried this code without any luck:

$("svg").click(
  function(){
     $("#path1, #halfLineLeft, #halfLineRight").css("animation-state", "running"),
     $("#path1, #halfLineLeft, #halfLineRight").css("animation-direction", "reverse");
  }
 );

Here is the codepen link with the live animation:

http://codepen.io/anon/pen/xEORBJ


回答1:


I don't know if this is the most efficient way, but adding reverse keyframes and toggling classes seems to work.

codepen example

#path1.active {
  animation: line 1.5s forwards;
}

#path1.inactive {
  stroke-dashoffset: -15.5;
  animation: unline 1s linear forwards;
}

#halfLineLeft.active {
  animation: shrinkleft 1s linear forwards;
}

#halfLineRight.active {
  animation: shrinkleft 1s linear forwards;
}

#halfLineLeft.inactive {
  transform: scale(0, 1);
  animation: unshrinkleft 1s linear forwards;
}

#halfLineRight.inactive {
  transform: scale(0, 1);
  animation: unshrinkleft 1s linear forwards;
}

#path1 {
  stroke-dasharray: 33px;
  stroke-dashoffset: 33px;
}

@keyframes line {
  100% {
    stroke-dashoffset: -15.5;
  }
}

@keyframes unline {
  100% {
    stroke-dashoffset: 33px;
  }
}

@keyframes shrinkleft {
  100% {
    transform: scale(0, 1);
  }
}

@keyframes unshrinkleft {
  100% {
    transform: scale(1, 1);
  }
}

#halfLineLeft {
  transform-origin: 1% 50%;
}

#halfLineRight {
  transform-origin: 100% 1%;
}

svg {
  transform: translate(50px, 50px);
}

$("svg").click(
  function() {
    $("#path1, #halfLineLeft, #halfLineRight").toggleClass("active");

    if (!$("#path1, #halfLineLeft, #halfLineRight").hasClass("active")) {

      $("#path1, #halfLineLeft, #halfLineRight").addClass("inactive");

    } else {
      $("#path1, #halfLineLeft, #halfLineRight").removeClass("inactive");
    }
  }
);



回答2:


You can add a class and then remove it. Check this

$("svg").on('click',function(){
    if($("#path1, #halfLineLeft, #halfLineRight").hasClass('forward')){
        $("#path1, #halfLineLeft, #halfLineRight").css("animation-direction", "reverse").removeClass('forward');
    }else{
     $("#path1, #halfLineLeft, #halfLineRight").css("animation-play-state", "running").addClass('forward');
    }  

  });


来源:https://stackoverflow.com/questions/39537494/how-to-reverse-css-animation-on-second-click-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!