I want the hamburger menu to change color on different sections. I tried few things but - first attempt is working only while scrolling down and second attempt working on only first tow sections
<div class="nav">
<svg class="burger" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="32px" id="Layer_1" style="enable-background:new 0 0 32 32;" version="1.1" viewBox="0 0 32 32" width="32px" xml:space="preserve">
<defs>
<linearGradient id="black" x1="0%" y1="0%" x2="100%" y2="0%" gradientTransform="rotate(90)">
<stop offset="0%" stop-color="#000"/>
<stop offset="100%" stop-color="#000"/>
</linearGradient>
<linearGradient id="white" x1="0%" y1="0%" x2="100%" y2="0%" gradientTransform="rotate(90)">
<stop offset="0%" stop-color="#fff"/>
<stop offset="100%" stop-color="#fff"/>
</linearGradient>
</defs>
<path d="M4,10h24c1.104,0,2-0.896,2-2s-0.896-2-2-2H4C2.896,6,2,6.896,2,8S2.896,10,4,10z M28,14H4c-1.104,0-2,0.896-2,2 s0.896,2,2,2h24c1.104,0,2-0.896,2-2S29.104,14,28,14z M28,22H4c-1.104,0-2,0.896-2,2s0.896,2,2,2h24c1.104,0,2-0.896,2-2 S29.104,22,28,22z"/>
</svg>
</div>
<section class="section dark" data-id="dark">One</section>
<section class="section light" data-id="light">Two</section>
<section class="section dark" data-id="dark">Three</section>
<section class="section light" data-id="light">Four</section>
<section class="section dark" data-id="dark">Five</section>
<section class="section light" data-id="light">Six</section>
<section class="section dark" data-id="dark">Seven</section>
<section class="section light" data-id="light">Eight</section>
CSS
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
transition: background 1s ease-in-out;
}
.nav {
position: fixed;
width: 100;
top: 0;
left: 0;
right: 0;
rpadding: 24px 28px;
padding: 2vh 28px;
rpadding: 0;
display: flex;
flex-direction: column;
justify-content: space-between;
z-index: 999;
}
svg {
stroke: white;
stroke-width: 3px;
}
/* svg.dark{
stroke: black;
}
svg.light{
stroke: white;
} */
.section {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh;
color: #000;
font-family: sans-serif;
font-size: 72px;
font-weight: 600;
text-transform: uppercase;
border:1px solid red;
}
[data-id = "dark"]{
background:black;
color:white;
}
JS attempt 1 ( codepen- https://codepen.io/tarunpatnayak/pen/rNYQpwL)
const mSections = document.querySelectorAll("section");
const Hsvg = document.querySelector("svg.burger");
// trial one
mOptions = {
threshold: "0.9"
};
mObserver = new IntersectionObserver((entries) => {
entries.forEach((e) => {
console.log(e);
// workin on scroll down
// please un - comment svg.dark and svg.light to make this one work
if (e.isIntersecting) {
// console.log(e.target.getAttribute('data-id'));
// Hsvg.classList.remove("light");
if (e.target.getAttribute("data-id") !== "light") {
Hsvg.classList.add("light");
} else {
Hsvg.classList.remove("light");
}
if (e.target.getAttribute("data-id") !== "dark") {
Hsvg.classList.add("dark");
} else {
Hsvg.classList.remove("dark");
}
}
});
}, mOptions);
mSections.forEach((mSection) => {
mObserver.observe(mSection);
});
JS attempt 2 (codepen - https://codepen.io/tarunpatnayak/pen/JjOeMJJ )
const mSections = document.querySelectorAll("section");
const Hsvg = document.querySelector("svg.burger");
// trial one
mOptions = {
threshold: "0.9"
};
mObserver = new IntersectionObserver((entries) => {
entries.forEach((e) => {
console.log(e);
// workin on scroll down method - 2
// please comment out svg.dark and svg.light to make this one work
if (e.isIntersecting) {
Hsvg.style.stroke = (e.target.classList.contains('light')) ? 'url(#black)' : 'url(#white)';
}
else {
Hsvg.style.stroke = (e.target.classList.contains('dark')) ? 'url(#white)' : 'url(#black)';
}
});
}, mOptions);
mSections.forEach((mSection) => {
mObserver.observe(mSection);
});
JS attempt 3 (jQuery)(codepen - https://codepen.io/tarunpatnayak/pen/VwrVrba )
var dOffset = $("[data-id = dark]").offset();
var lOffset = $("[data-id = light]").offset();
var Hsvg = $("svg.burger");
$(window).scroll(function () {
/* initial color */
// Hsvg.css('stroke', 'white');
// dOffset.each(function () {
if ($(window).scrollTop() >= dOffset.top) {
Hsvg.css("stroke", "white");
}
// });
// lOffset.each(function () {
if ($(window).scrollTop() >= lOffset.top) {
Hsvg.css("stroke", "black");
}
// });
});