答案 0 :(得分: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;
}
答案 1 :(得分: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>