0

I'm trying to add a drop shadow to my site's fixed navigation bar when it reaches a certain section. Can anyone explain why this isn't working?

In my .CSS,

.whiteDropShadow {
-moz-box-shadow: 0 0 10px #FFFFFF; 
-webkit-box-shadow: 0 0 10px #FFFFFF;
-o-box-shadow: 0 0 10px #FFFFFF;
box-shadow: 0 0 10px #FFFFFF;
}

In my .JS,

$(function() {

// Initial top offset from ABOUT section
var topOffset = $('#about').offset().top;

// FUNCTION: adds class to #navLinks when vertical distance from the top is larger than the initial top offset.
var editNavBar = function(){
    var verticalDistance = $(window).scrollTop(); // Current vertical distance from the top

    if (verticalDistance > topOffset) { 
        $('#navLinks').addClass('.whiteDropShadow');
    } else {
        $('#navLinks').removeClass('.whiteDropShadow');
    }
};

// Run upon scrolling
$(window).scroll(function() {
     editNavBar();
});

});

myom
  • 135
  • 2
  • 16

2 Answers2

1

I have it working here. I just logged out your values to make sure you could scroll far enough for the conditional to be hit. Also added #navLinks to your whiteDropShadow selector.

I'd double check your able to scroll long enough on your page to make your conditional truthy.

http://jsfiddle.net/y2zu5cwn/

<div id="navLinks">
    <a href="#">item1</a>
    <a href="#">item 2</a>
    <a href="#">item 3</a>
</div>

<div class="container">
    <div id="spacer">
        <p>Just for space</p>
    </div>

    <div id="about">
        <p>about</p>
    </div>    
</div>

// Initial top offset from ABOUT section
var topOffset = $('#about').offset().top;

// FUNCTION: adds class to #navLinks when vertical distance from the top is larger than the initial top offset.
var editNavBar = function(){
    var verticalDistance = $(window).scrollTop(); // Current vertical distance from the top
    console.log( verticalDistance, topOffset );


    if (verticalDistance > topOffset) { 
        $('#navLinks').addClass('whiteDropShadow');
    } else {
        $('#navLinks').removeClass('whiteDropShadow');
    }
};

// Run upon scrolling
$(window).scroll(function() {
     editNavBar();
});

#navLinks {
    position: fixed;
    top: 0;
    width: 100%;
    height: 20px;
    background: #aeaeae;
}



#navLinks a { color: #000;}

#navLinks.whiteDropShadow {
-moz-box-shadow: 0 0 10px #FFFFFF; 
-webkit-box-shadow: 0 0 10px #FFFFFF;
-o-box-shadow: 0 0 10px #FFFFFF;
box-shadow: 0 0 10px #FFFFFF;
}

.container { height: 1400px; }

#spacer {
    display: block;
    height: 500px;
    background: green;
}

#about {
    display: block;
    height: 500px;
    background: red;
}
Christopher Marshall
  • 10,678
  • 10
  • 55
  • 94
  • 1
    You're a saint. Thanks! The trick was adding #navLinks to my whiteDropShadow selector. Why does that work though? Does the "#navLinks.whiteDropShadow" selector not get created via the addClass function? Guess that makes sense. – myom Oct 14 '14 at 22:04
  • It works even without the ID selector (remove #navLinks from #navLink.whiteDropShadow in the fiddle). I'm not entirely sure what the problem was here to be honest as I didn't really change a lot. – Christopher Marshall Oct 14 '14 at 22:07
1

jquery-waypoints

Here is a site I used this for... http://cardbinder.herokuapp.com/card_sets/magic-2014/cards

You basically choose an element to be the trigger, then when that element hit's the top (or bottom) of the screen (or an offset from it), the element gets a class added to it. You're free to do what you will with that class at that point. I use the same library for lazy loading images on that same page.

Dudo
  • 4,002
  • 8
  • 32
  • 57
  • Waypoints is an awesome library. I would've recommended it but he was already so far along (and possibly the only thing he would have needed it for) it didn't seem right suggesting a plugin. – Christopher Marshall Oct 14 '14 at 22:17