Very first question on Stack Overflow, regarding Vanilla Javascript.
I have four links in an unordered list. I have set up a mouseover event on each <li> by using forEach.
When I hover over the element I add a css pseudo class of ::after to the element, which displays a small css rectangle underneath the hovered element.
When I move my mouse directly downward off the element and off the <ul>, the the Css pseudo class of ::after gets removed and the rectangle disappears, which is exactly what I want. Except if I move my mouse directly from one link element to another element to the left or right, the previous link element's css pseudo class of ::after still remains, so the square remains.
Below is some sample code. Any insights would be highly appreciated.
navigationMouseOver();
function navScroll() {
window.addEventListener("scroll", function() {
if (document.documentElement.scrollTop > 100) {
document.querySelector(".contact-info2").classList.add("visible");
} else {
document.querySelector(".contact-info2").classList.remove("visible");
}
})
}
function navigationMouseOver() {
let navLi = document.querySelectorAll(".link");
navLi.forEach(function(element) {
element.addEventListener("mouseover", function(e) {
let link = e.currentTarget;
let linkText = e.currentTarget.textContent;
console.log(linkText);
const subMenu = document.querySelector(".sub-menu");
const arrow = document.querySelector(".arrow");
let elementPos = link.getBoundingClientRect();
let elementTop = elementPos.top - 50;
let elementWidth = elementPos.width;
let elementBottom = elementPos.bottom - 10;
let elementCenter = (elementPos.left + elementPos.right) / 2;
let trueCenter = elementCenter - elementWidth;
let arrowW = arrow.clientWidth;
let arrowCenter = elementCenter - arrowW;
element.classList.add("after");
subMenu.classList.add("show");
arrow.style.bottom = `${elementBottom} px`
arrow.style.left = `${arrowCenter}px`
subMenu.style.top = `${elementBottom}px`;
subMenu.style.left = `${trueCenter}px`;
subMenu.innerHTML = "Hallo";
navMouseOver(element)
})
})
}
function navMouseOver(element) {
let subMenu = document.querySelector(".sub-menu")
let banner = document.querySelector(".banner");
banner.addEventListener("mouseover", function(e) {
if (!e.target.classList.contains("link")) {
subMenu.classList.remove("show");
element.classList.remove("after");
}
})
}
.banner-nav li.after::after {
content: "";
display: block;
position: absolute;
background: white;
height: 30px;
width: 30px;
top: - 20px;
left: 40%;
}
<section class="banner">
<div class="banner-logo">
<h1 class="logo-text"> Logo</h1>
</div>
<div class="banner-nav ">
<ul class="nav-ul ">
<li class="link"> Gallery </li>
</a>
<a href="#">
<li class="link"> Products</li>
</a>
<a href="#">
<li class="link"> About </li>
</a>
<a href="#">
<li class="link"> Contact </li>
</a>
</ul>
</div>
<div class="arrow"></div>
<div class="sub-menu"></div>
</section>
You don't need JavaScript for this. You can combine :hover pseudo class with ::after like this:
.banner-nav li:hover::after {}
.banner-nav li:hover::after {
content: "";
display: block;
position: absolute;
background: white;
height: 30px;
width: 30px;
top: - 20px;
left: 40%;
border: 1px solid;
}
<section class="banner">
<div class="banner-logo">
<h1 class="logo-text"> Logo</h1>
</div>
<div class="banner-nav ">
<ul class="nav-ul ">
<li class="link"> Gallery </li>
</a>
<a href="#">
<li class="link"> Products</li>
</a>
<a href="#">
<li class="link"> About </li>
</a>
<a href="#">
<li class="link"> Contact </li>
</a>
</ul>
</div>
<div class="arrow"></div>
<div class="sub-menu"></div>
</section>
Related
I have been staring at this code for far too long, unfortunately I do not see the problem.
I am trying to get the active menu entry highlighted when the relevant div gets scrolled into view. But nothing is happening and no errors are being thrown in the console.
My menu html:
<section class="LeftAnchorNav" style="display: block;">
<nav id="LeftAnchorNav">
<div class="container" style="padding-left: 50px;">
<div class="col-md-4 LeftAnchorNavWrapper">
<ul class="LeftAnchorNavMenu">
<li class="leftanchorlink">
<a class="leftlink" href="#20a51af3-f8b0-4ef9-ba73-cf3cd0a321b9">About us</a>
</li>
<li class="leftanchorlink">
<a class="leftlink" href="#d736bc13-a2a7-48d4-8ecc-75b9a17f801b">Demo Center</a>
</li>
<li class="leftanchorlink">
<a class="leftlink" href="#545a6339-87e4-41ed-ad51-70c3788cedee">Testimonial</a>
</li>
<li class="leftanchorlink">
<a class="leftlink" href="#9355324a-6219-4300-ae97-aa77bf67dab4">Newsletter</a>
</li>
<li class="leftanchorlink">
<a class="leftlink" href="#0c70b0db-3e70-4faa-ab98-154b4eae498e">Blog</a>
</li>
<li class="leftanchorlink">
<a class="leftlink" href="#4903bc53-b862-42f0-a600-e21061204e42">Contact</a>
</li>
<li class="leftanchorlink">
<a class="leftlink" href="#002f6fd7-758b-4b27-8c75-0ce087ee826a">Solution Finder</a>
</li>
</ul>
</div>
</div>
</nav>
</section>
An example div:
<div class="block anchorblock col-lg-12 col-md-12 col-sm-12 col-xs-12 span12 "><div id="20a51af3-f8b0-4ef9-ba73-cf3cd0a321b9"></div>
</div>
My jquery/js:
if ($('.LeftAnchorNav').length > 0) {
// prepare the variables
var lastID;
var anchorMenu = $(".LeftAnchorNavMenu");
var anchorMenuHeight = anchorMenu.outerHeight() + 100;
var anchorMenuItems = anchorMenu.find(".leftlink");
var anchorMenuItemsTarget = anchorMenuItems.map(function () {
var item = $($(this).attr("href"));
if (item.length) { return item; }
});
// bind everything to the scrolling
$(window).scroll(function () {
// get anchornav container scroll position and add buffer
var fromTop = $(this).scrollTop() + anchorMenuHeight + 300;
// get ID of the current scroll item
var currentItem = anchorMenuItemsTarget.map(function () {
if ($(this).offset().top < fromTop)
return this;
});
// get the ID of the current element
currentItem = currentItem[currentItem.length - 1];
var id = currentItem && currentItem.length ? currentItem[0].id : "";
if (lastID !== id) {
lastID = id;
// Set/remove active class
anchorMenuItems.removeClass("highlightleftnavactive")
anchorMenuItems.filter("[href='#" + id + "']").addClass("highlightleftnavactive");
}
});
}
It's quite fiddly to do the arithmetic for scrolling so this snippet uses IntersectionObserver instead. This has the added benefit of less processing overhead as it just gets informed when the elements come in or go out of view, not every time the user scrolls a bit.
It sets up the observer to observe when any of the relevant elements come into or go out of the viewport. When alerted to that it adds or removes the highlighting class to the related navbar link.
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
<style>
.LeftAnchorNav {
position: fixed;
z-index:1;
}
.tall {
width: 100vw;
height: 100vh;
background-image: linear-gradient(cyan, magenta, yellow, black);
}
.highlightleftnavactive {
background-color: yellow;
}
</style>
</head>
<section class="LeftAnchorNav" style="display: block;">
<nav id="LeftAnchorNav">
<div class="container" style="padding-left: 50px;">
<div class="col-md-4 LeftAnchorNavWrapper">
<ul class="LeftAnchorNavMenu">
<li class="leftanchorlink">
<a class="leftlink" href="#20a51af3-f8b0-4ef9-ba73-cf3cd0a321b9">About us</a>
</li>
<li class="leftanchorlink">
<a class="leftlink" href="#d736bc13-a2a7-48d4-8ecc-75b9a17f801b">Demo Center</a>
</li>
<li class="leftanchorlink">
<a class="leftlink" href="#545a6339-87e4-41ed-ad51-70c3788cedee">Testimonial</a>
</li>
<li class="leftanchorlink">
<a class="leftlink" href="#9355324a-6219-4300-ae97-aa77bf67dab4">Newsletter</a>
</li>
<li class="leftanchorlink">
<a class="leftlink" href="#0c70b0db-3e70-4faa-ab98-154b4eae498e">Blog</a>
</li>
<li class="leftanchorlink">
<a class="leftlink" href="#4903bc53-b862-42f0-a600-e21061204e42">Contact</a>
</li>
<li class="leftanchorlink">
<a class="leftlink" href="#002f6fd7-758b-4b27-8c75-0ce087ee826a">Solution Finder</a>
</li>
</ul>
</div>
</div>
</nav>
</section>
<div class="tall"></div>
<div class="block anchorblock col-lg-12 col-md-12 col-sm-12 col-xs-12 span12 "><div id="20a51af3-f8b0-4ef9-ba73-cf3cd0a321b9">
An example block coming into and going out of view it belongs to the About us link in the navbar</div>
</div>
<div class="tall"></div>
<script>
let callback = (entries) => {
entries.forEach(entry => {
let id = entry.target.firstChild.id;
let leftLink = document.querySelector("a.leftlink[href='#"+ id + "']");
if (entry.isIntersecting) { leftLink.classList.add('highlightleftnavactive');}
else { leftLink.classList.remove('highlightleftnavactive');}
});
};
const observer = new IntersectionObserver(callback);
const anchorBlocks = document.querySelectorAll('.anchorblock');
anchorBlocks.forEach( (anchorBlock) => {
observer.observe(anchorBlock);
});
</script>
Problem
I have an issue with my website, I want to change the color of the selected link to blue, and if other is selected put it back to gray, but don`t know how to target the clicked link in javascript. Here is my code.
var center = document.getElementsByClassName("center");
for (let i = 0; i < center.length; i++) {
center[i].addEventListener("click", DivSelector());
}
function DivSelector() {
for (let i = 0; i < center.length; i++) {
center[i].classList.Remove('active');
};
}
.nav-item {
text-decoration: none;
color: #505b67;
margin-left: 10px;
}
.active {
color: #4460f1;
}
<ul class="nav-tab-ul">
<li id="Profile">
<a class="center nav-item" href="">Profile</a>
</li>
<li id="Change-Password">
<a class="center nav-item" href="">Change password</a>
</li>
<li id="Notifications">
<a class="center nav-item" href="">Notifications</a>
</li>
<li id="My-Cards">
<a class="center nav-item" href="">My Cards</a>
</li>
</ul>
Don`t know how to select the clicked link here for adding the 'active' class.
Try this. its working
var center = document.getElementsByClassName("center");
for (let i = 0; i < center.length; i++) {
center[i].addEventListener("click", function(e){
for (let i = 0; i < center.length; i++) {
center[i].classList.remove('active');
e.target.classList.add('active');
}
})
}
All you need to do is add the class active to the actual link of the page you are on. So when you on the profile page you add active to profile link and none of the others
PROFILE PAGE
<ul class="nav-tab-ul">
<li id="Profile">
<a class="center nav-item active" href="">Profile</a>
</li>
<li id="Change-Password">
<a class="center nav-item" href="">Change password</a>
</li>
<li id="Notifications">
<a class="center nav-item" href="">Notifications</a>
</li>
<li id="My-Cards">
<a class="center nav-item" href="">My Cards</a>
</li>
</ul>
CHANGE PASSWORD PAGE
When you on the change password page you add the active class to that link and none of the others.
<ul class="nav-tab-ul">
<li id="Profile">
<a class="center nav-item" href="">Profile</a>
</li>
<li id="Change-Password">
<a class="center nav-item active" href="">Change password</a>
</li>
<li id="Notifications">
<a class="center nav-item" href="">Notifications</a>
</li>
<li id="My-Cards">
<a class="center nav-item" href="">My Cards</a>
</li>
</ul>
If these are separate pages there is no need for JS?
If you are using one header file and including the file into all your pages then it will be a different solution. Let us know why you want to use JS
You can get the element clicked in event.currentTarget.
const nodes = document.getElementsByClassName("center");
for (let i = 0; i < nodes.length; i++) {
nodes[i].addEventListener("click", divSelector);
}
function divSelector(event) {
removeAllActives();
event.currentTarget.className += ' active';
}
function removeAllActives() {
const actives = document.getElementsByClassName("active");
for (let i = 0; i < actives.length; i++) {
actives[i].classList.remove('active');
}
}
.active {
color: blue;
}
<a class="center"> Link 1 </a> <br/>
<a class="center"> Link 2 </a>
A few things:
When adding your event listener, you called DivSelector, meaning you added the return value of DivSelector() as a listener, which is void. Instead just pass the name of the function you want to add as a listener.
You need to add the 'active' class to the clicked element after removing it from all the others. You can get the clicked element by using the event argument passed to your event listener. The element will be event.target.
DivSelector should be defined before it's used, so it should be moved above the rest of the code.
Here's an example:
function DivSelector(event) {
for (let i = 0; i < center.length; i++) {
center[i].classList.remove('active');
}
event.target.classList.add('active');
}
let center = document.querySelectorAll("center");
for (let i = 0; i < center.length; i++) {
center[i].addEventListener("click", DivSelector);
}
.nav-item {
text-decoration: none;
color: #505b67;
margin-left: 10px;
}
.active {
color: #4460f1;
}
<ul class="nav-tab-ul">
<li id="Profile">
<a class="center nav-item" href="#">Profile</a>
</li>
<li id="Change-Password">
<a class="center nav-item" href="#">Change password</a>
</li>
<li id="Notifications">
<a class="center nav-item" href="#">Notifications</a>
</li>
<li id="My-Cards">
<a class="center nav-item" href="#">My Cards</a>
</li>
</ul>
sorry for my English...
I to try an image zoom as here to say, it working (the image zoom) without <header> Tag.
Width the <header> tag
When getting with the Maus on the image, the image disappears.
Here I can show how to build my Html code.
$('.wrapper').on('mouseenter', function() {
$('.img').css({
transform: 'scale(2.5)'
});
});
$('.wrapper').on('mouseleave', function() {
$('.img').css({
transform: 'scale(1.0)',
backgroundPosition: 'center center'
});
});
$('.wrapper').on('mousemove', function(e) {
$('.img').css({
backgroundPosition: (e.pageX * -1) + 'px ' + (e.pageY * -1) + 'px'
});
});
.row {
margin: 10px;
}
.wrapper {
width: 300px;
height: 300px;
border: 1px solid navy;
overflow: hidden;
}
.img {
width: 100%;
padding-bottom: 100%;
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
<div class="log">
<img id="logo" src="/kalamulur/Photo/my-logo5.png" alt="">
</div>
<div class="linken">
<ul class="linken_ul">
<li class="dropdown">
<i>Home</i>
</li>
<li class="dropdown">
<i>Taschen & Rucksäcke</i>
<div class="dropdown-content">
<i>Taschen, Tablettaschen & Computertaschen</i>
<i>Rucksäcke</i>
<i>Beutel</i>
<i>Gürteltasche, Mäppchen & Schnupftabakdose</i>
</div>
</li>
<li class="dropdown">
<i>Haus</i>
<div class="dropdown-content">
<i>Dekoration</i>
<i>Teppich</i>
<i>Yogamatten & Necessaire</i>
</div>
</li>
<li class="dropdown">
<i>Kleidung</i>
<div class="dropdown-content">
<i>T-Shirts für Frauen & Männer</i>
<i>Hösen für Frauen & Männer</i>
<i>Schals, Mützen & Hute</i>
<i>Sandalen</i>
</div>
</li>
<li class="dropdown">
<i>Schmuck</i>
<div class="dropdown-content">
<i>Ringe, Ohrringe, Ketten ...</i>
</div>
</li>
<li class="dropdown">
<i>Über uns</i>
<div class="dropdown-content">
<i>Über uns</i>
<i>Kundenservice</i>
</div>
</li>
<li class="dropdown">
🌍 English
<div class="dropdown-content">
Spanisch
</div>
</li>
</ul>
</div>
</header>
<main>
<div class="wrapper">
<div class="img" style="background-image: url('http://www.elevateweb.co.uk/wp-content/themes/radial/zoom/images/large/image2.jpg');"></div>
</div>
</main>
I too tried with and without <header> Element, and to show which only working without <header> Element....
Can please someone help me and say where is my mistake? , Thanks!
Your code works, fine, if you had give your wrapper bigger size you would have seen your img does not lose it self but moves up behind header.
Problem was here in your js: backgroundPosition: (e.pageX * -1) + 'px ' + (e.pageY * -1) + 'px'
This positioned your zoomed image behind header. If you remove it shows in box and zooms but does not update zoom on mouse movement.
I tired to play around with this but I couldn't make it work and I don't have any time for this anymore. So i am leaving this answer here maybe someone else has an idea.
I tried to play around with this:
https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/pageX
I could make mouse to react only for inside wrapper but I couldn't make the picture work to react to this.
Good luck.
$('.wrapper').on('mouseenter', function() {
$('.img').css({
transform: 'scale(1.5)'
});
});
$('.wrapper').on('mouseleave', function() {
$('.img').css({
transform: 'scale(1.0)',
backgroundPosition: 'center center'
});
});
$('.wrapper').on('mousemove', function(updateDisplay) {
var box = document.querySelector(".wrapper");
var pageX = document.getElementById("x");
var pageY = document.getElementById("y");
function updateDisplay(event) {
$('.img').css({
backgroundPosition: (event.pageX * -1) + 'px ' + (event.pageY * -1) + 'px'
});
}
box.addEventListener("mousemove", updateDisplay, false);
box.addEventListener("mouseenter", updateDisplay, false);
box.addEventListener("mouseleave", updateDisplay, false);
});
.wrapper {
width: 300px;
height: 300px;
border: 1px solid navy;
overflow: hidden;
}
.img {
margin-right: auto; /* 1 */
margin-left: auto;
width: 100%;
padding-bottom: 100%;
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
<div class="log">
<img id="logo" src="/kalamulur/Photo/my-logo5.png" alt="">
</div>
<div class="linken">
<ul class="linken_ul">
<li class="dropdown">
<i>Home</i>
</li>
<li class="dropdown">
<i>Taschen & Rucksäcke</i>
<div class="dropdown-content">
<i>Taschen, Tablettaschen & Computertaschen</i>
<i>Rucksäcke</i>
<i>Beutel</i>
<i>Gürteltasche, Mäppchen & Schnupftabakdose</i>
</div>
</li>
<li class="dropdown">
<i>Haus</i>
<div class="dropdown-content">
<i>Dekoration</i>
<i>Teppich</i>
<i>Yogamatten & Necessaire</i>
</div>
</li>
<li class="dropdown">
<i>Kleidung</i>
<div class="dropdown-content">
<i>T-Shirts für Frauen & Männer</i>
<i>Hösen für Frauen & Männer</i>
<i>Schals, Mützen & Hute</i>
<i>Sandalen</i>
</div>
</li>
<li class="dropdown">
<i>Schmuck</i>
<div class="dropdown-content">
<i>Ringe, Ohrringe, Ketten ...</i>
</div>
</li>
<li class="dropdown">
<i>Über uns</i>
<div class="dropdown-content">
<i>Über uns</i>
<i>Kundenservice</i>
</div>
</li>
<li class="dropdown">
🌍 English
<div class="dropdown-content">
Spanisch
</div>
</li>
</ul>
</div>
</header>
<main>
<div class="wrapper">
<span id="x"></span>
<div class="img" style="background-image: url('http://www.elevateweb.co.uk/wp-content/themes/radial/zoom/images/large/image2.jpg');"></div>
<span id="y"></span>
</div>
</main>
I'm trying to make some sort of gallery on my website. There are 3 buttons, and underneath some text and picture are placed. When I click the button, I want that the content changes (to the content from button #2 etc.). How can I achieve that?
<ul>
<a href="">
<li>Btn1</li>
</a>
<a href="">
<li>Btn2</li>
</a>
<a href="">
<li>Btn3</li>
</a>
</ul>
<div class="list-first"">
<p class="list-first list-first-mobile">some text from first btn</p>
<img src="imgs/stock1.jpeg" alt="jpg from first btn" class="list-first-img">
</div>
Try this ,hope it helps
document.querySelectorAll('button').forEach((el, i) => {
el.addEventListener("click", () => {
document.querySelectorAll('p').forEach(ed => ed.style.display = "none")
el.nextElementSibling.style.display = "block"
})
})
<style>
.loc-caption:before {
content: attr(title);
display: block;
}
ul {
text-align: center;
padding: 0;
}
li {
width: 25%;
display: inline-block;
vertical-align: top;
}
li img {
max-width: 100%;
height: auto;
}
</style>
<ul>
<li class="loc-caption"><button>ClickME</button>
<p style="display:none" class="list-first list-first-mobile ">
<img src="https://chainimage.com/images/related-pictures-natural-sceneries-beautiful-wallpapers.jpg" alt="jpg from first btn " class="list-first-img "> some text from first btn
</p>
</li>
<li class="loc-caption"><button>ClickME2</button>
<p style="display:none" class="list-first list-first-mobile ">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRwXvWYU5tYrokWILC7lgjmCYqYGvActZnt9q8AcPs4dR7hMTBR" alt="jpg from second btn " class="list-first-img "> some text from second btn
</p>
</li>
<li class="loc-caption"><button>ClickME3</button>
<p style="display:none" class="list-first list-first-mobile ">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTzzP-CJnW3xuNeqqMcNLzK_IFPCywsEKifAWlajEzWcdALIDLI" alt="jpg from third btn " class="list-first-img "> some text from third btn
</p>
</li>
</ul>
I need help for my personal website. I want to make a one page site with a fixed top navbar (with transparent background). At the scrolling of the page, the color of the menu elements must change dinamically from black to white on the sections that have a dark background (they have a ".dark-bg" class) and return white on the other sections. All sections are 100vh height (except for the menu, of course). This is the HTML main structure of the site:
<section class="section--menu fixed-header">
<nav class="menu" id="navigation">
<ul class="menu__list pull-md-right">
<li class="menu__item menu__item--current">
<a class="menu__link" data-target="intro-fabio">home</a>
</li>
<li class="menu__item">
<a class="menu__link" data-target="about-fabio">about</a>
</li>
<li class="menu__item">
<a class="menu__link" data-target="skills-fabio">skills</a>
</li>
<li class="menu__item">
<a class="menu__link" data-target="works-fabio">works</a>
</li>
<li class="menu__item">
<a class="menu__link" data-target="contacts-fabio">contacts</a>
</li>
</ul>
</nav>
</section>
<!-- HOME
======================================================== -->
<section id="intro-fabio">
</section>
<!-- ABOUT
======================================================== -->
<section id="about-fabio" class="dark-bg">
</section>
<!-- SKILLS
======================================================== -->
<section id="skills-fabio">
</section>
<!-- WORKS
======================================================== -->
<section id="works-fabio" class="dark-bg">
</section>
<!-- CONTACTS
======================================================== -->
<section id="contacts-fabio">
</section>
I wrote this jQuery script but it seems to work only for the last section with ".dark-bg" class.
$(document).ready(function() {
$(".dark-bg").each(function() {
detectBg( $(this) );
});
function detectBg(sezione) {
$(window).scroll(function() {
var finestra = $(window).scrollTop();
var sezCurr = sezione.offset().top;
var sezNext = sezione.next().offset().top;
if (finestra >= sezCurr && finestra < sezNext) {
$('.menu__link').css("color", "#ebebeb");
}
else {
$('.menu__link').css("color", "#1c1c1c");
}
});
}
});
Thanks in advance!
You need to handle scroll event of the window, and in that handler, check if any of the dark section is under the menu, if so, then change the color of the menu links. Here is an example of changing the color for all links, but it can be easily extended to do it separately for each link:
$(window).scroll(function() {
var vpHeight = $(window).height();
var isBlack = false;
$(".dark-bg").each(function(i, section) {
if(isBlack) {
return;
}
var offset = $(section).offset().top - $(window).scrollTop();
if(((offset + vpHeight) >= 0) && ((offset + vpHeight) <= vpHeight)) {
isBlack = true;
return;
}
});
$(".menu__link").css("color", isBlack ? "white" : "black");
});
body {
padding: 0;
margin: 0;
}
ul {
position:fixed;
background: orange;
padding: 0;
margin: 0;
list-style-type: none;
}
ul > li {
float: left;
padding: 0 4px;
}
section {
background:red;
height: 100vh;
}
.dark-bg {
background: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="menu__list pull-md-right">
<li class="menu__item menu__item--current">
<a class="menu__link" data-target="intro-fabio">home</a>
</li>
<li class="menu__item">
<a class="menu__link" data-target="about-fabio">about</a>
</li>
<li class="menu__item">
<a class="menu__link" data-target="skills-fabio">skills</a>
</li>
<li class="menu__item">
<a class="menu__link" data-target="works-fabio">works</a>
</li>
<li class="menu__item">
<a class="menu__link" data-target="contacts-fabio">contacts</a>
</li>
</ul>
<!-- HOME
======================================================== -->
<section id="intro-fabio">
</section>
<!-- ABOUT
======================================================== -->
<section id="about-fabio" class="dark-bg">
</section>
<!-- SKILLS
======================================================== -->
<section id="skills-fabio">
</section>
<!-- WORKS
======================================================== -->
<section id="works-fabio" class="dark-bg">
</section>
<!-- CONTACTS
======================================================== -->
<section id="contacts-fabio">
</section>