导航 css 在 jquery .show() 之后不工作

nav css not working after jquery .show()

我的导航在悬停时有一个底部边框,在您在网站上向下滑动后,我的第一个导航 git 被隐藏,我的第二个导航被显示。导航具有相同的 css,但我在第二个导航上的边框底部不起作用。这是我的 css 或 jquery 的问题吗?谁能帮我解决这个问题?

HTML:

<header>
        <a href="#home" id="logo" class="smoothScroll">
            <img src="img/logo.png">
        </a>
        <nav>
            <a href="#home" class="smoothScroll">Home</a>
            <a href="#about" class="smoothScroll">About</a>
            <a href="#projects" class="smoothScroll">Projects</a>
            <a href="#contact" class="smoothScroll">Contact</a>
        </nav>
    </header>
    <div id="header" class="fade">
        <a href="#home" id="logo" class="smoothScroll">
            <img src="img/logo-white.png">
        </a>
        <nav>
            <a href="#home" class="smoothScroll">Home</a>
            <a href="#about" class="smoothScroll">About</a>
            <a href="#projects" class="smoothScroll">Projects</a>
            <a href="#contact" class="smoothScroll">Contact</a>
        </nav>
    </div>

Jquery:

$(window).scroll(function() {
    if ($(window).scrollTop() > 100 ){
        $('header').show();
        $('header').removeClass('slideUp');
        $('header').addClass('slideDown');
        $('#header').addClass('hide');
    } else {
        $('header').addClass('slideUp');
        $('#header').removeClass('hide');
    };      
});

CSS:

header, #header{
  height: 75px;
  background: rgba(26, 28, 30, 0.75);
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  z-index: 50;
}
header{
  display: none;
}
#header{
  background-color: transparent;
}
nav{
  position: fixed;
  right: 0;
  margin-top: 22.5px; 
  margin-right: 30px;
  z-index: 55;
}
nav a:link, nav a:visited{
  position: relative;
  display: inline-block;
  padding: 0px 10px 0px 10px;
  text-decoration: none;
  font-size: 1.5em;
  color: #fffffa;
}
nav a:after{
  content: '';
  display: block;
  margin: auto;
  width: 0px;
  background: transparent;
  transition: width .5s ease, 
  background-color .5s ease;
}
nav a:hover:after{
  width: 100%;
  border-bottom: 2px solid #fffffa !important;
}

看起来您的 JQuery 在 ID 选择器中存在一些语法问题。确保包含 #

$(window).scroll(function() {
    if ($(window).scrollTop() > 100 ){
        $('#header').show();
        $('#header').removeClass('slideUp');
        $('#header').addClass('slideDown');
        $('#header').addClass('hide');
    } else {
        $('#header').addClass('slideUp');
        $('#header').removeClass('hide');
    };      
});

编辑

根据反馈,我重新审视了这个问题,并根据我对我认为可以解决这个问题的解释制作了一个 fiddle。持续的反馈将有助于解决此问题。

$(window).scroll(function() {
    if ($(window).scrollTop() > 100 ){
        $('header').addClass('hide');
        $('#header').removeClass('hide');
    } else {
        $('#header').addClass('hide');
        $('header').removeClass('hide');
    };      
});

Updated JSFiddle Link

脚本中的第一个问题是 HTML 代码。 您应该将第二个导航放在 header 元素内,如下所示:

 <header>
     <nav id="first-nav">
        <a href="#home" class="smoothScroll">Home</a>
        <a href="#about" class="smoothScroll">About</a>
        <a href="#projects" class="smoothScroll">Projects</a>
        <a href="#contact" class="smoothScroll">Contact</a>
     </nav>
     <nav id="second-nav">
        <a href="#home" class="smoothScroll">Home</a>
        <a href="#about" class="smoothScroll">About</a>
        <a href="#projects" class="smoothScroll">Projects</a>
        <a href="#contact" class="smoothScroll">Contact</a>
     </nav>
 </header>

为了方便处理,我给出了"first-nav"和"second-nav"的id。 第二个问题是你的jQuery。在您的 if 条件中,您告诉 jQuery 在浏览器滚动超过 100 时隐藏 header 元素. 这就是问题所在,因为 nav 元素需要在 header 元素内部使用。

我已经编辑了你的jQuery。

   $(document).ready(funciton() {
      //hide the second nav
      $('#second-nav').hide();

      //when the scroll event fired
      $(window).scroll(function() {
           if ($(window).scrollTop() > 100) {
               $('#second-nav').show();
               $('#first-hav').hide();
           }
           else {
               $('#first-nav').show();
               $('#second-hav').hide();
           }
      }); 
   });

如果有任何不清楚的地方请告诉我,我很乐意提供帮助。