2014-04-11 3 views
0

페이지가 body.home 인 경우에만 에 + 450을 추가하고 싶습니다. 이 같은 뭔가 :변수 선언 내에 조건문을 추가하는 방법이 있습니까?

var headHeight = jQuery('#masthead-space').height() + 23; 
var threshold = jQuery('.PopularTabsWidget').offset().top - headHeight (if body.home) + 450; 

jQuery(window).scroll(function() { 
    if (jQuery(window).scrollTop() >= threshold) 
     jQuery('.PopularTabsWidget').addClass('fixed'); 
    else 
     jQuery('.PopularTabsWidget').removeClass('fixed'); 
}); 
+1

당신이 body.home 경우 무엇을 의미합니까하려고 사용 방지하려면? –

답변

1

을 :)

var threshold = jQuery('.PopularTabsWidget').offset().top - headHeight + (jQuery('body').hasClass('home') ? 450 : 0); 

를 내가 제대로 body.home 방법을 이해한다면 body 요소의 CSS 클래스는 'home'입니다.

성능에 미치는 영향이 jQuery(document.body)

+0

예! 고마워,이게 완벽하게 작동 했어. 시간 제한이 끝나면 귀하의 답을 틱 할 것입니다. – J82

1

조건부/삼항 연산자 사용하여 시도 할 수 있습니다 : 당신은 삼항 작업을 (사용할 수 있습니다

var headHeight = jQuery('#masthead-space').height() + 23; 
var threshold = jQuery('.PopularTabsWidget').offset().top - headHeight + (body.home ? 450 : 0); 
+0

나는 똑같이 타이핑하고 있었고, 4 개의 새로운 대답을 얻었다. D –

0

이이

var headHeight = jQuery('#masthead-space').height() + 23; 
var threshold = jQuery('.PopularTabsWidget').offset().top - headHeight + body.home ? 450 : 0 ; 

jQuery(window).scroll(function() { 
    if (jQuery(window).scrollTop() >= threshold) 
     jQuery('.PopularTabsWidget').addClass('fixed'); 
    else 
     jQuery('.PopularTabsWidget').removeClass('fixed'); 
}); 
0
var threshold = jQuery('.PopularTabsWidget').offset().top - headHeight +(body.home?450:0); 
관련 문제