2012-12-26 63 views
0

我正在尋找一塊腳本,在我的頁面頂部隱藏了我的粘性導航。所以最後它應該在你進入網站時向下滾動。粘性導航隱藏在頁面頂部

這是我建立的網站:http://kmnew.kadushimarketing.com/index.php

這是我目前使用的腳本:

$(function() { 

    // grab the initial top offset of the navigation 
    var sticky_navigation_offset_top = $('#sticky_navigation').offset().top; 

    // our function that decides weather the navigation bar should have "fixed" css position or not. 
    var sticky_navigation = function(){ 
     var scroll_top = $(window).scrollTop(); // our current vertical position from the top 

     // if we've scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative 
     if (scroll_top > sticky_navigation_offset_top) { 
      $('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0 }); 
     } else { 
      $('#sticky_navigation').css({ 'position': 'relative' }); 
     } 
    }; 

    // run our function on load 
    sticky_navigation(); 

    // and run it again every time you scroll 
    $(window).scroll(function() { 
     sticky_navigation(); 
    }); 

}); 
+0

而不是使用'位置:相對'爲什麼你不把它藏起來? –

+0

它不應該一直隱藏。只在頁面的最頂端。 – Marcel

+0

隱藏它,並使用jquery創建一個滾動事件監聽器。您可以分析滾動位置並顯示/隱藏導航。 –

回答

0

我明白了。我將腳本更改爲:

$(document).ready(function(){ 

    // hide #sticky_navigation first 
    $("#sticky_navigation").hide(); 

    // fade in #sticky_navigation 
    $(function() { 
     $(window).scroll(function() { 
      if ($(this).scrollTop() > 100) { 
       $('#sticky_navigation').fadeIn(); 
      } else { 
       $('#sticky_navigation').fadeOut(); 
      } 
     }); 


    }); 

}); 

然後我還添加了位置:固定到我的css。

+0

但它可能無法正常工作......它在頁面加載時顯示得非常快,然後消失。有誰知道如何解決這一問題? – Marcel

0

我不知道如果這是你在找什麼。如果頁面大於窗口高度,請將其展示並放置在頂部

$(function() { 

    // grab the initial top offset of the navigation 
    var sticky_navigation_offset_top = $('#sticky_navigation').offset().top; 

    // our function that decides weather the navigation bar should have "fixed" css position or not. 
    var sticky_navigation = function(){ 
     var scroll_top = $(window).scrollTop(); // our current vertical position from the top 
     var windowHeight = $(window).height(); 

     // if we've scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative 
     if (scroll_top > windowHeight) { 
      $('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0, 'display': 'block' }); 
     } else { 
      $('#sticky_navigation').css({ 'position': 'relative', 'display': 'none' }); 
     } 
    }; 

    // run our function on load 
    sticky_navigation(); 

    // and run it again every time you scroll 
    $(window).scroll(function() { 
     sticky_navigation(); 
    }); 

}); 
+0

這是答案嗎? – Chanckjh

+0

不幸的是,它不會做任何不同於我擁有的代碼。 – Marcel

+0

那麼你想做什麼? – Chanckjh