gpt4 book ai didi

javascript - 滚动时根据其他元素显示/隐藏(切换)固定元素?

转载 作者:行者123 更新时间:2023-12-03 01:44:05 25 4
gpt4 key购买 nike

有没有办法根据垂直滚动时传递的元素在显示和隐藏固定元素之间切换?

这是我想要实现的目标的视觉引用:

enter image description here

我更希望它能够检测页面上固定元素的位置,而不是窗口的滚动位置。

希望这张照片足以解释;否则,请检查下面的代码片段:

$(document).ready(function() {
var $window = $(window);
var div2 = $('#div2');
var div1 = $('#div1');
var div1_top = div1.offset().top;
var div1_height = div1.height();
var div1_bottom = div1_top + div1_height;
console.log(div1_bottom);
$window.on('scroll', function() {
var scrollTop = document.documentElement.scrollTop;
var viewport_height = $window.height();
var scrollTop_bottom = scrollTop + viewport_height;
div2.toggleClass('show', scrollTop > div1_top && (scrollTop + window.innerHeight) < div1_bottom);
});
});
body {
background: #ccffcc;
padding: 0;
margin: 0;
border: 0;
text-align: center;
}

#div1 {
background: #0099ff;
height: 1500px;
color: #fff;
}

#div2 {
width: 100px;
height: 100px;
text-align: center;
position: fixed;
margin: auto;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: #ffff00;
color: #000;
display: none;
}

#div2.show {
display: block;
}

#div3 {
height: 1500px;
color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<br>
<br>
<br>
<br>
<br> Scroll area before <b>div1</b> appears
<br>
<br>
<br>
<br>
<br>
<br>
<div id="div1">
<div id="div2">This is <b>div2</b></div>
This is <b>div1</b>
<br>
<i>(Toggle show/hide <b>div2</b> when the top of <b>div2</b> passes the top of <b>div1</b>)</i>
</div>
<div id="div3">
This is <b>div3</b>
<br>
<i>(Toggle show/hide <b>div2</b> when the bottom of <b>div2</b> reaches the top of <b>div3</b>)</i>
</div>

最佳答案

这是你想要的吗?

如果是,解决办法是考虑窗口顶部到#div2(中心部分)的距离

已编辑

添加评论提到的功能

$(document).ready(function() {
var $window = $(window);
var div2 = $('#div2');
var div1 = $('#div1');
$window.on('scroll', function() {
var scrollTop = document.documentElement.scrollTop;
var viewport_height = $window.height();
var scrollTop_bottom = scrollTop + viewport_height;
var window_top_to_div2 = ($window.height()-div2.height())/2;


var div1_top = div1.offset().top;
var div1_height = div1.height();
var div1_bottom = div1_top + div1_height;

div2.toggleClass('show', scrollTop >= (div1_top-window_top_to_div2) && (scrollTop + window.innerHeight) <= (div1_bottom+window_top_to_div2));
});
});
body {
background: #ccffcc;
padding: 0;
margin: 0;
border: 0;
text-align: center;
}

#div1 {
background: #0099ff;
height: 1500px;
color: #fff;
}

#div2 {
width: 100px;
height: 100px;
text-align: center;
position: fixed;
margin: auto;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: #ffff00;
color: #000;
display: none;
}

#div2.show {
display: block;
}

#div3 {
height: 1500px;
color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<br>
<br>
<br>
<br>
<br> Scroll area before <b>div1</b> appears
<br>
<br>
<br>
<br>
<br>
<br>
<div id="div1">
<div id="div2">This is <b>div2</b></div>
This is <b>div1</b>
<br>
<i>(Toggle show/hide <b>div2</b> when the top of <b>div2</b> passes the top of <b>div1</b>)</i>
</div>
<div id="div3">
This is <b>div3</b>
<br>
<i>(Toggle show/hide <b>div2</b> when the bottom of <b>div2</b> reaches the top of <b>div3</b>)</i>
</div>

关于javascript - 滚动时根据其他元素显示/隐藏(切换)固定元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50710816/

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