2014-10-22 3 views
1

사용자가 페이지의 특정 지점을지나 스크롤 할 때 고정 클래스를 div (.filter-target)에 적용하는 기본 스크립트가 있습니다. 사용자가 브라우저의 크기를 조정하고 브라우저의 너비가 <100px (즉, minWidth 변수) 인 경우 스크롤 이벤트를 사용 중지 할 수있는 방법을 알고 싶습니다.브라우저에서 스크롤 이벤트 사용 안 함

var app = {}; 

app.filter = (function() { 
    var module = {}; 

    var $filter = $('.filter-target'); 
// Creates a spacer, this is to push the content of the page down the same size of the floating menu 
    var $filterSpacer = $('<div />', { 
     "class": "filter-drop-spacer", 
     "height": $filter.outerHeight() 
    }); 

    var minWidth = Modernizr.mq('only screen and (min-width: 1100px)'); 
    var windowWidth = $(window).width(); 

    module.init = function() { 

     var fixedFilter = function() { 
      $filter 
       .before($filterSpacer) 
      .addClass("is-fixed") 
      .css('width', $('.page-content').width()); 
     }; 

     var relativeFilter = function() { 
      $filter 
       .removeClass("is-fixed") 
       .css('width', ''); 

      $filterSpacer.remove(); 
     }; 

     var filterController = function() { 
      if (minWidth == true) { 
       if (!$filter.hasClass('is-fixed') && $(window).scrollTop() > $filter.offset().top) { 
        // Checks the filter doesn't have the class fix and that vertical number of pixels hidden is greater than the navigations position on the page. This will get executed the first time the page scrolls past the point. I.E When you scroll hits the menu. 
        fixedFilter(); 
       } else if ($filter.hasClass('is-fixed') && $(window).scrollTop() < $filterSpacer.offset().top) { 
        // If the navigation element has the fix class and that vertical number of pixels hidden is less than the top of the newly added spacer. I.E When you scroll UP past the spacer. 
        relativeFilter(); 
       } 
      } else { 
       relativeFilter(); 
      } 
     }; 

     $(window).scroll(function() { 
      filterController(); 
     }); 

    }; 

    return module; 
})(); 

$(document).ready(function() { 
    app.filter.init(); 
    console.log('filter'); 
}); 

답변

0

당신은

$(window).resize(function() { 
    if($(window).width() < 1100){ 
     minWidth = false; 
    }  
}); 
+0

감사는 false하게 크기 조정에 minWidth를

var minWidth = true; 

을 설정 시작에서 resize 이벤트

일어날 수 -하지만 어떻게 실제로 것 사용 중지하려고하십니까? 아마 relativeFilter()를 발사하겠습니까? – jshjohnson

+0

크기가 작을 때 minWidth를 false로 변경하고 너비가 1100 미만이면 그렇지 않습니다. –

+0

$ (window) .width()가 아닌 정확한 브라우저 너비를 얻기 위해 modernizr 변수를 사용했지만 작동합니다 똑같다! 감사 – jshjohnson