简体   繁体   English

我试图让我的导航栏在我向下滚动时向上缩回,然后在我向上滚动时再次出现,但它没有做任何事情

[英]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'm very new to writing code but I've seen this done the same way, I don't get why it does nothing.我对编写代码非常陌生,但我看到它以同样的方式完成,我不明白为什么它什么都不做。 The issue is likely somewhere in the CSS part.问题可能在 CSS 部分的某个地方。

JavaScript JavaScript

var navbar = document.getElementsByClassName("navbar");
       var lastY = window.pageYOffset;
       window.onscroll = function()
      {
       var currY = window.pageYOffset;
       if (lastY > currY)
       {
        document.getElementsByClassName("navbar").style.top = "0";
       }
       else
       {
        document.getElementsByClassName("navbar").style.top = "-50px";
       }
       lastY = currY;
      }

HTML- I'm not exactly sure why I have to put all the links in their separate navbar-items class divs, but if I don't do this they start overlapping the header (i want the navbar to have that name on the left and the links on the right) and also make the other contents of the page after the navbar vanish. HTML-我不完全确定为什么我必须将所有链接放在它们单独的导航栏项目类 div 中,但是如果我不这样做,它们就会开始重叠标题(我希望导航栏在左侧有该名称以及右侧的链接),并且还会使导航栏后页面的其他内容消失。

<div class="navbar">
        <h1>Квартална кръчма Тримата глупаци</h1>
        <div class="navbar-items">
          <div class="navbar-items"><a href="index.html">Home</a></div>
          <div class="navbar-items"><a style="color:red;" href="Menu.html">Menu</a></div>
          <div class="navbar-items"><a href="About_Us.html">About us</a></div>
          <div class="navbar-items"><a href="Contact_Us.html">Contact us</a></div>
        </div>
      </div>

CSS CSS

.navbar {
    overflow: hidden;
    position: fixed;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background:#505d61;
    z-index: 99;
    padding: 10px;
    height: 60px;
    top: 0;
    width: 100%;
    box-shadow: 0 0 10px black;
}

.navbar-items {
    overflow: hidden;
    float:left;
    display:block;
    margin-left: 40px;
    margin-right: 5px;
    gap: 80px;
    font-size: 21px;
    font-weight: 550;
}

.navbar a{
   color:black;
   text-decoration: none;
}

.navbar a:hover{ 
    color: white;
}

As the comment from CBroe points to, you issue is that you are expecting an element back from document.getElementsByClassName("navbar") but you are actually getting back an array.正如 CBroe 的评论所指出的那样,您的问题是您期望从document.getElementsByClassName("navbar")返回一个元素,但实际上您正在返回一个数组。 To access the element you need to pull it out of the array, you can do this like document.getElementsByClassName("navbar")[0] .要访问您需要将其从数组中拉出的元素,您可以像document.getElementsByClassName("navbar")[0]一样执行此操作。

So updating your code to因此,将您的代码更新为

var lastY = window.pageYOffset;
window.onscroll = function(){
    var currY = window.pageYOffset;
    if (lastY > currY){
        document.getElementsByClassName("navbar")[0].style.top = "0";
    }
    else{
        document.getElementsByClassName("navbar")[0].style.top = "-50px";
    }
    lastY = currY;
}

Should fix the issue.应该解决问题。

暂无
暂无

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

相关问题 我试图在用户向下滚动时修复我的导航栏,但是当我向下滚动时它不会停留 - I'm trying to make my navbar fixed when the user scrolls down, but when I scroll down it doesn't stay 我希望我的导航栏在我向下滚动时消失并在我向上滚动时出现。 导航栏是固定的。 怎么了? - I want my navbar to disappear when I scroll down and appear when I scroll up. The navbar is fixed. What is wrong? 当我向上和向下滚动时,导航栏品牌不会被隐藏 - Navbar brand doesn't get hidden when I scroll up and down 向下滚动certian amont时,我试图使div出现? - I am trying to make a div appear when i scroll down a certian amont ? 当我向上滚动并在向上滚动或当我将鼠标悬停在上面时,如何使导航条变得透明? - How do i make my nav bar transparent when I scroll down and come back when I scroll up or when I hover my mouse over is? 当整页js上下滚动时,我想重新启动视频,我能做些什么吗? - I wanted to restart the video when full page js scroll up and down, anything i can do for this? 向下滚动页面时,如何使导航栏下方出现阴影? - How do I make a shadow appear under the navigation bar when I scroll down the page? 单击按钮时,如何使导航栏滚动页面? - How do I make a navbar scroll the page when clicked on a button? 向下滚动时如何移动图像? - How do I make an image move when i scroll down? 在React中滚动时,如何使导航栏突出显示? - How do I make my navbar highlight as I scroll in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM