I'm trying to create a fixed menu that changes color depending on the background of different sections.
Currently, I am using a data-color attribute but I am struggling with removing and adding the class to #open-button. Adding the class works fine, but removing the correct one is proving to be difficult for me.
If you want to take a look at my code, check out my fiddle.
Here's a snippet of my code:
<div id="top-wrapper">
<div class="menu-button" id="open-button"><span></span></div>
</div>
<section class="section black-bg" data-color="icon-white">
Section One has a black background
</section>
<section class="section white-bg" data-color="icon-black">
Section Two has a white background
</section>
<section class="section black-bg" data-color="icon-white">
Section Three has a black background
</section>
<section class="section white-bg" data-color="icon-black">
Section Four has a white background
</section>
jQuery snippet:
$(function(){
$(window).on('scroll', function() {
var scrollTop = $(this).scrollTop();
$('.section').each(function() {
var topDistance = $(this).offset().top;
if ( (topDistance) < scrollTop ) {
$('#open-button').addClass($(this).attr('data-color'));
}
});
});
})