gpt4 book ai didi

html - html-css 中多个粘性元素的问题

转载 作者:行者123 更新时间:2023-12-04 01:32:53 26 4
gpt4 key购买 nike

我有类“信息”的 div,如 <div class="info"><span>March 1 2020</span></div>它的 CSS 是 -

.info{
position:sticky;
top:0;
}

但是所有这些 div 在滚动时都是重叠的。
如何解决重叠问题?
我的意思是,如果新的粘性元素撞击前一个粘性元素,那么前一个粘性元素应该像 https://developer.mozilla.org/en-US/docs/Web/CSS/position#Sticky_positioning 中给出的那样消失。

        body {
font-size: 1em;
user-select: none;
background-color: #e0e0e0;
font-family: sans-serif;
font-size: 100%;
}
/* multiple sticky element css --STARTS*/
.info{
position:sticky;
top:0;
}
/* multiple sticky element css _______ENDS*/

main {
width: 50%;
min-width: 550px;
margin: 0 auto;
background-color: #e0e0e0;
height: 300px;
overflow-y: scroll;
}



#wrap {
padding: 5px;
display: grid;
grid-gap: 2px;
position: relative;
z-index: 1;
}

#wrap .right_wrap {
width: 80%;
padding-left: 20%;
display: flex;
justify-content: flex-end;
}

#wrap .left_wrap {
display: flex;
justify-content: flex-start;
width: 80%;
}

.right_wrap .sms_section {
background-color: #dbf4c6;
border-radius: 7.5px;
padding: 6px 7px 8px 9px;
}

.left_wrap .sms_section {
background-color: #f4f4f4;
border-radius: 7.5px;
padding: 6px 7px 8px 9px;
}

.sms_section {
display: grid;
grid-template-columns: 1fr 48px;
}

.left_time,
.right_time {
grid-column: 2/3;
grid-row: 2;
margin-top: -6px;
}

#inpt {
position: sticky;
bottom: 0;
text-align: left;
background-color: #d0d0d0;
padding: 5px 0 5px 5px;
box-shadow: 0 0 20px -11px;
}

input[type='text'] {
font-size: 16px;
width: 80%;
border-radius: 2px;
border: none;
padding: 5px;

}
<body>
<main>
<section id="wrap">
<div class="info"><span>March 1 2020</span></div>

<div class="left_wrap">
<section class="sms_section">
<div class="left_sms">left message left message left message left message left message message messagemessage message messagemessage message messagemessage message messagemessage message messagemessage message messagemessage message messagemessage message messagejkjkjk kjkjkjjkk kjijiji kjkjkjkjkjkjkjkjkjkjkjkjjjkj kjkjkjkjkkkjkjkj </div>
<span class="left_time">
<code>4.06AM</code>
</span>
</section>
<!--.sms_section-->
</div>
<div class="info"><span>March 2 2020</span></div>
<div class="left_wrap">
<section class="sms_section">
<div class="left_sms">left message left message left message left message left message</div> <span class="left_time"><code>4.06AM</code></span>
</section>
</div>

<div class="right_wrap">
<section class="sms_section">
<div class="right_sms">left message left message left message left message left message left message left message left message left message left message</div> <span class="right_time"><code>4.06AM</code></span>
</section>
</div>

<div class="left_wrap">
<section class="sms_section">
<div class="left_sms">left message left message left message left message left message</div> <span class="left_time"><code>4.06AM</code></span>
</section>
</div>
<div class="info"><span>Yesterday</span></div>

<div class="right_wrap">
<section class="sms_section">
<div class="right_sms">left message left message left message left message left message</div> <span class="right_time"><code>4.06AM</code></span>
</section>
</div>
<div class="right_wrap">
<section class="sms_section">
<div class="right_sms">left message left message left message left message left message</div> <span class="right_time"><code>4.06AM</code></span>
</section>
</div>
<div class="right_wrap">
<section class="sms_section">
<div class="right_sms">left message left message left message left message left message</div> <span class="right_time"><code>4.06AM</code></span>
</section>
</div>
<div class="right_wrap">
<section class="sms_section">
<div class="right_sms">left message left message left message left message left message</div> <span class="right_time"><code>4.06AM</code></span>
</section>
</div>

<div class="right_wrap">
<section class="sms_section">
<div class="right_sms">left message left message left message left message left message</div> <span class="right_time"><code>4.06AM</code></span>
</section>
</div>

<div id="inpt">
<input type="text" placeholder="type here">

</div>

</section>
</main>


</body>

在这里帮助

最佳答案

您需要为每一天创建一个容器,将其设置为 position:relative 以供粘性元素锚定。

.sticky-contain{
position: relative;
}

body {
font-size: 1em;
user-select: none;
background-color: #e0e0e0;
font-family: sans-serif;
font-size: 100%;
}
/* multiple sticky element css --STARTS*/
.info{
position:sticky;
top:0;
}
/* multiple sticky element css _______ENDS*/

main {
width: 50%;
min-width: 550px;
margin: 0 auto;
background-color: #e0e0e0;
height: 300px;
overflow-y: scroll;
}



#wrap {
padding: 5px;
display: grid;
grid-gap: 2px;
position: relative;
z-index: 1;
}

#wrap .right_wrap {
width: 80%;
padding-left: 20%;
display: flex;
justify-content: flex-end;
}

#wrap .left_wrap {
display: flex;
justify-content: flex-start;
width: 80%;
}

.right_wrap .sms_section {
background-color: #dbf4c6;
border-radius: 7.5px;
padding: 6px 7px 8px 9px;
}

.left_wrap .sms_section {
background-color: #f4f4f4;
border-radius: 7.5px;
padding: 6px 7px 8px 9px;
}

.sms_section {
display: grid;
grid-template-columns: 1fr 48px;
}

.left_time,
.right_time {
grid-column: 2/3;
grid-row: 2;
margin-top: -6px;
}

#inpt {
position: sticky;
bottom: 0;
text-align: left;
background-color: #d0d0d0;
padding: 5px 0 5px 5px;
box-shadow: 0 0 20px -11px;
}

input[type='text'] {
font-size: 16px;
width: 80%;
border-radius: 2px;
border: none;
padding: 5px;

}
<body>
<main>
<section id="wrap">
<div class="sticky-contain">
<div class="info"><span>March 1 2020</span></div>

<div class="left_wrap">
<section class="sms_section">
<div class="left_sms">left message left message left message left message left message message messagemessage message messagemessage message messagemessage message messagemessage message messagemessage message messagemessage message messagemessage message messagejkjkjk kjkjkjjkk kjijiji kjkjkjkjkjkjkjkjkjkjkjkjjjkj kjkjkjkjkkkjkjkj </div>
<span class="left_time">
<code>4.06AM</code>
</span>
</section>
<!--.sms_section-->
</div>
</div>
<div class="sticky-contain">

<div class="info"><span>March 2 2020</span></div>
<div class="left_wrap">
<section class="sms_section">
<div class="left_sms">left message left message left message left message left message</div> <span class="left_time"><code>4.06AM</code></span>
</section>
</div>

<div class="right_wrap">
<section class="sms_section">
<div class="right_sms">left message left message left message left message left message left message left message left message left message left message</div> <span class="right_time"><code>4.06AM</code></span>
</section>
</div>

<div class="left_wrap">
<section class="sms_section">
<div class="left_sms">left message left message left message left message left message</div> <span class="left_time"><code>4.06AM</code></span>
</section>
</div>

</div>
<div class="sticky-contain">

<div class="info"><span>Yesterday</span></div>

<div class="right_wrap">
<section class="sms_section">
<div class="right_sms">left message left message left message left message left message</div> <span class="right_time"><code>4.06AM</code></span>
</section>
</div>
<div class="right_wrap">
<section class="sms_section">
<div class="right_sms">left message left message left message left message left message</div> <span class="right_time"><code>4.06AM</code></span>
</section>
</div>
<div class="right_wrap">
<section class="sms_section">
<div class="right_sms">left message left message left message left message left message</div> <span class="right_time"><code>4.06AM</code></span>
</section>
</div>
<div class="right_wrap">
<section class="sms_section">
<div class="right_sms">left message left message left message left message left message</div> <span class="right_time"><code>4.06AM</code></span>
</section>
</div>

<div class="right_wrap">
<section class="sms_section">
<div class="right_sms">left message left message left message left message left message</div> <span class="right_time"><code>4.06AM</code></span>
</section>
</div>
</div>
<div id="inpt">
<input type="text" placeholder="type here">

</div>

</section>
</main>


</body>

关于html - html-css 中多个粘性元素的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60525126/

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