export const NavBar = () => {
return <div className="navbar">this is navbar</div>;
};
const Content = () => {
return (
<div className="main">
<div className="background">
some content
</div>
</div>
);
};
const App = () => {
return (
<>
<NavBar/>
<Content/>
</>
);
}
ReactDOM.render(<App/>,document.body);
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", monospace;
}
.App {
text-align: center;
}
.App-logo {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: #61dafb;
}
@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.navbar {
width: 100%;
height: 64px;
background: red;
position: fixed;
top: 0;
left: 0;
z-index: 100000;
}
.main {
height: 200vh;
width: 100%;
position: relative;
background: blue;
}
.background {
width: 100%;
height: 200px;
background: red;
position: absolute;
top: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
I'm having trouble finding a solution and don't know where to begin.
Here's a simple codepen that showcases my issue:
Codepen
My objective is to have the navbar display a black border when it overlaps the "some content" section. Is there any solution that can assist me with this? Or should I simply set it like: When y scroll is between 247px and 453px change border. Something like this:
const [scroll, setScroll] = useState();
useEffect(() => {
setScroll(window.pageYOffset)
}, [window.pageYOffset])
return <div style={{ borderBottom: `solid black ${(scroll >= 247 && scroll <= 453) && '2px'}` }}>navbar</div>
Is there a more efficient approach to achieving this?