简体   繁体   English

我试图在用户向下滚动时修复我的导航栏,但是当我向下滚动时它不会停留

[英]I'm trying to make my navbar fixed when the user scrolls down, but when I scroll down it doesn't stay

I'm trying to make it so that when the user starts scrolling, the navbar becomes fixed at the top.我试图做到这一点,以便当用户开始滚动时,导航栏固定在顶部。 I'm using vanilla javascript to add my fixed navbar class dynamically on a scroll event, and when I check the devtools the class is being added, but the navbar doesn't change, so I'm assuming the issue is with my CSS.我正在使用 vanilla javascript 在滚动事件上动态添加我的固定导航栏类,当我检查 devtools 正在添加该类时,但导航栏没有改变,所以我假设问题出在我的 CSS 上。 Any help is appreciated.任何帮助表示赞赏。

 const navbar = document.querySelector(".navbar"); // fixed navbar on scroll window.addEventListener("scroll", () => { if (window.scrollY > 80) { navbar.classList.add(".navbar-fixed"); } else { navbar.classList.remove(".navbar-fixed"); } });
 .navbar-fixed { position: fixed; top: 0; left: 0; width: 100%; background: white; color: black; z-index: 2; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1); } .nav-container { display: flex; align-items: center; justify-content: space-between; max-width: 1100px; margin: 0 auto; padding: 0 30px; height: 100%; } .nav { list-style-type: none; display: flex; background: transparent; }
 <!-- NAVBAR --> <div class="bg-img"> <nav id="home" class="navbar"> <div class="nav-container"> <div class="nav-logo">Logo</div> <ul class="nav"> <li> <a href="#home">Home</a> </li> <li> <a href="#about">About</a> </li> <li> <a href="#">Events</a> </li> </ul> </div> </nav> <div style="height: 1000px"></div> </div> <!-- END OF NAVBAR -->

Pretty close, just remove the dot when you add and remove the classList of navbar:非常接近,只需在添加和删除导航栏的 classList 时删除点:

 const navbar = document.querySelector(".navbar"); // fixed navbar on scroll window.addEventListener("scroll", () => { if (window.scrollY > 80) { navbar.classList.add("navbar-fixed"); } else { navbar.classList.remove("navbar-fixed"); } });

const navbar = document.querySelector("#home");
// fixed navbar on scroll
window.addEventListener("scroll", () => {
  if (window.scrollY > 80) {
    navbar.classList.add("navbar-fixed");
  } else {
    navbar.classList.remove("navbar-fixed");
  }
  
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我试图让我的导航栏在我向下滚动时向上缩回,然后在我向上滚动时再次出现,但它没有做任何事情 - I'm trying to make my navbar retract upwards when I scroll down and then appear again when I scroll up, but it doesn't do anything 我希望我的导航栏在我向下滚动时消失并在我向上滚动时出现。 导航栏是固定的。 怎么了? - I want my navbar to disappear when I scroll down and appear when I scroll up. The navbar is fixed. What is wrong? 如何创建固定在屏幕底部的页脚,但当用户向下滚动时,它不再是固定页脚? - How do I create a footer that is fixed at the bottom of the screen but when the user scrolls down it is no longer fixed footer? 当我向上和向下滚动时,导航栏品牌不会被隐藏 - Navbar brand doesn't get hidden when I scroll up and down 当我在 hover 上方时,为什么我的下拉菜单不可见? - Why doesn't my drop-down menu stay visible when I hover over it? 我有一个导航栏,当您向下滚动时,它会变得固定。 除非您向下滚动,否则它不起作用 - I have a navigation bar that becomes fixed when you scroll down. It doesn't work unless you're scrolled down 将鼠标悬停在项目上时,我的CSS / JS下拉菜单不会消失 - My CSS/JS drop down menu won't stay down when I hover over the items 无论访问者滚动的页面有多低,如何使DIV停留在屏幕顶部? - How do I make a DIV stay at the top of the screen no matter ow far down the page my visitor scrolls? 当用户向下滚动时,如何使另一个 HTML 页面出现在 HTML 页面中? - How can I make another HTML page appear within an HTML page when the User Scrolls down? 滚动时如何保持顶部菜单固定 - How to keep my top menu stay fixed when i scroll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM