在最后一个元素上停止固定块

Stop fixed block on last element

我有以下片段(将片段打开为整页):

#container_1{
    margin-top: 50px;
    margin-bottom: 150px;
}

.block1{
    background: green;
    margin-bottom: 15px;
    color: #FFFFFF;
}

#moving_block{
    z-index: 0;
    background-color: purple;
    color: #FFFFFF;
}

#footer{
  background-color: gray;
  height: 5000px;
  color: #FFFFFF;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<div id="container_1" class="container">
    <div class="col-md-5 col-md-offset-1">
        <div class="row block1">
            <div class="col-md-8">
                <p>TEXT</p>
            </div>
        </div>
        <div class="row block1">
            <div class="col-md-8">
                <p>TEXT</p>
            </div>
        </div>
        <div class="row block1">
            <div class="col-md-8">
                <p>TEXT</p>
            </div>
        </div>
        <div class="row block1">
            <div class="col-md-8">
                <p>TEXT</p>
            </div>
        </div>
        <div class="row block1">
            <div class="col-md-8">
                <p>TEXT</p>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div id="moving_block" class="col-md-3 affix">
            <div class="row">
                <p>TEXT</p>
            </div>
         </div>
    </div>
</div>

<section id="footer">
MY FOOTER
</section>

当他遇到最后一个绿色块时,我想停止紫色块。

这可以使用 position:sticky 来完成。

我考虑 bootstrap V4,因为有内置的粘性功能。我还简化了代码,只保留与演示相关的代码:

#container_1 {
  margin-top: 50px;
  margin-bottom: 150px;
}

.block1 {
  background: green;
  color: #FFFFFF;
}
.block1:not(:last-child) {
  margin-bottom: 15px;
}

#moving_block {
  background-color: purple;
  color: #FFFFFF;
}

#footer {
  background-color: gray;
  height: 5000px;
  color: #FFFFFF;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet">
<div id="container_1" class="container">
  <div class="row">
    <div class="col-5 col-offset-1">
      <div class="row block1">
          <p>TEXT</p>
      </div>
      <div class="row block1">
          <p>TEXT</p>
      </div>
      <div class="row block1">
          <p>TEXT</p>
      </div>
      <div class="row block1">
          <p>TEXT</p>
      </div>
      <div class="row block1">
          <p>TEXT</p>
      </div>
    </div>
    <div class="col-6">
      <div id="moving_block" class="overflow-auto position-sticky sticky-top">
          <p>TEXT</p>
      </div>
    </div>
  </div>
</div>

<section id="footer">
  MY FOOTER
</section>