代码之家  ›  专栏  ›  技术社区  ›  ST80

Jquery如何定位:固定了一个元素,直到页面的某个div结束

  •  0
  • ST80  · 技术社区  · 8 年前

    我有一个我只想成为的元素 position: fixed

        $(window).on("scroll", function() {
          var scrollTop = $(this).scrollTop();
    
          var containerOffset = $(".content").offset().top;
          var containerHeight = $(".content").height();
          var f1 = parseInt(containerHeight);
          var f2 = parseInt(containerOffset);
          var sum = f1 + f2;
    
            if (scrollTop > sum - 240) {
              $(".item")
                .css("position", "relative")
                .css("top", f1 + "px")
            }  else {
              $(".item")
                .css("position", "fixed")
                .css("top", "20px");
            }
    
        });
    .content {
      height: 1000px;
      border: 1px solid red;
      background: grey;
    }
    
    .item {
      position: fixed;
      top: 20px;
      height: 40px;
      background: #fff;
      width: 100%;
    }
    
    .another {
      background: orange; 
      height: 400px;
    }
    
    .more {
      background: darkblue;
      height: 400px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="container">
      
      <div class="content">
        <div class="item">
          scroll only until end of grey CONTENT DIV
        </div>
      </div>
      <div class="another">
        <h1>Another div</h1>
      </div>
      <div class="more">
        <h1>More content</h1>
      </div>
    
    </div>
    2 回复  |  直到 8 年前
        1
  •  0
  •   fdomn-m    8 年前

    if (scrollTop > sum - 240) {
    

    目前还不清楚240是从哪里来的,但请将其与您的 .item top 属性

    if (scrollTop > sum - 20) {
    

        $(window).on("scroll", function() {
          var scrollTop = $(this).scrollTop();
    
          var containerOffset = $(".content").offset().top;
          var containerHeight = $(".content").height();
          var f1 = parseInt(containerHeight);
          var f2 = parseInt(containerOffset);
          var sum = f1 + f2;
    
            if (scrollTop > sum - 20) {
              $(".item")
                .css("position", "relative")
                .css("top", f1 + "px")
            }  else {
              $(".item")
                .css("position", "fixed")
                .css("top", "20px");
            }
    
        });
    .content {
      height: 1000px;
      border: 1px solid red;
      background: grey;
    }
    
    .item {
      position: fixed;
      top: 20px;
      height: 40px;
      background: #fff;
      width: 100%;
    }
    
    .another {
      background: orange; 
      height: 400px;
    }
    
    .more {
      background: darkblue;
      height: 400px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="container">
      
      <div class="content">
        <div class="item">
          scroll only until end of grey CONTENT DIV
        </div>
      </div>
      <div class="another">
        <h1>Another div</h1>
      </div>
      <div class="more">
        <h1>More content</h1>
      </div>
    
    </div>

    更新小提琴: https://jsfiddle.net/pb0Ljtn4/20/

        2
  •  0
  •   fdomn-m    8 年前

    将此css添加到html中

    div.sticky_top {
        position: -webkit-sticky; /* Safari */
        position: sticky;
        top: 20px;
    }
    

    加 sticky_top

    <div class="item sticky_top">
        scroll only until end of grey CONTENT DIV
    </div>
    

    https://codepen.io/anon/pen/bxZaPd