jQuery: code stops working after adding delay() and removeClass()

后端 未结 3 874
太阳男子
太阳男子 2020-12-11 11:27

Here\'s my code...

 $(\'nav ul li\').hover(function() {

    $(this).addClass(\'hoverIn\');


  },
  function() {

    $(this).addClass(\'hoverOut\');


  })         


        
3条回答
  •  暖寄归人
    2020-12-11 11:56

    jQuery .delay() applies to the animation queue, it does not serve as a timer like setTimeout does...

    To your goal, I can suggest you to use the hoverIntent plug-in:

    See this working Fiddle Example! adjust the timeout to fit your needs

    jQuery

    $("nav ul li").hoverIntent({    
        sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
        interval: 10,   // number = milliseconds for onMouseOver polling interval    
        timeout: 800,   // number = milliseconds delay before onMouseOut    
        over:function(){
            $(this).removeClass("hoverOut").toggleClass("hoverIn");
        },
        out: function(){
            $(this).removeClass("hoverIn").toggleClass("hoverOut");
        }
    });
    

提交回复
热议问题