简体   繁体   English

使用 html/css 转到页面时隐藏导航栏

[英]hide nav bar when going to a page using html/css

I am working on a simple portfolio for my friend.我正在为我的朋友制作一个简单的作品集。 He wants it so that when I go to his portfolio section, the navbar simply should be hidden and it should only show up when I hover over it.他想要它,这样当我 go 到他的投资组合部分时,导航栏应该简单地隐藏起来,它应该只在我 hover 超过它时显示。

I saw a lot of tutorials on w3schools on how they do it but quite wasn't able to do it.我在 w3schools 上看到了很多关于他们如何做到这一点的教程,但完全无法做到。 Here is my HTML code for my navigation.这是我的 HTML 导航代码。

Once again, I want it so that the Navigation page should be hidden when I click "portfolio" and that it should only show when I hover over it.再一次,我想要它,以便在我单击“投资组合”时隐藏导航页面,并且它应该只在我 hover 超过它时显示。

Here is an example of what I mean.这是我的意思的一个例子。 https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_navbar_hide_scroll https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_navbar_hide_scroll

HTML CODE: HTML 代码:

    <header id="header" class="header-tops">
    <div class="container">

      <h1><a href="index.html">Parth Prajapati</a></h1>      
      <!-- <a href="index.html" class="mr-auto"><img src="assets/img/logo.png" alt="" class="img-fluid"></a> -->
      <h2>I'm a 4th year <span>Architectural Student</span> based in Toronto</h2>

      <nav class="nav-menu d-none d-lg-block">
        <ul>
          <li class="active"><a href="#header">Home</a></li>
          <li><a href="#about">About</a></li>          
          <li><a href="#portfolio">Portfolio</a></li>
          <li><a href="#contact">Contact</a></li>
        </ul>
      </nav><!-- .nav-menu -->

      <div class="social-links">        
        <!-- <a href="#" class="instagram"><i class="icofont-instagram"></i></a>         -->
        <a href="https://www.linkedin.com/in/parth-prajapati-93b34b143/" target="_blank" class="linkedin"><i class="icofont-linkedin"></i></a>
      </div>
    </div>
  </header><!-- End Header -->

JS Code: JS代码:

var prevScrollpos = window.pageYOffset;
window.onscroll = function () {
  var currentScrollPos = window.pageYOffset;
  if (prevScrollpos > currentScrollPos) {
    document.getElementsByClassName('container').style.top = "0";
  } else {
    document.getElementsByClassName('container').style.top = "-50px";
  }
  prevScrollpos = currentScrollPos;
}

what css properties did you add?您添加了哪些 css 属性? There are few things you can do to improve your code.您可以做一些事情来改进您的代码。 1)see if your navbar position needs fixed or sticky property and 2)use id to your navbar, classes have lower specificity than id. 1)查看您的导航栏 position 是否需要fixedsticky属性,以及 2)将 id 用于您的导航栏,类的特异性低于 id。 the example you provided has this css.您提供的示例有这个 css。

#navbar {
  background-color: #333;
  position: fixed;
  top: 0;
  width: 100%;
  display: block;
  transition: top 0.3s;
}

your js code just applying the style to the whole container class, is that what you really want?您的 js 代码只是将样式应用于整个容器 class,这是您真正想要的吗?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM