2016-06-16 33 views
3

我有兩個導航欄。讓我們說一個頂部的用戶導航欄和用戶導航欄下的主要導航欄。製作一個導航欄用戶滾動時粘住

我想在用戶滾動時有一個粘滯的主導航欄。

究竟即時試圖做的是使導航欄相似,我最喜歡的遊戲網站標題,這裏https://playoverwatch.com/en-us/ 見當您滾動和頂部第二個堅持是如何在第一導航欄很好消失?我真的想要有類似的東西,或者完全像這樣。

#ipsLayout_header header { 
 
    margin-bottom: 15px; 
 
    background-color: rgba(2,25,72,.3); 
 
} 
 
.ipsLayout_container { 
 
    max-width: 1340px; 
 
    padding: 0 15px; 
 
} 
 
.ipsNavBar_primary { 
 
    background: #304d66; 
 
}
<div id="ipsLayout_header"> 
 
    <header> 
 
     <div class="ipsLayout_container"><!-- my first navbar --> 
 
      <ul id="elUserNav"> 
 
       <li id="cCreate"> 
 
\t \t \t \t links 
 
       </li> 
 
      </ul> 
 
     </div> 
 
    </header> \t 
 
    <nav class="ipsLayout_container"><!-- my second navbar --> 
 
     <div class="ipsNavBar_primary"> 
 
      <ul class="ipsResponsive_block"> 
 
       <li id="elNavSecondary_34"> 
 
       links 
 
       </li> \t 
 
      </ul> 
 
     </div> 
 
    </nav> 
 
</div>

回答

2

這裏是代碼。使標題div固定在頂部並在滾動時給頂部減去上部菜單高度。

CSS -

#ipsLayout_header{ 
    position: fixed; 
    top: 0; 
    left: 0; 
    width: 100%; 
    display: block; 
    z-index: 10; 
    background: #fff; 
    -webkit-transition: .3s all 0s ease; 
    -moz-transition: .3s all 0s ease; 
    transition: .3s all 0s ease; 
} 

js-

$(window).scroll(function() { 
    var scroll = $(window).scrollTop(); 
    var headerHeight = $('header').outerHeight(); 
    if (scroll >= 100) { 
    $("#ipsLayout_header").css('top', -headerHeight); 
    } 
    else { 
    $("#ipsLayout_header").css('top', '0'); 
    } 
}); 

的jsfiddle - https://jsfiddle.net/dhananjaymane11/wvykLqxb/1/

+0

感謝一羣兄弟,作品完美。但我還有一個問題。我試圖使它類似於這個https://playoverwatch.com/en-us/標題,當你向下滾動頁面,你可以看到第二個導航欄擴展到100%。我試圖編輯javascript以添加'if(scroll> = 100){(「。ipsLayout_container」).css('max-width','100%'); }'它的作品只有一次,所以當你向下滾動它時,但是當滾動到頂部時,第二個導航欄仍然擴展到100%,因爲我是一個JavaScript的新手,我不知道如何做這個工作。任何想法? – Nippledisaster

+1

我已更新jsfiddle - https://jsfiddle.net/dhananjaymane11/wvykLqxb/2/希望它會幫助你。 – DJAy

+0

非常感謝您的幫助,作品完美:P – Nippledisaster

1

嘗試......

$(document).ready(function(){ 
 
\t $(window).scroll(function(){ 
 
    \t if ($(document).scrollTop()>$('nav').offset().top) 
 
    \t $('nav').addClass('onTop'); 
 
    else 
 
    \t $('nav').removeClass('onTop'); 
 
    }) 
 
})
#ipsLayout_header{ 
 
    width:100%; 
 
    height:800px; 
 
    background:#feacbe; 
 
    padding:0px; 
 
    margin:0px; 
 
    } 
 
    #ipsLayout_header header { 
 
     margin-bottom: 15px; 
 
     height: 44px; 
 
     background-color: rgba(2,25,72,.3); 
 
    } 
 
    .ipsLayout_container { 
 
     max-width: 1340px; 
 
     padding: 0 15px; 
 
    } 
 
    .ipsNavBar_primary { 
 
     width:100%; 
 
     height:40px; 
 
     background: #304d66; 
 
    } 
 
    .onTop{ 
 
     position:fixed; 
 
     z-index:9999; 
 
     width:92%; 
 
     top:0; 
 
    } 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
 
<div id="ipsLayout_header"> 
 

 
\t <header> 
 
\t \t <div class="ipsLayout_container"><!-- my first navbar --> 
 
\t   <ul id="elUserNav"> 
 
\t \t \t  <li id="cCreate"> 
 
\t \t \t \t links 
 
\t \t \t  </li> 
 
\t   </ul> 
 
\t  </div> 
 
\t </header> 
 
\t 
 
\t <nav class="ipsLayout_container"><!-- my second navbar --> 
 
\t \t <div class="ipsNavBar_primary"> 
 
\t \t \t <ul class="ipsResponsive_block"> 
 
\t \t   <li id="elNavSecondary_34"> 
 
\t \t \t \t links 
 
\t \t   </li> \t 
 
\t \t \t </ul> 
 
\t \t </div> 
 
\t </nav> 
 

 
</div>

的$(document).scrollTop()是用來獲取當前的滾動功能窗口的位置,基於你可以改變CSS類。

+0

感謝您對您的H埃爾普:) – Nippledisaster

0
<h2>First header</h2> 
<header><h1>Sticky Header</h1></header> 
<img src="large-image.jpg" width="782" height="2000" alt="Big Image" /> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> 
<script type="text/javascript"> 
    $(window).scroll(function() { 
if ($(this).scrollTop() > 1){ 
    $('header').addClass("sticky"); 
    } 
    else{ 
    $('header').removeClass("sticky"); 
    } 
}); 
</script> 
<style type="text/css"> 

    header{ 
    width: 100%; 
    text-align: center; 
    font-size: 72px; 
    line-height: 108px; 
    height: 108px; 
    background: #335C7D; 
    color: #fff; 
    font-family: 'PT Sans', sans-serif; 
} 

.sticky { 
    position: fixed; 
    top: 0; 
} 
</style> 

https://jsfiddle.net/mv0h6r1t/

1

$(function() { 
 
    var win = $(window); 
 
    var header = $('#ipsLayout_header'); 
 
    var height = $('header').outerHeight(true); 
 
    
 
    win.on('load scroll', function() { 
 
    if(win.scrollTop() > height) { 
 
     header.addClass('sticky'); 
 
     header.css({ 
 
     marginTop: -height 
 
     }); 
 
    } else { 
 
     header.removeClass('sticky'); 
 
     header.css({ 
 
     marginTop: 0 
 
     }); 
 
    } 
 
    }); 
 
});
body { 
 
    margin: 0; 
 
} 
 

 
#ipsLayout_header { 
 
    transition: margin 0.25s ease; 
 
    overflow: hidden; 
 
    position: fixed; 
 
    z-index: 100; 
 
    right: 0; 
 
    left: 0; 
 
    top: 0; 
 
} 
 
#ipsLayout_header header { 
 
    background-color: rgba(2,25,72,.3); 
 
    transform: translateY(0); 
 
    margin-bottom: 15px; 
 
} 
 
.ipsLayout_container { 
 
    position: relative; 
 
    max-width: 1340px; 
 
    padding: 0 15px; 
 
} 
 
.ipsNavBar_primary { 
 
    background: #304d66; 
 
} 
 
.page-content { 
 
    height: 1000px; 
 
} 
 
.ipsResponsive_block, 
 
#elUserNav {margin: 0;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="ipsLayout_header"> 
 

 
    <header> 
 
    <div class="ipsLayout_container"><!-- my first navbar --> 
 
     <ul id="elUserNav"> 
 
     <li id="cCreate"> 
 
      links 
 
     </li> 
 
     </ul> 
 
    </div> 
 
    </header> 
 

 
    <nav class="ipsLayout_container"><!-- my second navbar --> 
 
    <div class="ipsNavBar_primary"> 
 
     <ul class="ipsResponsive_block"> 
 
     <li id="elNavSecondary_34"> 
 
      links 
 
     </li> \t 
 
     </ul> 
 
    </div> 
 
    </nav> 
 

 
</div> 
 
<div class="page-content"> 
 
    
 
</div>

+0

謝謝您的幫助:) – Nippledisaster

+0

@ŢamanŞheƦzad歡迎您) –