2017-10-10 2 views
0

Ich habe eine feste Kopfzeile am oberen Rand der Seite, und nach dem Scrollen nach unten und zeigt einen Schieberegler, ich habe eine zweite Anker-Menü. Wie kann ich dieses Ankermenü klebrig zum Header machen. image shows effectZweites Menü sticky to header

+1

Können Sie jQuery verwenden? – will

+1

Haben Sie versucht, [header {position: sticky}] (https://developer.mozilla.org/en-US/docs/Web/CSS/position#CSS_4) zu verwenden? – styfle

+0

Ja, ich kann Jquery verwenden. Ich benutze nie Position sticky – rinkaslb

Antwort

2

Ich bin mir ziemlich sicher, dass das ist, was Sie suchen!

https://codepen.io/will0220/pen/QqreKY

Grundsätzlich müssen Sie die Mitte Inhalt Navigationsleiste die Position vom oberen Rand des Bildschirms prüfen, die durch die Überprüfung des Elements Top-Offset minus der Scroll-Fenster erfolgt. Wenn Sie nach oben oder unten scrollen, binden Sie sich ein. Wenn Sie also nach unten scrollen und das Mid-Page-Nav weniger als die Höhe Ihrer festen Kopfzeile vom oberen Bildschirmrand entfernt ist, klonen Sie es und fügen Sie eine feste Version hinzu. Klonen ist der Weg, also ändert sich die Länge des Inhalts nicht. Wenn wir dann in die entgegengesetzte Richtung scrollen, können wir einfach überprüfen, wo das ursprüngliche In-Page-Nav (das noch existiert), mit der gleichen Berechnung wie zuvor.

Das in der festen Navigationsleiste Schalt behandeln werden:

var thisScroll = 0, lastScroll = 0; 
$(window).scroll(function(){ 
    thisScroll = $(window).scrollTop(); 
    if($('nav').offset().top - thisScroll <= 50 && !$('#stuckNav').length && thisScroll > lastScroll){ 
    var newNav = $('nav').clone(); 
    newNav.attr('id', 'stuckNav'); 
    $('#wrapper').append(newNav); 
    } 
    else if($('nav').offset().top - $(window).scrollTop() > 50 && thisScroll < lastScroll){ 
    $('#stuckNav').remove(); 
    } 
    lastScroll = thisScroll; 
}); 

Und die CSS braucht etwas wie folgt aus:

#stuckNav{ 
    position: fixed; 
    top: 50px; 
    z-index: 2; 
} 
nav{ 
    position: relative; 
    width: 100%; 
    background: #FFF; 
    text-align: center; 
    padding: 10px 0; 
} 
+0

Das ist perfekt – rinkaslb

0

Mit nur javascript: https://codepen.io/claytron5000/pen/OxZKxB?editors=1010

var secondary = document.querySelector(".secondary"); 
 

 
var fromTop = secondary.offsetTop; 
 

 
document.onscroll = function() { 
 
    if (!secondary.classList.contains("stuck") && window.pageYOffset >= fromTop) { 
 
    secondary.classList.add("stuck"); 
 
    } 
 
    else if (secondary.classList.contains("stuck") && window.pageYOffset < fromTop){ 
 
    secondary.classList.remove("stuck"); 
 
    } 
 
};
.container p { 
 
    max-width: 768px; 
 
    margin: auto; 
 
} 
 
.header, .secondary { 
 
    background: beige; 
 
    position: relative; 
 
} 
 
ul li { 
 
    display: inline-block; 
 
    background: lightgrey; 
 
} 
 
.stuck { 
 
    position: fixed; 
 
    top:0; 
 
    width: 100%; 
 
}
<div id="container"> 
 
    <div class="header"> 
 
    <ul> 
 
     <li>here's</li> 
 
     <li>some</li> 
 
     <li>items</li> 
 
    </ul> 
 
    </div> 
 
    <p>Some content that will scroll away.</p> 
 
    <div class="secondary"> 
 
    <ul> 
 
     <li>here's</li> 
 
     <li>different</li> 
 
     <li>items</li> 
 
    </ul> 
 
    </div> 
 
    <p>And a whole lotta ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. 
 

 
    Why do we use it? 
 
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 
    Why do we use it? 
 
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 
    Why do we use it? 
 
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 
    Why do we use it? 
 
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 
    Why do we use it? 
 
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 
    Why do we use it? 
 
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 
 

 

 
    Where does it come from? 
 
    Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p> 
 
</div>