I have successfully created a function called changeColour
in my code that changes the color of the navbar when scrolling past a certain point. However, I am encountering an issue where I only want this effect to be applied on the Home page. I would like all other pages (AboutUs, Shop, ContactUs) to maintain their default styles using the selectors
.navbarbg .nav-list-green a .nav-list-green a:hover
.
Below is the current state of my code:
Please find below my initial implementation inside const changeColour {}
.
Navbar.jsx
'use client'
import React, { useState, useEffect } from 'react'
import { Link } from 'react-router-dom'
import Logo from './Logo.jsx'
import '../App.css'
const Navbar = () => {
const state = useState()
const navigation = [
{_id:101, title: 'ABOUT US', href: '/AboutUs'},
{_id:102, title: 'SHOP', href: '/Shop'},
{_id:103, title: 'MENU', href: '/Menu'},
{_id:104, title: 'CONTACT US', href: '/ContactUs'},
];
const [colour, setColour] = useState(false)
useEffect(() => {
const changeColour = () => {
if (isItHomePage()) setColour(window.scrollY >= 650)
};
function isItHomePage() {
return window.location.href.indexOf("/") ===
0;
}
window.addEventListener('scroll', changeColour);
return () => {
window.removeEventListener('scroll', changeColour)
}
}, [])
return (
<div className={colour ? 'navbar navbarbg' : 'navbar'}>
<div className="container">
<Logo />
<ul className={colour ? 'nav-list-beige nav-list-green' : 'nav-list-beige'}
>
{
navigation.map((item) => (
<Link to={item?.href} key={item._id}>
<li className={colour ? 'nav-link nav-link-colour' : 'nav-link'}>
{item?.title}
<span className={`${item?.href === state && 'style=color: blue;'}`}></span>
</li>
</Link>
))
}
</ul>
<div>
</div>
</div>
</div>
)
}
export default Navbar
My App.css
/* navbar.jsx CSS */
.navbarbg {
background-color: hsl(0, 0%, 100%);
backdrop-filter: blur(30px);
box-shadow: 1px 1px 5px 1px hsl(0, 0%, 63%);
transition-duration: 500ms;
}
.navbar {
/* (green) background-color: hsl(96, 24%, 44%, 90%); */
/* background-color: hsl(49, 87%, 88%); */
/* border-bottom: 1px solid black; */
position: fixed; top: 0px;
height: 80px;
width: 100%;
z-index:99;
transition-duration: 500ms
}
.nav-list-beige {
display: flex;
gap: 50px;
list-style-type: none;
justify-content: center;
font-size: larger;
display: flex;
position: relative; top: -30px; left: 350px;
}
.nav-list-beige a {
color: hsl(48, 54%, 89%);
text-decoration: none;
transition-duration: 500ms;
}
.nav-list-beige a:hover {
color: hsl(48, 100%, 85%);
transition-duration: 300ms;
text-shadow: 0px 0px 2px;
}
.nav-list-green a {
color: hsl(0, 0%, 0%);
}
.nav-list-green a:hover {
color: hsl(96, 24%, 44%);
}
I would greatly appreciate any suggestions you may have. Thank you.