admin管理员组文章数量:1026620
I'm trying to setup a sticky navbar with nextjs but the resulting navbar is not working like it should. Can anyone help with the code below?
import React, { useEffect } from 'react'
import styles from './navbar.module.css'
const Navbar = () => {
const [scrolled, setScrolled] = React.useState(false);
const handleScroll = () => {
const offset = window.scrollY;
if (offset > 200) {
setScrolled(true);
}
else {
setScrolled(false);
}
}
useEffect(() => {
window.addEventListener('scroll', handleScroll)
})
return (
<div className={scrolled ? styles.scrolled : styles.navbar}>
<ul className={styles.ul}>
Something
</ul>
Navbar
</div>
)
};
export default Navbar;
I'm trying to setup a sticky navbar with nextjs but the resulting navbar is not working like it should. Can anyone help with the code below?
import React, { useEffect } from 'react'
import styles from './navbar.module.css'
const Navbar = () => {
const [scrolled, setScrolled] = React.useState(false);
const handleScroll = () => {
const offset = window.scrollY;
if (offset > 200) {
setScrolled(true);
}
else {
setScrolled(false);
}
}
useEffect(() => {
window.addEventListener('scroll', handleScroll)
})
return (
<div className={scrolled ? styles.scrolled : styles.navbar}>
<ul className={styles.ul}>
Something
</ul>
Navbar
</div>
)
};
export default Navbar;
Share
Improve this question
asked May 18, 2021 at 18:40
Co.tibiCo.tibi
531 gold badge1 silver badge5 bronze badges
1
- 1 What do you mean by "not working like it should" ? Can you describe what exactly happens that you don't want, or does not happen that you want ? – Roman Mkrtchian Commented May 18, 2021 at 18:48
2 Answers
Reset to default 2If what you want is to have a sticky navbar, you can do it with pure CSS with position: sticky
like this:
header, nav, main {
padding: 1.7rem 1rem;
}
header {
background-color: #d99;
}
nav {
position: sticky;
top: 2rem;
background-color: #9d9;
}
main {
height: 100vh;
background-color: #99d;
}
<header>
Header
</header>
<nav>
Navbar
</nav>
<main>
Main
</main>
I'm not sure if this would fix your problem, but you could try changing your useEffect
to have an empty dependency array at the end of it so that it runs when the ponent mounts, like this:
useEffect(() => {
window.addEventListener('scroll', handleScroll)
}, [])
I'm trying to setup a sticky navbar with nextjs but the resulting navbar is not working like it should. Can anyone help with the code below?
import React, { useEffect } from 'react'
import styles from './navbar.module.css'
const Navbar = () => {
const [scrolled, setScrolled] = React.useState(false);
const handleScroll = () => {
const offset = window.scrollY;
if (offset > 200) {
setScrolled(true);
}
else {
setScrolled(false);
}
}
useEffect(() => {
window.addEventListener('scroll', handleScroll)
})
return (
<div className={scrolled ? styles.scrolled : styles.navbar}>
<ul className={styles.ul}>
Something
</ul>
Navbar
</div>
)
};
export default Navbar;
I'm trying to setup a sticky navbar with nextjs but the resulting navbar is not working like it should. Can anyone help with the code below?
import React, { useEffect } from 'react'
import styles from './navbar.module.css'
const Navbar = () => {
const [scrolled, setScrolled] = React.useState(false);
const handleScroll = () => {
const offset = window.scrollY;
if (offset > 200) {
setScrolled(true);
}
else {
setScrolled(false);
}
}
useEffect(() => {
window.addEventListener('scroll', handleScroll)
})
return (
<div className={scrolled ? styles.scrolled : styles.navbar}>
<ul className={styles.ul}>
Something
</ul>
Navbar
</div>
)
};
export default Navbar;
Share
Improve this question
asked May 18, 2021 at 18:40
Co.tibiCo.tibi
531 gold badge1 silver badge5 bronze badges
1
- 1 What do you mean by "not working like it should" ? Can you describe what exactly happens that you don't want, or does not happen that you want ? – Roman Mkrtchian Commented May 18, 2021 at 18:48
2 Answers
Reset to default 2If what you want is to have a sticky navbar, you can do it with pure CSS with position: sticky
like this:
header, nav, main {
padding: 1.7rem 1rem;
}
header {
background-color: #d99;
}
nav {
position: sticky;
top: 2rem;
background-color: #9d9;
}
main {
height: 100vh;
background-color: #99d;
}
<header>
Header
</header>
<nav>
Navbar
</nav>
<main>
Main
</main>
I'm not sure if this would fix your problem, but you could try changing your useEffect
to have an empty dependency array at the end of it so that it runs when the ponent mounts, like this:
useEffect(() => {
window.addEventListener('scroll', handleScroll)
}, [])
本文标签: javascriptSticky navbar with React NextjsStack Overflow
版权声明:本文标题:javascript - Sticky navbar with React Nextjs - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745650734a2161301.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论