简体   繁体   English

绝对位置固定在可滚动容器中

[英]Position absolute and fixed in a scrollable container

I have a scrollable container, I want an image to be displayed in the middle on the bottom of the container, that will stay in the same place on the screen when I will scroll down. 我有一个可滚动的容器,我希望图像显示在容器底部的中间,当我向下滚动时,该图像将保留在屏幕上的同一位置。 How should I do this? 我应该怎么做? Right now I have something like this: 现在我有这样的事情:

  #container { height: 80vh; border: 1px solid black; padding: 0 !important; overflow-y: auto; overflow-x: hidden; position: relative; } #fixed { position: fixed; bottom: 0; left:0; right:0; margin-left:auto; margin-right:auto; background-image: url('../images/img.png'); width: 256px; height: 256px; } 
  <div id="container"> // scrollable content <div id="fixed"> </div> </div> 

But the image seems to be completely out of place. 但是图像似乎完全不合适。 Position: absolute make the element disppear when I keep scrolling. Position: absolute让我在滚动时消失。 How can I solve this? 我该如何解决?

Here's a solution: 这是一个解决方案:

Main changes to your draft: min-height of container: 100vh (full height of window), plus some details ( margin: 0; on body , box-sizing: border-box; on container, bottom: 1px on image div) 草稿的主要更改:容器的min-height :100vh(窗口的全高),加上一些详细信息( margin: 0;bodybox-sizing: border-box;在容器上, bottom: 1px图像div上bottom: 1px

 body { margin: 0; } #container { min-height: 100vh; box-sizing: border-box; border: 1px solid black; padding: 0 !important; overflow-y: auto; overflow-x: hidden; position: relative; } #fixed { position: fixed; bottom: 1px; left: 0; right: 0; margin-left: auto; margin-right: auto; background-image: url('http://placehold.it/256'); width: 256px; height: 256px; } 
 <div id="container"> // scrollable content <br><br><br><br><br><br> <p>blabla</p> <br><br><br><br><br><br> <p>blabla</p> <br><br><br><br><br><br> <p>blabla</p> <br><br><br><br><br><br> <p>blabla</p> <br><br><br><br><br><br> <p>blabla</p> <br><br><br><br><br><br> <p>blabla</p> <div id="fixed"> </div> </div> 

You can use flexbox layout. 您可以使用flexbox布局。

jsFiddle jsFiddle

 #container { height: 80vh; border: 1px solid; display: flex; flex-direction: column; } #scroll { background: pink; overflow: auto; flex: 1; } #fixed { background: silver; flex: 0 0 30px; } 
 <div id="container"> <div id="scroll"> <div style="height: 200vh;"></div> </div> <div id="fixed"> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM