Seeking assistance with altering the background color of a header upon scroll. I keep receiving null when trying to reference the navbar
variable. Any suggestions on how I can achieve this effect? Utilizing transform:translateY
for a subtle scroll animation.
import React from 'react'
import { Navbar, Container, Nav } from 'react-bootstrap'
import { LinkContainer } from 'react-router-bootstrap'
const Header = () => {
const navbar = document.getElementById("navbar");
let scrolled = false;
window.onscroll = function () {
if (document.body.scrollTop >= 200 || document.documentElement.scrollTop >= 200) {
navbar.classList.add('color-nav');
if (!scrolled) {
navbar.style.transform = 'translateY(-70px)'
}
setTimeout(function () {
navbar.style.transform = 'translateY(0px)'
scrolled = true
}, 200)
} else {
navbar.classList.remove('color-nav');
scrolled = false
}
};
return (
<div id='navbar' >
<Navbar fixed="top" className='navbar' collapseOnSelect expand="lg" >
<Container>
<LinkContainer to='/'>
<Navbar.Brand className='logo' >Logo</Navbar.Brand>
</LinkContainer>
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
<Navbar.Collapse id="responsive-navbar-nav">
<Nav className="me-auto">
<LinkContainer to='/services'>
<Nav.Link className='links'>Services</Nav.Link>
</LinkContainer>
</Nav>
<Nav>
<LinkContainer to='/login'>
<Nav.Link className='links'>Login</Nav.Link>
</LinkContainer>
<LinkContainer to='/signup'>
<Nav.Link className='links'>
Sign Up
</Nav.Link>
</LinkContainer>
</Nav>
</Navbar.Collapse>
</Container>
</Navbar>
</div >
)
}
export default Header