固定与其父级相关的div

Thi*_*ira 3 html javascript css jquery css3

我有这个片段,但我需要将红色框限制为不透明的白色框.

.main {
  background-color: black;
  width: 100%;
  height: 1250px;
  position: relative;
}

.container{
  display: block;
  position: absolute;
  width: 250px;
  height: 650px;
  background: white;
}

.red-box{
  background: red;
  width: 150px;
  height: 140px;
  position: fixed;
}
Run Code Online (Sandbox Code Playgroud)
<div class="main">
  <div class="container">
    <div class="red-box">
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

kuk*_*kuz 5

由于red-boxfixed已经,让您的container固定too-希望这能解决你的问题.干杯!

body {
  margin: 0;
}
.main {
  background-color: black;
  width: 100%;
  height: 1250px;
  position: relative;
}
.container {
  display: block;
  position: fixed;
  width: 250px;
  height: 650px;
  background: white;
}
.red-box {
  background: red;
  width: 150px;
  height: 140px;
  position: fixed;
}
Run Code Online (Sandbox Code Playgroud)
<div class="main">
  <div class="container">
    <div class="red-box">
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

编辑:

希望这会引导它 - 使用jquery red-div滚动出来container:

$(document).scroll(function() {
  var wrapper = $('.container');
  var box = $('.red-box');

  var offset = wrapper.offset().top - $(window).scrollTop() + wrapper.outerHeight() - box.outerHeight();


  if (offset >= 0) {
    box.css({
      'top': 0
    });
    return;
  }

  box.offset({
    'left': box.offset().left,
    'top': $(window).scrollTop() + offset
  });


});
Run Code Online (Sandbox Code Playgroud)
body {
  margin: 0;
}
.main {
  background-color: black;
  width: 100%;
  height: 1250px;
  position: relative;
}
.container {
  display: block;
  position: absolute;
  width: 250px;
  height: 650px;
  background: white;
}
.red-box {
  background: red;
  width: 150px;
  height: 140px;
  position: fixed;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
  <div class="container">
    <div class="red-box">
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)