首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用jQuery在第二次点击时反转CSS动画

如何使用jQuery在第二次点击时反转CSS动画
EN

Stack Overflow用户
提问于 2016-09-17 01:54:59
回答 2查看 1.4K关注 0票数 5

我已经制作了下面的菜单图标CSS动画,当我点击它时会触发它。当我使用jQuery第二次点击它时,我想让它反转成动画。

代码语言:javascript
复制
#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);
}

这是我到目前为止所拥有的jQuery。

代码语言:javascript
复制
$("svg").click(
  function(){
     $("#path1, #halfLineLeft, #halfLineRight").css("animation-play-state", "running");
  }
 );

当我第二次点击SVG图标时,我似乎想不出如何让它反转过来。我尝试了这段代码,但没有任何运气:

代码语言:javascript
复制
$("svg").click(
  function(){
     $("#path1, #halfLineLeft, #halfLineRight").css("animation-state", "running"),
     $("#path1, #halfLineLeft, #halfLineRight").css("animation-direction", "reverse");
  }
 );

以下是实时动画的codepen链接:

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-17 14:03:37

我不知道这是不是最有效的方法,但添加反转关键帧和切换类似乎是有效的。

codepen example

代码语言:javascript
复制
#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
EN

Stack Overflow用户

发布于 2016-09-17 02:28:22

您可以添加一个类,然后将其删除。看看这个

代码语言:javascript
复制
$("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');
    }  

  });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39537494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档