2016-03-08 49 views
0

我發現此代碼。對div的固定導航效果

$(document).ready(function(){ 
 
\t $(window).bind('scroll', function() { 
 
\t var navHeight = $(window).height() - 70; 
 
\t \t \t if ($(window).scrollTop() > navHeight) { 
 
\t \t \t \t $('nav').addClass('fixed'); 
 
\t \t \t } 
 
\t \t \t else { 
 
\t \t \t \t $('nav').removeClass('fixed'); 
 
\t \t \t } 
 
\t \t }); 
 
\t });
/* 
 
Tutorial Name: Scroll To Top Then Fixed Navigation Effect With JQuery and CSS 
 
Description: Create a sticky navigation bar that remains fixed to the top after scroll 
 
Author: Stanislav Kostadinov 
 
Author URI: http://stanislav.it 
 
Version: 1.0.0 - 11.01.2014 
 
*/ 
 

 
* {margin: 0; padding: 0;} 
 

 
a {text-decoration: none;} 
 

 
/* This class is added on scroll */ 
 
.fixed { 
 
\t position: fixed; 
 
\t top: 0; 
 
\t height: 70px; 
 
\t z-index: 1; 
 
} 
 

 
body { 
 
\t color: #fff; 
 
\t font-family: 'open-sans-bold'; 
 
\t font-size: 18px; 
 
\t text-align: center; 
 
} 
 

 
/* Font Face Settings */ 
 
@font-face { 
 
    font-family: 'open-sans-bold'; 
 
\t src: url('../fonts/open-sans/OpenSans-Bold.eot'); 
 
    src: url('../fonts/open-sans/OpenSans-Bold.eot?iefix') format('embedded-opentype'), 
 
\t \t url('../fonts/open-sans/OpenSans-Bold.ttf'); 
 
    font-weight: normal; 
 
} \t 
 

 
/* Navigation Settings */ 
 
nav { 
 
\t position: absolute; 
 
\t bottom: 0; 
 
\t width: 100%; 
 
\t height: 70px; 
 
\t background: #fff; 
 
} 
 

 
nav li { 
 
\t display: inline-block; 
 
\t padding: 24px 10px; 
 
} 
 

 
nav li a { 
 
\t color: #757575; 
 
\t text-transform: uppercase; 
 
} 
 

 
section { 
 
\t height: 100vh; 
 
} 
 

 
/* Screens Settings */ 
 
#screen1 { \t 
 
\t background: #43b29d; 
 
} 
 

 
#screen1 p { 
 
\t padding-top: 200px; 
 
} 
 

 
#screen2 { 
 
\t background: #efc94d; 
 
} 
 

 
#screen3 { 
 
\t background: #e1793d; 
 
} 
 

 
@media only screen and (max-width: 520px) { 
 

 
\t nav li { 
 
\t \t padding: 24px 4px; 
 
\t } 
 

 
\t nav li a { 
 
\t \t font-size: 14px; 
 
\t } 
 

 
}
<section id="screen1"> 
 

 
\t <p>Scroll down</p> 
 

 
\t <nav> 
 
\t \t <ul> 
 
\t \t \t <li><a href="#">Home</a></li> 
 
\t \t \t <li><a href="#">About</a></li> 
 
\t \t \t <li><a href="#">Services</a></li> 
 
\t \t \t <li><a href="#">Team</a></li> 
 
\t \t \t <li><a href="#">Contact</a></li> 
 
\t \t </ul> 
 
\t </nav> 
 

 
</section> 
 
\t 
 
<section id="screen2"></section> 
 
<section id="screen3"></section>

這裏是一個工作實施例。 http://stanhub.com/tutorials/sticky-navigation/

它的粘性菜單和很好的作品,但問題是它只是粘着然後頁面全部向下滾動。是否有可能修復後導航div 40px向下滾動?可以說導航盒和頂級瀏覽器之間有40個像素。

+0

多高是你的導航欄?此外,你會發布你的代碼,以便更容易推斷出你的具體問題是什麼? –

+0

你好,我的導航欄是40px,我的導航欄和頂級瀏覽器之間的空間也是40px。所以上面的項目不應該固定到瀏覽器(底部),而是40px。我的源代碼是在本地運行的,所以它不可能。 – Peter

回答

0

是的,更改代碼中的變量從 var navHeight = $(window).height() - 70;

到 var navHeight = $(window).height() - 40;

應當與指定高度

0

沒有你可以發佈您的代碼,這個解決您的問題是我能做到根據您的描述最好的。

改變你的Javascript這樣的:

$(document).ready(function(){ 
    $(window).bind('scroll', function() { 
     var navHeight = 40; //Match the distance of your nav bar from the top of the window 
     if ($(window).scrollTop() > navHeight) { 
      $('nav').addClass('fixed'); 
     } 
     else { 
      $('nav').removeClass('fixed'); 
     } 
    }); 
}); 
+0

再次嗨。所以我終於上傳了它。這裏是我的代碼http://quranen.dk/index.php/1#start-0(你必須儘量減少瀏覽器到平板電腦/手機的大小以查看效果。從內容跳到最上面(我不知道爲什麼)。順便說一句,我改變導航到.nav4(只是你知道)。你可以請求看看嗎? – Peter

+0

內容跳轉的原因是因爲當nav被設置爲'position:fixed'時它會將它從文檔流中提取出來,所以導航的高度不再將內容推下來---我的答案是否解決了原始問題? –

+0

hello @Diddle Dot。它不能正常工作。然後向下滾動,菜單隱藏了2-3秒,然後顯示出來,你知道爲什麼嗎?或者你有什麼想法爲什麼這樣做? – Peter