假设我有一个元素,它在视觉上是容器的一部分。这个容器的高度可以很长。
现在,当用户向下滚动时,我想将此元素放置在屏幕的底部。但是当容器在某个时刻结束时,我希望元素保留在该容器的底部并且不再向下滚动。
所以再一次:当容器还没有结束时,元素位于屏幕的底部:
...但是当我继续滚动时,它停在容器内:
我有点被困在这里了。当元素位于容器内部的顶部时,我确实知道如何执行此操作。向下滚动只会使其停在底部。
问题似乎在于,要么元素将被移动到文档流之外,因此它不会保留在容器内,要么它将始终位于容器的底部,因此该元素不会“不随用户滚动并保持在屏幕的底部。
有什么想法吗?
<div className="mx-20 my-36">
<div className="bg-slate-200 h-[1200px] w-full relative border-2 border-black relative">
<div className="fixed bottom-0 left-0 right-0 p-2 bg-white w-full border-2 border-red-600">Element</div>
</div>
<div className="my-12">
The page continues here but the element remains in the container ...
</div>
</div>
PS:这里使用顺风和反应,但也欢迎任何普通CSS!我很想在没有 JavaScript 的情况下解决这个问题。
fixed
切换为
sticky
relative
- 它不再做任何事情
<div className="mx-20 my-36">
<div className="bg-slate-200 border-2 border-black w-full">
<div className="h-[1200px]">
</div>
<div className="sticky bottom-0 left-0 right-0 p-2 bg-white w-full border-2 border-red-600">Element</div>
</div>
<div className="my-12">
The page continues here but the element remains in the container ...
</div>
</div>
演示:
Position: Sticky 使元素出现在父容器的顶部,然后滚动到固定位置(字面意思类似于position:fixed)的所有其他元素,直到父容器的末尾。
要完成这项工作,您需要:
.sticky {
position: -webkit-sticky;
position: sticky;
bottom: 0;
height: 30px;
width: 100vw;
background: yellow;
}
<!-- Content you want before the sticky element -->
<div>
<div style="height: 300px; background: red;">Scroll</div>
<div style="height: 300px; background: orange;"></div>
</div>
<!-- Content you want after the sticky element -->
<div>
<!-- The sticky element will appear as if its been placed here -->
<div style="height: 300px; background: green;"></div>
<div style="height: 300px; background: blue;"></div>
<div style="height: 300px; background: red;">Scroll</div>
<div style="height: 300px; background: orange;"></div>
<div style="height: 300px; background: red;">Scroll</div>
<div style="height: 300px; background: orange;"></div>
<div style="height: 300px; background: red;">Scroll</div>
<div style="height: 300px; background: orange;"></div>
<div class="sticky">Sticky Section</div>
</div>
如果你想走 jquery 路线...
$(document).scroll(function() {
var viewportPlacement = $(window).scrollTop() + $(window).height(); //finds the height of the bottom of the viewport
var initialSticky = $('.sticky').prev().height() + $('.sticky').height(); //finds the initial placement of the sticky element, plus the height of the element so the whole thing has to scroll into view for the fixed position to be added
if (viewportPlacement >= initialSticky) { //if the bottom of the viewport has the whole sticky element in it
$('.sticky').css('position', 'fixed') //then add position fixed to it
} else if (viewportPlacement < initialSticky){ //else if the initial placement of the sticky element is below the viewport
$('.sticky').css('position', 'static') //remove the position fixed
}
})
.sibling {
background-color: lavender;
height: 150vh;
}
.sticky {
background-color: pink;
box-sizing: border-box;
border: 1px solid fuchsia;
position: static;
padding: 20px;
width: 100%;
bottom: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="sibling"></div>
<div class="sticky">Hello World</div>
<div class="sibling"></div>