2017-04-07 28 views
0

我正在導航欄上工作。當我向下滾動時,我希望它變得更薄。我有代碼工作,但我無法得到應用的樣式..開發工具說,他們正在申請,但他們不是。所以對於jquery或者只是香草js,我怎麼能一次應用這些設置。這裏是如何 「應該」 工作的屏幕截圖:如何將這幾個CSS設置應用於jQuery?

由於某種原因,我不能在這裏張貼的CSS,所以在這裏is a link to codepen

$(document).ready(function() { 
 
    // all custom jQuery will go here 
 
    var nav = $(".main-nav"); 
 

 
    $(window).scroll(function(){ 
 
     if($(this).scrollTop() > 5){ 
 
      nav.addClass("main-nav-scrolled"); 
 
     }else{ 
 
      nav.removeClass("main-nav-scrolled"); 
 
     } 
 
    }); 
 
});
.fixed-nav { 
 
    position: fixed; 
 
    top: 0; 
 
    left: 0; 
 
    right: 0; 
 
    z-index: 9999; 
 
    width: 100%; 
 
    height: 110px; 
 
    background-color: #fff; 
 
    border-bottom: solid 1px #f2f2f2; } 
 
    .fixed-nav li, 
 
    .fixed-nav a { 
 
    height: 110px; 
 
    line-height: 110px; } 
 

 
.fixed-nav { 
 
    height: 50px; } 
 
    .fixed-nav li, 
 
    .fixed-nav a { 
 
    height: 50px; 
 
    line-height: 50px; } 
 

 
.menu { 
 
    width: 90%; 
 
    max-width: 980px; 
 
    margin: 0 auto; } 
 

 
.menu a { 
 
    color: #333; 
 
    text-transform: uppercase; } 
 
    .menu a:visited { 
 
    color: #333; } 
 
    .menu a:hover, .menu a:target { 
 
    color: #ED1C24; 
 
    transition: color 0.4s; } 
 

 
.menu-items { 
 
    display: inline-block; } 
 

 
.sitename { 
 
    display: inline-block; 
 
    margin-right: 30px; } 
 

 
a.sitename, a:visited.sitename { 
 
    color: #e0e0e0; } 
 

 
.menu-items li { 
 
    display: inline-block; 
 
    margin-right: 10px; 
 
    margin-left: 10px; } 
 
.menu-items a { 
 
    text-decoration: none; } 
 

 
.show, .hide { 
 
    display: none; 
 
    padding-left: 15px; 
 
    background-color: transparent; 
 
    background-repeat: no-repeat; 
 
    background-position: center left; 
 
    color: #dde1e2; } 
 

 
.show { 
 
    background-image: url(assets/down-arrow-icon.png); } 
 

 
.hide { 
 
    background-image: url(assets/up-arrow-icon.png); } 
 

 
@media only screen and (max-width: 800px) { 
 
    .menu { 
 
    position: relative; 
 
    width: 100%; } 
 

 
    .sitename { 
 
    position: absolute; 
 
    top: 0; 
 
    left: 15px; 
 
    margin-left: 0px; } 
 

 
    .menu-items { 
 
    display: none; 
 
    width: 100%; 
 
    margin-top: 50px; 
 
    background-color: #fff; } 
 
    .menu-items li { 
 
     display: block; 
 
     text-align: center; } 
 

 
    .show, .hide { 
 
    position: absolute; 
 
    top: 0; 
 
    right: 15px; } 
 

 
    .show { 
 
    display: inline-block; } 
 

 
    .hide { 
 
    display: none; } 
 

 
    #menu:target .show { 
 
    display: none; } 
 
    #menu:target .hide, #menu:target .menu-items { 
 
    display: inline-block; } } 
 
@media only screen and (max-width: 220px) { 
 
    .sitename, .show, .hide { 
 
    font-size: 14px; } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<header> 
 
    <nav> 
 
    <div class="container"> 
 
     <div class="row"> 
 
     <div class="twelve columns main-nav fixed-nav"> 
 
      <div class="menu" id="menu"><a class="sitename" href="#">Brand-Logo</a> <a class="show" href="#menu">Menu</a><a class="hide" href="#hidemenu">Menu</a> 
 
      <ul class="menu-items"> 
 
       <li><a href="#">Home</a></li> 
 
       <li><a href="#">About</a></li> 
 
       <li><a href="#">Services</a></li> 
 
       <li><a href="#">Work</a></li> 
 
       <li><a href="#">Blog</a></li> 
 
       <li><a href="#">Empty</a></li> 
 
       <li><a href="#">Contact Us</a></li> 
 
      </ul> 
 
      </div> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </nav> 
 
</header>

+1

您沒有在任何位置定義樣式'main-nav-scrolled'。 –

回答

2

您需要首先定義.main-nav-scrolled類的屬性。事情是這樣的:

$(document).ready(function() { 
 
    // all custom jQuery will go here 
 
    var nav = $(".main-nav"); 
 

 
    $(window).scroll(function() { 
 
    if ($(this).scrollTop() > 5) { 
 
     nav.addClass("main-nav-scrolled"); 
 
    } else { 
 
     nav.removeClass("main-nav-scrolled"); 
 
    } 
 
    }); 
 
});
.fixed-nav { 
 
    position: fixed; 
 
    top: 0; 
 
    left: 0; 
 
    right: 0; 
 
    z-index: 9999; 
 
    width: 100%; 
 
    height: 110px; 
 
    background-color: #fff; 
 
    border-bottom: solid 1px #f2f2f2; 
 
} 
 

 
.fixed-nav li, 
 
.fixed-nav a { 
 
    height: 110px; 
 
    line-height: 110px; 
 
} 
 

 
.fixed-nav { 
 
    height: 70px; 
 
} 
 

 
.fixed-nav li, 
 
.fixed-nav a { 
 
    height: 50px; 
 
    line-height: 50px; 
 
} 
 

 
.menu { 
 
    width: 90%; 
 
    max-width: 980px; 
 
    margin: 0 auto; 
 
} 
 

 
.menu a { 
 
    color: #333; 
 
    text-transform: uppercase; 
 
} 
 

 
.menu a:visited { 
 
    color: #333; 
 
} 
 

 
.menu a:hover, 
 
.menu a:target { 
 
    color: #ED1C24; 
 
    transition: color 0.4s; 
 
} 
 

 
.menu-items { 
 
    display: inline-block; 
 
} 
 

 
.sitename { 
 
    display: inline-block; 
 
    margin-right: 30px; 
 
} 
 

 
a.sitename, 
 
a:visited.sitename { 
 
    color: #e0e0e0; 
 
} 
 

 
.menu-items li { 
 
    display: inline-block; 
 
    margin-right: 10px; 
 
    margin-left: 10px; 
 
} 
 

 
.menu-items a { 
 
    text-decoration: none; 
 
} 
 

 
.show, 
 
.hide { 
 
    display: none; 
 
    padding-left: 15px; 
 
    background-color: transparent; 
 
    background-repeat: no-repeat; 
 
    background-position: center left; 
 
    color: #dde1e2; 
 
} 
 

 
.show { 
 
    background-image: url(assets/down-arrow-icon.png); 
 
} 
 

 
.hide { 
 
    background-image: url(assets/up-arrow-icon.png); 
 
} 
 

 
.main-nav-scrolled { 
 
    height: 45px; 
 
} 
 

 
.main-nav-scrolled .menu-items { 
 
    margin-top: 0px; 
 
    margin-bottom: 4px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<header> 
 
    <nav> 
 
    <div class="container"> 
 
     <div class="row"> 
 
     <div class="twelve columns main-nav fixed-nav"> 
 
      <div class="menu" id="menu"><a class="sitename" href="#">Brand-Logo</a> <a class="show" href="#menu">Menu</a><a class="hide" href="#hidemenu">Menu</a> 
 
      <ul class="menu-items"> 
 
       <li><a href="#">Home</a></li> 
 
       <li><a href="#">About</a></li> 
 
       <li><a href="#">Services</a></li> 
 
      </ul> 
 
      </div> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </nav> 
 
</header> 
 
SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes 
 
here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff 
 
Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br>SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes 
 
here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br> SomeStuff Goes here<br>

+0

是的我知道我需要爲.fixed-nav定義屬性。問題是我還必須將它們應用於li和它下面的元素。如果我這樣做與.fixed-nav相同,它不起作用。但是,如果我將它們硬編碼到設置,它就會。我無法弄清楚爲什麼。 –

+0

這就是發生了什麼.... http://pasteboard.co/9CuZJ9hN.png –

+0

@JoshBeyer ofcourse你必須申請'li'和'a'元素...看到這個片段,我已經使用過'.main -nav-scrlloed'以及'.main-nav-scrlloed .menu-items' –

0

您可以使用jQuery的.scrollTop()方法。基本上,你有兩個CSS文件,將取決於用戶是否滾動到某個位置或不適用。下面是一個例子:

$(function() { 
     $(window).scroll(function() { 
      if ($(window).scrollTop() >= 100) { 
       $(".header").addClass("smaller"); 
      } else { 
       $(".header").removeClass("smaller"); 
      } 
     }); 
    }); 

該代碼將上述較小的類添加到報頭類每當用戶滾動大於或等於100個像素,否則它會被移除。然後,只要應用較小的課程,您就可以更改高度或隱藏一些對象。你只需要知道基本的CSS。