0

仅当您滚动了 150 像素或更多时,我才需要帮助来执行mouseentermouseleave功能...

    $(document).ready(function(){

            $(document).scroll(function() {
                var top = $(document).scrollTop();

        if (top > 150) $(".menywrapper").mouseenter(function(){
$(".main-navigation").removeClass( "nav-closed", 200, "linear" );  
 });
        if (top > 150) $(".menywrapper").mouseleave(function(){
$(".main-navigation").addClass( "nav-closed", 200, "linear" );  }); 
 });
4

3 回答 3

2

不要在每个滚动事件上绑定 mouseenter/mouseleave,执行一次并检查 var :

$(document).ready(function(){
     var top = 0;
     $(document).scroll(function() {
         top = $(document).scrollTop();
     });

     $(".menywrapper").mouseenter(function(){
         if(top>150)
             $(".main-navigation").removeClass( "nav-closed", 200, "linear" );  
     });

     // same for mouseleave

});
于 2013-08-09T14:08:33.803 回答
1

在事件处理程序中移动 if 条件

$(".menywrapper").mouseenter(function(){
   if (top > 150) 
      $(".main-navigation").removeClass( "nav-closed", 200, "linear" );  
});
$(".menywrapper").mouseleave(function(){
   if (top > 150) 
      $(".main-navigation").addClass( "nav-closed", 200, "linear" );  }); 
 });
于 2013-08-09T14:08:33.133 回答
0

如果您高于或低于 150 像素,您可以使用布尔值来跟踪;

$(document).ready(function() {
    var below_150 = false;

    $(document).scroll(function() {
        var top = $(document).scrollTop();
        below_150 = (top > 150);
    }

    $(".menywrapper").hover(
        function() {
            if( below_150 )
            {
                $(".main-navigation").removeClass( "nav-closed", 200, "linear" );
            }
        },
        function() {
            if( below_150 )
            {
                $(".main-navigation").addClass( "nav-closed", 200, "linear" );
            }
        }
    )
});
于 2013-08-09T14:11:48.060 回答