【问题标题】:nav css not working after jquery .show()jquery .show() 后导航 css 不工作
【发布时间】:2015-04-19 21:17:15
【问题描述】:

我的导航在悬停时有一个边框底部,在您在网站上向下滑动后,我的第一个导航 gits 被隐藏并且我的第二个导航被显示。导航具有相同的 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;
}

【问题讨论】:

    标签: javascript jquery html css css-transitions


    【解决方案1】:

    看起来您的 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');
        };      
    });
    

    编辑

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

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

    Updated JSFiddle Link

    【讨论】:

    • 不,这不是问题,我的第一个导航在
      中,第二个导航在 ID 为“header”的
    • 不理解这里的赞成票 - &lt;header&gt; 显然是一个不需要标识符的 HTML5 元素。此外,一行中的四个相同的选择器可以很容易地链接起来以获得更简洁的代码。
    • @Shikkediel 感谢您的反馈。重新访问这个问题,我可以看到您的担忧。由于一些事情,缺乏小提琴和对意图的松散解释,特别是关于 &lt;header&gt; 元素和 &lt;div id="header"&gt; 元素,有些令人困惑。在多次阅读问题后,我重新设计了一个集中尝试来解决我所理解的问题。
    • 感谢重访。查看代码后,我不得不承认这有点令人困惑。应用的类没有任何相应的 CSS,唯一似乎有效地做任何事情的方法是第一个 $('header').show()...
    【解决方案2】:

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

     <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();
               }
          }); 
       });
    

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

    【讨论】:

    • 这是编写我的 html 的更好方法,但它不能解决为什么我的下划线适用于第一个导航而不是第二个导航
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签