我相信以下内容会完全按照您的意愿进行...
滚动过去时将#firstHeader
绑定到顶部,然后当#secondHeader
与底部碰撞时#firstHheader
它们将加入并且#firstHeader
将被推离屏幕直到#secondHeader
到达屏幕顶部并绑定在那里......
希望这是有道理的,如果没有,如果你看到它可能会更容易:http: //jsfiddle.net/yZKea/
该解决方案确实使用jquery
. 因此,您需要head
在其余JS
代码之前包含对它的引用:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
注意:这JS
并不像看起来那么令人生畏或只要它看起来,大部分都是重复设置样式。您可以轻松地将其更改为几行 :)
CSS
html, body {
margin: 0;
padding: 0;
border: 0;
}
.spacer{
height:200px;
background:#f00;
opacity:.5;
border-top:#d22 5px solid;
width: 100%;
}
#firstHeader{
background:#00f;
height: 100px;
width: 100%;
z-index:500;
}
#secondHeader{
background:#0f0;
height: 100px;
width: 100%;
z-index:500;
}
HTML
<body>
<div class="spacer"></div>
<div id="firstHeader">Header 1</div>
<div class="spacer"></div>
<div class="spacer"></div>
<div class="spacer"></div>
<div id="secondHeader">Header 2</div>
<div class="spacer"></div>
<div class="spacer"></div>
<div class="spacer"></div>
<div class="spacer"></div>
<div class="spacer"></div>
<div class="spacer"></div>
<div class="spacer"></div>
<div class="spacer"></div>
<div class="spacer"></div>
<div class="spacer"></div>
<div class="spacer"></div>
<div class="spacer"></div>
<div class="spacer"></div>
<div class="spacer"></div>
<div class="spacer"></div>
</body>
JS
function scrollFunction(){
var sticky1 = $('#firstHeader');
var sticky2 = $('#secondHeader');
sticky1.css({
position: "static",
top: 0
});
sticky2.css({
position: "static",
top: 0
});
$('body').css({
"padding-top": 0
});
var topOffset1 = sticky1.offset().top;
var topOffset2 = sticky2.offset().top;
var stickyHeight1 = sticky1.outerHeight();
var stickyHeight2 = sticky2.outerHeight();
var scrollHeight = $(window).scrollTop();
if(topOffset1 <= scrollHeight && scrollHeight < topOffset2 - stickyHeight1){
sticky1.css({
position: "fixed",
top: 0
});
sticky2.css({
position: "static",
top: 0
});
$('body').css({
"padding-top": stickyHeight1
});
}
else if(scrollHeight >= topOffset2 - stickyHeight1 && scrollHeight < topOffset2){
sticky1.css({
position: "fixed",
top: - (scrollHeight - (topOffset2 - stickyHeight1))
});
sticky2.css({
position: "fixed",
top: stickyHeight1 - (scrollHeight - (topOffset2 - stickyHeight1))
});
$('body').css({
"padding-top": stickyHeight1 + stickyHeight2
});
}
else if(scrollHeight >= topOffset2){
sticky1.css({
position: "static"
});
sticky2.css({
position: "fixed",
top: 0
});
$('body').css({
"padding-top": stickyHeight2
});
}
else{
sticky1.css({
position: "static",
top: 0
});
sticky2.css({
position: "static",
top: 0
});
$('body').css({
"padding-top": 0
});
}
}
$(window).scroll(scrollFunction);
更新了 JS
这应该可以解决屏幕闪烁的问题。
var topOffset1, topOffset2;
function scrollFunction(){
var sticky1 = $('#firstHeader');
var sticky2 = $('#secondHeader');
var stickyHeight1 = sticky1.outerHeight();
var stickyHeight2 = sticky2.outerHeight();
var scrollHeight = $(window).scrollTop();
if(topOffset1 <= scrollHeight && scrollHeight < topOffset2 - stickyHeight1){
sticky1.css({
position: "fixed",
top: 0
});
sticky2.css({
position: "static",
top: 0
});
$('body').css({
"padding-top": stickyHeight1
});
}
else if(scrollHeight >= topOffset2 - stickyHeight1 && scrollHeight < topOffset2){
sticky1.css({
position: "fixed",
top: - (scrollHeight - (topOffset2 - stickyHeight1))
});
sticky2.css({
position: "fixed",
top: stickyHeight1 - (scrollHeight - (topOffset2 - stickyHeight1))
});
$('body').css({
"padding-top": stickyHeight1 + stickyHeight2
});
}
else if(scrollHeight >= topOffset2){
sticky1.css({
position: "static"
});
sticky2.css({
position: "fixed",
top: 0
});
$('body').css({
"padding-top": stickyHeight2
});
}
else{
sticky1.css({
position: "static",
top: 0
});
sticky2.css({
position: "static",
top: 0
});
$('body').css({
"padding-top": 0
});
}
}
$(function(){
topOffset1 = $('#firstHeader').offset().top;
topOffset2 = $('#secondHeader').offset().top;
});
$(window).scroll(scrollFunction);
JS 对于可变数量的标题
只需将类添加.header
到您的标题中,这将适用于任意数量的标题(1、5、100...您明白了)。
这是对上述 JS 的完全替代,每个新标题都会在旧标题绑定到顶部之前将其推离屏幕。
请参阅此更新jsfiddle
以获取其实际操作示例:http: //jsfiddle.net/5bkst/
var topOffset = new Array();
function scrollFunction(){
var scrollHeight = $(window).scrollTop();
var headerCounter = 0;
var scrolled = 0;
var headerItems = $('.header').length;
$('.header').each(function(index, el){
var elementHeight = $(this).outerHeight();
var nextElementHeight = 0;
var nextElement;
if(index !== $('.header').length - 1){
nextElementHeight = $('.header').eq(index + 1).outerHeight();
nextElement = $('.header').eq(index + 1);
}
if(scrollHeight >= topOffset[headerCounter]
&& (scrollHeight < topOffset[headerCounter + 1] || headerCounter == headerItems-1)){
scrolled = 1;
if(scrollHeight >= topOffset[headerCounter + 1] - elementHeight){
$(this).css({
position: "fixed",
top: - (scrollHeight - (topOffset[headerCounter + 1] - elementHeight))
});
nextElement.css({
position: "fixed",
top: topOffset[headerCounter + 1] - scrollHeight
});
$('body').css({
"padding-top": elementHeight + nextElementHeight
});
return false;
}
else{
$(this).css({
position: "fixed",
top: 0
});
nextElement.css({
position: "static",
});
$('body').css({
"padding-top": elementHeight
});
}
}
else{
$(this).css({
position: "static"
});
}
headerCounter++;
});
if(scrolled == 0){
$('body').css({
"padding-top": 0
});
}
}
$(function(){
$('.header').each(function(){
topOffset.push($(this).offset().top);
});
});
$(window).scroll(scrollFunction);