私は、CSSアニメーションを使用してXになるためのハンバーガーアイコンをアニメーション化しました。 animation-direction
プロパティを使用してアニメーションを元に戻したい。しかし、demo hereを見ると、アニメーションをスムーズに逆転させることができないだけでなく、2回目のクリックでフォワードアニメーションを再生できなくなることがわかります(jsトグル)。CSS Animations - アニメーションを自由に前後に切り替えながら、クリックイベントでアニメーションを反転させるにはどうすればよいですか?
これをどのようにしてスムーズにアニメートできますか?
.openUpArrowAnim .hamburger:nth-child(1) {
position: relative;
display: block;
width: 2em;
height: .3em;
background-color:white;
margin-right: auto;
margin-left: auto;
transform-origin: 25px -3px;
-webkit-transform-origin: 23px 7px;
animation-name: crossBar1;
animation-duration: .5s;
animation-timing-function: ease;
animation-fill-mode: forwards;
animation-direction: normal;
}
.openUpArrowAnim .hamburger:nth-child(2) {
position: relative;
display: block;
width: 2em;
height: .3em;
background-color:white;
margin-top: 5px;
margin-bottom: 5px;
margin-right: auto;
margin-left: auto;
animation: crossBar2 .5s ease .2s forwards;
}
.openUpArrowAnim .hamburger:nth-child(3) {
position: relative;
display: block;
width: 2em;
height: .3em;
background-color:white;
margin-right: auto;
margin-left: auto;
transform-origin: 25px -3px;
-webkit-transform-origin: 25px -3px;
-ms-transform-origin: 25px -3px;
animation: crossBar3 .5s ease forwards;
}
.openUpArrowRvrs .hamburger:nth-child(1){
animation-name: crossBar1;
animation-duration: 1s;
animation-timing-function: ease;
animation-direction: reverse;
}
.openUpArrowRvrs .hamburger:nth-child(2){
animation: crossBar2 1s ease .2s reverse both;
}
.openUpArrowRvrs .hamburger:nth-child(3){
animation: crossBar3 1s ease reverse forwards;
}
@keyframes crossBar1{
0% { transform: translateY(0px); }
50% { transform: translateY(9px); }
100% { transform: rotate(-45deg); }
}
@keyframes crossBar2{
to {width: 0px;}
}
@keyframes crossBar3{
0% { transform: translateY(0px); }
50% { transform: translateY(-9px); }
100% { transform: rotate(45deg); }
}
クラスを切り替えてアニメーションを再起動/再適用できるとは思いません。ブラウザは「meh、私はすでにそれをした、私はあなたを無視するつもりだ」とか、そういうものを考えている。アニメーションを再起動するためのJSベースのアプローチについては、こちらの記事を参照してください。https://css-tricks.com/restart-css-animation/別の方法として、名前の異なる複数のアニメーションを使用する方法があります。この質問に関する私のコメントを見てください:https://stackoverflow.com/questions/47410458/why-animate-css-animation-is-played-only-once#comment81777338_47410458 –