问题
Holla, I need to reset the animation when I close menu and open it again. How can I do that? Animation works only the first time when I open menu.
CSSPlugin.defaultTransformPerspective = 600;
TweenMax.staggerFrom('ul#menu li', 1.5, {rotationX:-90, transformOrigin:"50% 0%", ease:Elastic.easeOut}, 0.4);
Here is the code: http://codepen.io/hafsadanguir/pen/qOdaab.
Thanks for help!
回答1:
Is this the kind of result you were after?
JavaScript:
CSSPlugin.defaultTransformPerspective = 600;
var toggleMenu = $('.menu-toggle');
var logo = $('#logo');
var logotitle = $('#logotitle');
var listItems = $('ul#menu li');
var timeline = new TimelineMax({ paused: true, reversed: true });
timeline.fromTo([logo, logotitle], 0.6, { top: 300 }, { top: -50, ease: Power2.easeInOut });
timeline.staggerFromTo(listItems, 1.2, { autoAlpha: 0, rotationX: -90, transformOrigin: '50% 0%' }, { autoAlpha: 1, rotationX: 0, ease: Elastic.easeOut.config(1, 0.3) }, 0.1, 0.3);
toggleMenu.on('click', function() {
$(this).toggleClass('on');
timeline.reversed() ? timeline.play() : timeline.reverse();
});
A few things have changed so here are the details:
- First and foremost, I didn't see the need for the
hiddenclass so I have removed it from my solution. - Plus, the
onclass toggled on the.menu-sectionelement seemed pretty unnecessary as well. I removed it, but kept the properties defined on the.menu-sectionCSS rule. - On to the fun part, the JavaScript.
- You basically needed a TimelineMax to operate upon.
- Judging by the imports you had in the JavaScript settings panel, I am assuming that you have some idea of what
TimelineMax(andTimelineLite) are all about. By in any case,TimelineLiteis about building and managing sequences of TweenLite, TweenMax, TimelineLite, and/or TimelineMax instances andTimelineMaxis an extension ofTimelineLite, adding more power to it. - So, what happens in the code is that a
TimelineMaxinstance has been initialised, tweens have been added to it and then, upon clicking of the.menu-togglebutton element, this timeline is either played or reversed. - The line
timeline.reversed() ? timeline.play() : timeline.reverse();is basically a shortened, fancy version of anifcondition and it comes to down to personal preference more than anything else, no performance gain or anything that I am aware of. In a normalifclause, it would have been written like this:if (timeline.reversed()) { timeline.play(); } else { timeline.reverse(); }.- The condition that is being checked is .reversed() property of
timeline. You would notice that while initialisingnew TimelineMax(...), I setreversed: trueproperty on it. What it does is that after all the tweens are added totimeline, it would behave as iftimelinewas immediately reversed such that the orientation of thetimelinehad been flipped. Read more about it in theTimelineMaxdocumentation link I shared above. - The .play() and .reverse() methods are pretty self-explanatory as they make the
timelinego forwards or backwards respectively.
- That is about it.
Hope this helps.
来源:https://stackoverflow.com/questions/32441947/how-to-animate-correctly-a-gsap-menu