我一直在阅读粘性标题,这是我目前所发现的。第一个粘性 header 效果很好,但是当它遇到第一个 header 时,我如何向上滚动第一个 header 并使第二个 header 卡住?
http://jsfiddle.net/
Scroll this page.
Super amazing header
Still there?
Yep!
Scroll so hard!
Super amazing 123
Still there?
Yep!
Scroll so hard!
var sticky = document.querySelector('.sticky');
var origOffsetY = sticky.offsetTop;
function onScroll(e) {
window.scrollY >= origOffsetY ? sticky.classList.add('fixed') :
sticky.classList.remove('fixed');
}
document.addEventListener('scroll', onScroll);
我相信以下内容将完全如您所愿...
当滚动过去时,#firstHeader
将绑定(bind)到顶部,然后当 #secondHeader
Và #firstHheader
的底部发生碰撞时他们将加入,#firstHeader
将被推出屏幕,直到 #secondHeader
到达屏幕顶部并绑定(bind)到那里...
希望这是有道理的,如果没有,如果您实际看到它可能会更容易:http://jsfiddle.net/yZKea/
此解决方案确实使用了 jquery
。因此,您需要在 head
中的其余 JS
代码之前包含对它的引用:
Để ý JS
并不像看起来那样令人生畏 hoặcdài,大部分内容是样式的重复设置。您可以轻松地将其更改为几行:)
CSS
html, nội dung {
margin: 0;
padding: 0;
đường viền: 0;
}
.spacer{
height:200px;
background:#f00;
opacity:.5;
border-top:#d22 5px solid;
width: 100%;
}
#firstHeader{
background:#00f;
chiều cao: 100px;
width: 100%;
z-index:500;
}
#secondHeader{
background:#0f0;
chiều cao: 100px;
width: 100%;
z-index:500;
}
HTML
Tôi là một lập trình viên xuất sắc, rất giỏi!