setTimeout in a JQuery animation

百般思念 提交于 2019-12-11 01:36:21

问题


I'm having a problem with the setTimeout(). I want, in the mouseout state, that the submenu slides Up after a interval (500 miliseconds). But the setTimeout() isn't working.

Like in this link: http://jsfiddle.net/felipepalazzo/Xyhvn/2/

The code:

(function($){
    $.fn.showMenu = function(options){

        var settings = $.extend({
            height  : '40px',
            speed   : '500',
            heightx : '20px'              
        }, options || {});

        return this.each(function(){
           var elem = $(this);
           var menu_timer;
            elem.hover(function(){
                $(this).stop().animate({'height' : settings.height}, settings.speed);
                    }, function(){
                      //setTimeout(function(){  
                      $(this).stop().animate({'height' : settings.heightx}, settings.speed);
                            //},500);
                    }); 
        });      
    };
})(jQuery);

回答1:


This is out of scope.

var that = this;
setTimeout(function(){
    $(that).stop().animate({'height' : settings.heightx}, settings.speed);
},500);



回答2:


Use delay()

So for example

$(this).delay(500).stop().animate({'height' : settings.heightx}, settings.speed);



回答3:


I think that the problem relies on the element $(this), when you're inside of the function of the setTimeout the element this it's not the same. Why you don't try to save the element in a var and then executes the function

var foo = this;
setTimeout(function(){
    $(foo).stop().animate({'height' : settings.heightx}, settings.speed);
},500);


来源:https://stackoverflow.com/questions/10840106/settimeout-in-a-jquery-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!