gpt4 book ai didi

javascript - 在
的末尾隐藏一个 div

转载 作者:行者123 更新时间:2023-12-04 08:32:30 25 4
gpt4 key购买 nike

我知道如何显示或隐藏 div。
当滚动到第 2 秒时;
'banner' div 必须在 sec2(节)的开头可见
并在 sec2 部分结束时隐藏。
但不知道如何检查(在这种情况下)'sec2' 是否结束。
这个一定要贴吧?在(粘性)菜单栏下。
这不是真正的源代码,而是相同的脚本和 css 设置。
因为真正的页面太大了,不能在这里发布。

$(window).scroll(function() {
var scrollTop = $(window).scrollTop();
var showbar = $('#sec2').height();
var showbar = showbar -25;

if ( scrollTop > showbar) {
$("#banner").fadeIn(700);
}else{
$("#banner").fadeOut(250);
}
});
html, body {
margin:0px;
height:100%;
width:100%;
padding:0;
scroll-behavior: smooth;
}

.container {
position: absolute;
width:100%;
min-height:100%;
height:100%;
text-align: center;
}

.wrapper{
position: relative;
width:100%;
max-width:1000px;
height:100%;
}

section {
display: block;
color:white;
height:100%;


}

#menubar{
position: -webkit-sticky;
position: sticky;
top:0px;
width:100%;
background-color:black;

height:25px;
text-align: center;
z-index:1000;

}

#menubar a {
margin:20px 20px 20px 20px;
text-decoration:none;
color:white;
font-weight:bold;
}

#menubar a:hover {
color:yellow;
}

#sec1 {
background-color:red;
}

#sec2 {
background-color:blue;
}

#sec3 {
background-color:green;
}

#banner {
display:none;
z-index:1000;
position: -webkit-sticky; /* Safari */
position: sticky;
top: 25;
height:35px;
color:black;
background-color:white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="wrapper">
<div id="menubar">
<a href="#sec1">Section 1</a>
<a href="#sec2">Section 2</a>
<a href="#sec3">Section 3</a>
</div>
<div id="banner">Show this div only in section 2</div>
<section id="sec1"> bla </section>
<section id="sec2"> bla bla </section>
<section id="sec3"> bla bla bla </section>
</div>
</div>
<script>
function scrolled(o){
//visible height + pixel scrolled = total height
if(o.offsetHeight + o.scrollTop == o.scrollHeight){
var x = document.getElementById("#banner");
x.style.display = "block";
} else {
x.style.display = "none";
}}
</script>

希望有人可以帮助我。

最佳答案

我稍微改变了 html 的结构,将部分标签包装在 div 中:

<div class="sections">
<section id="sec1"> bla </section>
<section id="sec2"> bla bla </section>
<section id="sec3"> bla bla bla </section>
</div>
这样做是为了使粘性规则适用于所有部分的高度。而且他还对css进行了调整。

$(window).scroll(function() {
var scrollTop = $(window).scrollTop();
var showbar = $('#sec2').height();
var showbar_out = $('#sec3').height();
var showbar = showbar /*-25*/;

if ( scrollTop > showbar && scrollTop < showbar_out) {
$("#banner").fadeIn(700);
}else{
$("#banner").fadeOut(250);
}


});
html, body {
margin:0px;
height:100%;
width:100%;
padding:0;
scroll-behavior: smooth;
}

.container {
position: absolute;
width:100%;
min-height:100%;
height:100%;
text-align: center;
}

.wrapper{
position: relative;
width:100%;
max-width:1000px;
/*height:100%;*/
}

.sections {
display: flex;
flex-direction: column;
}

section {
display: block;
color:white;
/*height:100%;*/
height: 100vh;
}

#menubar{
position: -webkit-sticky;
position: sticky;
top:0px;
width:100%;
background-color:black;

height:25px;
text-align: center;
z-index:1000;

}

#menubar a {
margin:20px 20px 20px 20px;
text-decoration:none;
color:white;
font-weight:bold;
}

#menubar a:hover {
color:yellow;
}

#sec1 {
background-color:red;
}

#sec2 {
background-color:blue;
}

#sec3 {
background-color:green;
}

#banner {
display:none;
z-index:1000;
position: -webkit-sticky; /* Safari */
position: sticky;
top: 25px;
height:35px;
color:black;
background-color:white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="wrapper">
<div id="menubar">
<a href="#sec1">Section 1</a>
<a href="#sec2">Section 2</a>
<a href="#sec3">Section 3</a>
</div>
<div id="banner">Show this div only in section 2</div>
<div class="sections">
<section id="sec1"> bla </section>
<section id="sec2"> bla bla </section>
<section style="height: 200vh" id="sec3"> bla bla bla </section>
</div>
</div>
</div>
<script>
/* function scrolled(o){
//visible height + pixel scrolled = total height
if(o.offsetHeight + o.scrollTop == o.scrollHeight){
var x = document.getElementById("#banner");
x.style.display = "block";
} else {
x.style.display = "none";
}}*/
</script>

关于javascript - 在 <section> 的末尾隐藏一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64960415/

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