gpt4 book ai didi

javascript - 当到达当前卡住的 header 时,用第二个 header 替换粘性 header

转载 作者:太空宇宙 更新时间:2023-11-03 20:16:55 28 4
gpt4 key购买 nike

我一直在阅读粘性标题,这是我目前所发现的。第一个粘性 header 效果很好,但是当它遇到第一个 header 时,我如何向上滚动第一个 header 并使第二个 header 卡住?

http://jsfiddle.net/

<style>
body {
margin: 0;
text-align: center;
font-family: sans-serif;
}
.fixed {
position: fixed;
top: 0;
}
.sticky {
width: 100%;
background: #F6D565;
padding: 25px 0;
text-transform: uppercase;
}
</style>

<p style="margin-bottom:100px;">Scroll this page.</p>
<div class="sticky"><h3>Super amazing header</h3></div>
<p style="margin-top:500px;">Still there?</p>
<p style="margin-top:500px;">Yep!</p>
<p style="margin-top:500px;">Scroll so hard!</p>
<div class="sticky"><h3>Super amazing 123</h3></div>
<p style="margin-top:500px;">Still there?</p>
<p style="margin-top:500px;">Yep!</p>
<p style="margin-top:500px;">Scroll so hard!</p>
<script>
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);
</script>

最佳答案

我相信以下内容将完全如您所愿...

当滚动过去时,#firstHeader 将绑定(bind)到顶部,然后当 #secondHeader#firstHheader 的底部发生碰撞时他们将加入,#firstHeader 将被推出屏幕,直到 #secondHeader 到达屏幕顶部并绑定(bind)到那里...

希望这是有道理的,如果没有,如果您实际看到它可能会更容易: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 添加到您的 header ,这将适用于任意数量的 header (1、5、100……您明白了)。

这是对上述 JS 的完全替代,每个新 header 都会在旧 header 绑定(bind)到顶部之前将其到屏幕之外。

请参阅此更新的 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);

关于javascript - 当到达当前卡住的 header 时,用第二个 header 替换粘性 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18938191/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com