css 当元素滚动出屏幕时,将绝对定位的元素锚定到顶部或底部

1wnzp6jl  于 2022-12-30  发布在  其他
关注(0)|答案(3)|浏览(282)

我有一个垂直上下滚动的容器。在容器中有一些绝对定位的项目。下面是一个例子来说明。

#body {
  height: 200px;
  overflow: scroll;
  font-family: sans-serif;
  font-size: 40px;
}

#container {
  width: 100%;
  height: 600px;
  background-color: rgba(255, 0, 0, 0.1);
  position: relative;
  display: flex;
  align-items: start;
  justify-content: center;
}

#item {
  display: inline;
  background-color: rgba(0, 0, 255, 0.1);
  padding: 10px 20px;
  border-radius: 5px;
  position: absolute;
  top: 150px;
}
<div id="body">
  <div id="container">
    <div id="item">
      Hello
    </div>
  </div>
</div>

我想确保在用户上下滚动时,项目的一小部分始终可见,以便用户始终可以看到那里的内容,而不会失去跟踪。当用户向下滚动太远时,它应该如下所示...

相反,如果用户向上滚动太多,它应该看起来像这样...

    • 元素可以放置在容器中的任何位置也很重要。**

这在纯CSS中可行吗?如果不可行,用纯CSS & JS实现同样效果的最有效方法是什么(记住容器中可能有多个项目)?

omqzjyyz

omqzjyyz1#

不确定我是否正确理解了这些条件,但假设目标是在容器内滚动时使#item始终部分可见,也许可以尝试在#item上设置position: sticky
这样,可以通过topbottom来指定当#item粘附到边界时#item的偏移范围。
示例:

#body {
  height: 200px;
  overflow: scroll;
  font-family: sans-serif;
  font-size: 40px;
  background-color: rgba(255, 0, 0, 0.1);
}

#container {
  width: 50%;
  margin: 0 auto;
  height: 900px;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

#item {
  background-color: rgba(0, 0, 255, 0.1);
  padding: 10px 20px;
  border-radius: 5px;
  position: sticky;
  top: -50px;
  bottom: -50px;
}

#spacer {
  height: 220px;
}
<div id="body">
  <div id="container">
    <div id="spacer"></div>
    <div id="item">
      Hello
    </div>
  </div>
</div>
wwtsj6pe

wwtsj6pe2#

div{
  width:150px;
  height:150px;
  background: purple;
  opacity: .25;
  border-radius:10px;
  margin:auto;
  position:sticky;
  top:-130px;
  bottom:-130px;
}
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<div></div>
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

让我知道你的想法。

ppcbkaq5

ppcbkaq53#

既然你悬赏了这个,我不能把它标记为重复,所以我只把它作为一个答案:
问题是:Can a position:absolute element be made sticky?
The unappreciated correct answer.
这是我刚刚整理出来的a new answer,这是一个正在进行中的工作,它有可能被采用来解决上面的问题,等待对底层问题的澄清。

相关问题