I'm relatively new to js and finally managed to create a script (took me bloody 3 hours! :-D) that basically inverts the color of my dummy navigation (text/links plus logo image) when user scrolls past the initial darker dummy slider section, and onto the rest of the page which has white background. So far so good. Here is my code:
function logoSwitch () {
$('.altLogo').each(function() {
$(this).css('top',
$('.startLogo').offset().top - $(this).closest('.row').offset().top
);
});
};
$(document).scroll(function() {logoSwitch();});
logoSwitch();
* {
padding: 0;
margin: 0;
}
.row {
min-height: 100vh;
overflow: hidden;
position: relative;
width: 100%;
}
.logo {
top: 30px;
width: 100%;
transform-origin: left top 0;
}
.startLogo {
position: fixed;
}
.altLogo {
position: absolute;
}
.white {
color: white;
}
.black {
color: #1d1d1d;
}
.grey {
background: #ccc;
}
.white {
background: white;
}
.hero-black {
background: #000;
}
.light {
color: white;
}
.menu-link, .menu-logo {
width: 20%;
float: left;
text-align: center;
}
.logo-image {
width: 80px;
margin-top: -7px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row hero-black">
<div class="logo light startLogo">
<div class="menu-logo"><img class="logo-image" src="https://newhomespain.com/wp-content/uploads/2016/11/footer-logo-white-small-compressed.png"></div>
<div class="menu-link">Home</div>
<div class="menu-link">Webshop</div>
<div class="menu-link">About</div>
<div class="menu-link">Contact</div>
</div>
</div>
<div class="row white">
<div class="logo black altLogo">
<div class="menu-logo"><img class="logo-image" src="https://newhomespain.com/wp-content/uploads/2016/11/new-home-spain-hi-res-logo.png"></div>
<div class="menu-link">Home</div>
<div class="menu-link">Webshop</div>
<div class="menu-link">About</div>
<div class="menu-link">Contact</div>
</div>
</div>
I now have 2 problems:
I had to start somewhere, so i did - but i now see that the script is very limited to affect only two classes: "altLogo" (displaying the coloured logo and black text on white background) and "startLogo" (displaying the white logo and text on dark background). This makes it very rigid and i can't seem to think of a way to implement it globally in the header section of my webpage in a smart way. How could i amend the scipt to make it more flexible - in stead of having to insert the "altLogo" and "startLogo" basically everywhere?
While trying to gain knowledge on how to code the script i ended up with until now, i came across this website, which does the trick in an very clever way it seems:
The logo and text color in the header changes based on the color of the underlying div/background, and can change multiple times back and forth while scrolling down, if let's say we have a dark section with a dark backgound, followed by a new light section, then a dark one and then a light one again.
Is there an "easy" way to do this? Basically to tell the script to change the navigation text into black and use colored image/logo if backgound is white, and revert back to all white text and image/logo if background is dark?
Link to my fiddle: