简体   繁体   English

用鼠标离开反转jquery动画

[英]Reversing jquery animation with mouse leave

This is really frustrating me because it's a simple question that I know I am gonna face-palm myself for when someone gives a 1 line answer for hah. 这真的让我感到沮丧,因为这是一个简单的问题,我知道当有人给哈哈的1行回答时,我自己会脸红。 Anyway I have the following on mouseenter snippet 无论如何,我在mouseenter片段上有以下内容

$("nav#primary ul li").mouseenter(function(){
    $(this).find('ul:first').stop(true, true).animate({
              height: ['toggle', 'swing'],
              opacity: 'toggle'
          }, 300, 'linear');
  });

My question is how do I reverse the effect of the animation after the mouse leaves primary ul li? 我的问题是,在鼠标离开原边后,如何反转动画的效果?

$("nav#primary ul li").mouseenter(function(){
    //your code for mouseenter
},function(){
    //your code for mouseleave
});

If I understood your question correctly, you are animating hight and opacity when the mouse enters an element and revert back to original condition when it leaves. 如果我正确理解了您的问题,则当鼠标进入一个元素时,您正在设置动画和不透明度,并在离开时恢复到原始状态。

$( "av#primary ul li" ).hover(
    function() {  // on mouse enter
        // save the original values
        var h = $(this).css('height');
        var o = $(this).css('opacity');
        $(this).attr('data-Oheight',h).attr('data-Oopacity',o);
        // do your animation here
    }, function() {     // on mouse leave
        // retrive the original values
        var h = $(this).attr('data-Oheight');
        var o = $(this).attr('data-Oopacity');
        // revert back
        //do your animation to revert back
    }
);

However, having said that , there is TweenMax tool from GreenSock. 但是,话虽如此,但是还有GreenSock的TweenMax工具。 It is very handy when it comes to these kind of animation requirements. 这些动画要求非常方便。 Let me know if you need to know more. 让我知道您是否需要了解更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM