gpt4 book ai didi

javascript - 垂直滚动某些部分进入 View 时如何隐藏/显示固定元素?

转载 作者:太空宇宙 更新时间:2023-11-04 06:13:54 29 4
gpt4 key购买 nike

我想要一个固定元素 #logoimode3 在某些部分垂直滚动进入 View 时隐藏/显示。

例如:
#section1#section3 滚动到 View 中时显示 #logoimode3
当#section2 滚动到 View 中时隐藏 #logoimode3

所以当蓝色部分滚动到 View 中时,固定元素应该消失。然后当绿色部分滚动到 View 中时再次显示。我希望我的 Logo 在滚动 #section2 时消失。

我做错了什么?

这是我的 HTML 代码:

    <img id="logoimode3" class="logo3" style="position:fixed;top:0px;left:0px;" src="https://imode.info/imode/slike/ikone/IMODE_znak-01.svg" alt="logo" height="" width="30px">
<section id="section1" style="background: red; height:100vh;"></section>
<section id="section2" style="background: blue; height:100vh;"></section>
<section id="section3" style="background: green; height:100vh;"></section>
<footer></footer>

这是我的 JavaScript 代码:

    jQuery(document).ready(function($) {

$('#logoimode4').css({
'display': 'none'
});

$(function() {
var $window = $(window);
var logo = $('#logoimode4');
var div1 = $('#section1stran');
var div2 = $('#section2stran');
var div3 = $('#section3stran');
var div4 = $('#section4stran');
var div5b = $('#section5bstran');

var div1_height = div1.height();
var div2_height = div2.height();
var div3_height = div3.height();
var div4_height = div4.height();
var div5b_height = div5b.height();

$window.on('scroll', function() {
var scrollTop = document.documentElement.scrollTop;
var viewport_height = $window.height();
var scrollTop_bottom = scrollTop + viewport_height;

// if (scrollTop >= 0 && (scrollTop_bottom <= div1_height * 1.9 )) {
// logo.css({'display' : 'none'});
// }

if (scrollTop >= div1_height * 0 + div1_height * 1 + div2_height + div3_height + div4_height + div5b_height && (scrollTop_bottom <= div1_height * 5 + div1_height + div2_height + div3_height + div4_height + div5b_height)) {
logo.css({
'display': 'block'
});
} else {
logo.css({
'display': 'none'
});
}

// Work with Class
/* if (scrollTop > div1_height && (scrollTop_bottom <= div1_height * 3)) {
logo.addClass('hidden');
} else {
logo.removeClass('hidden');
} */
});
});
});

最佳答案

获取部分的高度,当部分的顶部达到 0 时它隐藏,当部分的底部达到 0 时它再次显示

<!DOCTYPE html>

<html>

<head>
<style></style>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
jQuery(document).ready(function() {
var sec2 = document.getElementById("section2");
var pos = sec2.getBoundingClientRect();
var height1 = pos.height * -1;
if (pos.top < 1 && pos.top > height1) {
jQuery('#logoimode3').hide();
}
else if(pos.top < height1 || pos.top > 1) {
jQuery('#logoimode3').show();
}
});
jQuery(window).scroll(function() {
var sec2 = document.getElementById("section2");
var pos = sec2.getBoundingClientRect();
var height1 = pos.height * -1;
if (pos.top < 1 && pos.top > height1) {
jQuery('#logoimode3').hide();
}
else if(pos.top < height1 || pos.top > 1) {
jQuery('#logoimode3').show();
}
});
</script>

</head>

<body>
<img id="logoimode3" class="logo3" style="position:fixed;top:0px;left:0px;" src="https://imode.info/imode/slike/ikone/IMODE_znak-01.svg" alt="logo" height="" width="30px">

<section id="section1" style="background: red; height:100vh;"></section>
<section id="section2" style="background: blue; height:100vh;"></section>
<section id="section3" style="background: green; height:100vh;"></section>
<section id="section4" style="background: pink; height:100vh;"></section>

</body>

<footer></footer>

</html>

关于javascript - 垂直滚动某些部分进入 View 时如何隐藏/显示固定元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56168633/

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