I am using react.js. What I am doing is when the user scrolls the page then I have to add the class to the header menu. I tried below code but it's not working.
I create a file in the public folder and added below the script in the index.html
<script src="%PUBLIC_URL%/custom.js"></script>
custom.js
const header = document.getElementById("header_menu");
const sectionOneOptions = {
rootMargin: "-200px 0px 0px 0px"
};
const sectionOneObserver = new IntersectionObserver(function(
entries,
sectionOneObserver
) {
entries.forEach(entry => {
if (!entry.isIntersecting) {
header.classList.add("menuscroll");
} else {
header.classList.remove("menuscroll");
}
});
},
sectionOneOptions);
Header.js
import React from 'react';
import ReactDOM from 'react-dom';
import {Link} from 'react-router-dom';
const HeaderMenu = () => {
return (
<header id="header_menu">
<div className="right-menu float-right">
<ul>
<li><Link to={'/'} >Home</Link></li>
<li><Link to={'/about'} >About Us</Link></li>
<li><Link to={'/contact'} >Contact Us</Link></li>
</ul>
</div>
</header>
);
}
export default HeaderMenu;