Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

use useRef to get current class

const Navbar = () => {
    const navbar = useRef(null)
    window.addEventListener('scroll', () => {
        if (window.scrollY > navbar.current.offsetHeight + 550) {
            navbar.current.classList.add('active');
        }else{
            navbar.current.classList.remove('active');
        }
    });
    return (
        <Fragment>
            <nav id='navbar' className="nav" ref={navbar}>
                <div className="container">
                    <h1 className="logo"><a href="/index.html"></a>My Website</h1>
                    <ul>
                        <li><a href="#" className="current">Home</a></li>
                        <li><a href="#">About</a></li>
                        <li><a href="#">Services</a></li>
                        <li><a href="#">Contact</a></li>
                     </ul>
                </div>
            </nav>
            {/* other elements */}
        </Fragment>
    )
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: moves zeroes 
Javascript :: lexical scope javascript 
Javascript :: how-to-show-base64-image-in-react 
Javascript :: execute shell command in javascript 
Javascript :: js compare objects 
Javascript :: ${ js 
Javascript :: current page number and clicked page number jqery datatables 
Javascript :: why geting empty array from mongodb 
Javascript :: how to remove duplicate values in array javascript 
Javascript :: redux devtools config 
Javascript :: url to buffer node.js 
Javascript :: ping discord by autocode 
Javascript :: match regex 
Javascript :: post method in reactjs hooks. 
Javascript :: shuffle array 
Javascript :: jsx example 
Javascript :: create a promise in javascript 
Javascript :: javascript double question mark 
Javascript :: prepend to js array 
Javascript :: javascript get currency symbol by currencyCode 
Javascript :: headless ui modal 
Javascript :: events onclick 
Javascript :: mogoosejs 
Javascript :: how to export fs.readFile 
Javascript :: pass text and variable in alert javascript 
Javascript :: how to get data from for loop in react native 
Javascript :: how to seperate header body and footer in node 
Javascript :: get numbers from a string 
Javascript :: firebase get key value 
Javascript :: js ctx dash line 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =