2016-09-16 112 views
3

我做了下列菜单图标的CSS动画,当我点击它时触发。我想让它在第二次使用jQuery时点击它的动画。如何使用jQuery第二次点击CSS动画

#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的,我有那么远,

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

我似乎无法弄清楚如何使它在反向动画时,我对SVG图标点击第二次。我想这个代码没有任何的运气:

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

这里与上述实时动画codepen链接:

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

+0

也许你可以添加一个变量来存储的点击数。第一次点击运行第一个动画,第二次点击运行其他。 – Trialsman

+0

@Trialsman或布尔值从true变为false并返回。并在每次点击它''whichAnimation =!whichAnimation' –

+0

你有没有考虑过这个类的切换? – charlietfl

回答

2

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

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"); 
    } 
    } 
); 
+0

感谢您的解决方案。我将不得不使用它,因为我不能找出另一种方式,但我真正想要的是为了不重写相反的动画并切换类,而是添加css属性“animation-direction:reverse”在第二次点击图标上。 –

0

您可以添加一个类,然后将其删除。检查此

$("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

我试过这段代码,但它只是跳转到动画的初始帧而没有查看反向动画,加上返回到初始帧后它不会响应任何更多的点击。 –