2017-10-10 61 views
0

我有在页面的顶部的固定报头,和向下滚动,并示出了滑块之后,我有一个次级锚菜单。我怎样才能使这个锚菜单粘到头上。 image shows effect二级菜单粘到标题

+1

你可以使用jQuery? – will

+1

您是否尝试过使用[header {position:sticky}](https://developer.mozilla.org/en-US/docs/Web/CSS/position#CSS_4)? – styfle

+0

是的,我可以使用jQuery。我从来没有使用位置粘 – rinkaslb

回答

2

我敢肯定这是你在找什么!

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

基本上,你需要从屏幕上,这是通过检查元素上偏移减去窗口的滚动做的顶部检查中间内容导航栏的位置。然后,如果您向上或向下滚动,则系统会自动关闭 - 因此,如果您向下滚动,并且中间页面导航小于屏幕顶部固定标题的高度,请将其克隆并添加固定版本。克隆是要走的路,所以内容的长度不会改变。然后当我们向相反的方向滚动时,我们可以使用与之前相同的计算来检查原始页内导航(仍然存在)的位置。

这会处理在固定的导航栏切换:

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; 
}); 

而CSS需要的东西是这样的:

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

这是完美的 – rinkaslb

0

只需使用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>