jQuery:从顶部执行超过150px

时间:2013-08-09 14:04:05

标签: javascript jquery wordpress jquery-ui function

只有滚动150px 或更多时才需要帮助才能执行 mouseenter mouseleave 功能...

    $(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" );  }); 
 });

3 个答案:

答案 0 :(得分: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

});

答案 1 :(得分:1)

在ifhandhandler中移动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" );  }); 
 });

答案 2 :(得分: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" );
            }
        }
    )
});