CSS动画向前播放然后向后播放

6
我正在尝试创建一个CSS动画,当用户点击汉堡菜单时,它会变成X(第一步),然后当用户再次点击时,它会动画回到汉堡菜单状态(第二步)。
第一步已经可行,但我不知道如何反转动画。谢谢你的帮助! http://jsfiddle.net/aX6Cf/ HTML
<a id="mobile-menu">
    <span></span>
</a>

CSS

@-webkit-keyframes rotate-plus {
  from {
    -webkit-transform:rotate(0deg);
  }
  to {
    -webkit-transform:rotate(45deg);
  }
}


@-webkit-keyframes rotate-minus {
  from {
    -webkit-transform:rotate(0deg);

  }
  to {
    -webkit-transform:rotate(-45deg);
  }
}

@-webkit-keyframes transition-1 {
  from {
    top: -6;
    transition: all .2s ease-out;

  }
  to {
    top: 0;
    transition: all .2s ease-out;
  }
}

@-webkit-keyframes transition-2 {
  from {
    bottom: -6;
    transition: all .2s ease-out;

  }
  to {
    bottom: 0;
    transition: all .2s ease-out;
  }
} 


body {
    margin: 20px;
}

#mobile-menu {
  display: block;
  position: relative;
  cursor: pointer; 
  width: 30px;
  padding: 6px 30px 9px 0; 
  box-sizing: border-box;
}

#mobile-menu span, 
#mobile-menu span:before, 
#mobile-menu span:after {
  height: 3px;
  width: 30px;
  background: #000;
  display: block;
  content: "";
  position: absolute;
  left: 50%;
  margin-left: -15px;
}

#mobile-menu span:before {
  top: -6px; 
}

#mobile-menu span:after {
  bottom: -6px;
}

#mobile-menu.active span {
  background-color: transparent;
}

#mobile-menu.active span:before {
-webkit-animation: rotate-plus .05s ease-out .1s forwards,
                   transition-1 .05s ease-out forwards;  
        animation: rotate-plus .05s ease-out .1s forwards,
                   transition-1 .05s ease-out forwards; 
}

#mobile-menu.active span:after {
-webkit-animation: rotate-minus .05s ease-out .1s forwards, 
                   transition-2 .05s ease-out forwards;
        animation: rotate-minus .05s ease-out .1s forwards, 
                   transition-2 .05s ease-out forwards;
}

jQuery

$("#mobile-menu").click(function(){
    $("#mobile-menu").toggleClass("active");
});

1
使用转换效果,而不是动画。 - Zach Saucier
有时候使用 CSS 来完成这些事情可能是很麻烦的(如果可能的话)。在这种特殊情况下,我会直接使用 GSAP。http://jsfiddle.net/aX6Cf/1/ - d0c
2个回答

2

我收回我在上面评论中说的话。看起来实际上可以使用普通的CSS来实现这个…诀窍是正确地使用过渡延迟。

HTML

<a id="mobile-menu">
   <span></span>
</a>

CSS

body {
    margin: 20px;
}

#mobile-menu {
    display: block;
    position: relative;
    cursor: pointer; 
    width: 30px;
    padding: 6px 30px 9px 0; 
    box-sizing: border-box;
}

#mobile-menu span, 
#mobile-menu span:before, 
#mobile-menu span:after {
    height: 3px;
    width: 30px;
    background: #000;
    display: block;
    content: "";
    position: absolute;
    left: 50%;
    margin-left: -15px;
}

#mobile-menu span {
    transition: background-color .3s ease .3s;
    -webkit-transition: background-color .3s ease .3s;
}

#mobile-menu span:before {
    top: -6px;
    transition: top .2s ease .2s, transform .2s ease;
    -webkit-transition: top .2s ease .2s, -webkit-transform .2s ease;
}

#mobile-menu span:after {
    bottom: -6px;
    transition: bottom .2s ease .2s, transform .2s ease;
    -webkit-transition: bottom .2s ease .2s, -webkit-transform .2s ease;
}

#mobile-menu.active span {
    background-color: transparent;
    transition: background-color .3s ease;
    -webkit-transition: background-color .3s ease;
}

#mobile-menu.active span:before {
    top: 0;
    -webkit-transform: rotate(45deg);
    transform: rotate(45deg);
    transition: top .2s ease .1s, transform .2s ease .3s;
    -webkit-transition: top .2s ease .1s, -webkit-transform .2s ease .3s;
}

#mobile-menu.active span:after {
    bottom: 0;
    -webkit-transform: rotate(-45deg);
    transform: rotate(-45deg);
    transition: bottom .2s ease .1s, transform .2s ease .3s;
    -webkit-transition: bottom .2s ease .1s, -webkit-transform .2s ease .3s;
}

jQuery

$(function() {
    $("#mobile-menu").click(function(){
        $("#mobile-menu").toggleClass("active");
    });
})

Online Demo


太棒了!我不知道在transition属性中可以使用transform并利用delay属性。非常感谢!我可以问一下为什么过渡效果在加载时没有触发吗? - user1706680
一个过渡只指定属性变化发生的方式,而不是何时发生(即过渡本身不能触发属性变化)。在设置初始属性值时触发转换并没有太多意义,因为(1)你没有一个“从”部分来插值,(2)唯一明智的默认选择是IMO“从”部分==“到”部分==初始值,这有点无聊。请注意,如果需要,可以在加载后立即触发转换:http://jsfiddle.net/aX6Cf/5/。 - d0c

0

看看这个JSfiddle,我稍微调整了一下(以适应你的代码),来自于这个令人惊叹的Codepen原始版本

HTML

<a id="mobile-menu" href="#">
  <span></span>
</a>

CSS

a#mobile-menu {
  display: inline-block;
  width:30px;
  height:18px;
  cursor: pointer;
  text-decoration: none;
}
a#mobile-menu span {
  position: relative;
  display: inline-block;
  width: 30px;
  height: 3px;
  color:#252525;
  font:bold 14px/.4 Helvetica;
  text-transform: uppercase;
  text-indent:-55px;
  background: #252525;
  transition:all .2s ease-out;
}
a#mobile-menu span::before, a#mobile-menu span::after {
  content:'';
  width: 30px;
  height: 3px;
  background: #252525;
  position: absolute;
  left:0;
  transition:all .2s ease-out;
}
a#mobile-menu span::before {
  top: -7px;
}
a#mobile-menu span::after {
  bottom: -7px;
}
a#mobile-menu.active span {
    background: #fff;
}
a#mobile-menu.active span::before {
  top:0;
  -webkit-transform: rotateZ(45deg);
     -moz-transform: rotateZ(45deg);
      -ms-transform: rotateZ(45deg);
       -o-transform: rotateZ(45deg);
          transform: rotateZ(45deg);
}
a#mobile-menu.active span::after {
  bottom:0;
  -webkit-transform: rotateZ(-45deg);
     -moz-transform: rotateZ(-45deg);
      -ms-transform: rotateZ(-45deg);
       -o-transform: rotateZ(-45deg);
          transform: rotateZ(-45deg);
}

a#mobile-menu {
  position: absolute;
  left:50%;
  margin-left:-9px;
  top:50%;
  margin-top:-9px;
}

jQuery

$('a').click(function() {
   $(this).toggleClass('active');
});

正如我所说,所有的功劳都归功于这个 CodePen


我很感激你的努力,但重要的是在上下线移动到中间后才开始旋转。 - user1706680

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接