在固定位置创建粘性Div的问题

时间:2020-07-08 12:52:07

标签: javascript html jquery css

我尝试使用下面的代码制作粘性DIV,但效果不如预期。当我向下滚动时,DIV是粘滞的,但与网站页眉以及页脚重叠。 如何使用CSS / JS解决此问题? 任何帮助都将是可观的。谢谢。

def neighbors_for_person(person_id):
"""
Returns (movie_id, person_id) pairs for people
who starred with a given person.
"""
movie_ids = people[person_id]["movies"]
neighbors = set()
for movie_id in movie_ids:
    for person_id in movies[movie_id]["stars"]:
        neighbors.add((movie_id, person_id))
return neighbors
.a{
    float: left;
    width: 67%;
}
.b{
    width: 32%;
    float: right;
    position: fixed;
    right: 0;
}
.c{
    width: 100%;
}

-----

1 个答案:

答案 0 :(得分:1)

我想这就是您要查找的内容,只需滚动页面以查看粘滞的div,最好在整个页面中运行摘要

window.onscroll = function() {
  progress = document.getElementsByClassName('b')[0];
  let height = window.pageYOffset;
  if (height > 395) {
    progress.style.position = 'fixed';
    progress.style.top = `${20}px`;
  } else {
    progress.style.position = 'absolute';
    progress.style.top = `${400}px`;
  }
}
body {
  height: 300vh;
}

.b {
  width: 32%;
  position: absolute;
  top: 400px;
  background: darkcyan;
  height: 250px;
}
<div class="b"></div>