0

I want to add + 450 to var threshold only if the page is body.home. So something like this:

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
  • 1
    what do you mean with if body.home ? Commented Apr 11, 2014 at 10:56

4 Answers 4

1

You may use ternary operation (?:)

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

If I understand correctly body.home means body element has a CSS class 'home'.

To avoid performance impact use jQuery(document.body)

Sign up to request clarification or add additional context in comments.

1 Comment

Yes! Thank you, this worked perfectly. I'll tick your answer once the time limit is up.
1

You could try using conditional/ternary operator:

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

1 Comment

I was typing the same, and got notification 4 new answers :D
0

try this

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');
});

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.