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

滚动时根据其他元素显示/隐藏(切换)固定元素?

  •  0
  • nrweb  · 技术社区  · 7 年前

    是否可以根据垂直滚动时传递的元素在显示和隐藏固定元素之间切换?

    以下是我希望实现目标的视觉参考:

    enter image description here

    我希望这个函数能够检测页面上固定元素的位置,而不是窗口的滚动位置。

    希望照片足够说明问题;否则,请检查下面的片段:

    $(document).ready(function() {
      var $window = $(window);
      var div2 = $('#div2');
      var div1 = $('#div1');
      var div1_top = div1.offset().top;
      var div1_height = div1.height();
      var div1_bottom = div1_top + div1_height;
      console.log(div1_bottom);
      $window.on('scroll', function() {
        var scrollTop = document.documentElement.scrollTop;
        var viewport_height = $window.height();
        var scrollTop_bottom = scrollTop + viewport_height;
        div2.toggleClass('show', scrollTop > div1_top && (scrollTop + window.innerHeight) < div1_bottom);
      });
    });
    body {
      background: #ccffcc;
      padding: 0;
      margin: 0;
      border: 0;
      text-align: center;
    }
    
    #div1 {
      background: #0099ff;
      height: 1500px;
      color: #fff;
    }
    
    #div2 {
      width: 100px;
      height: 100px;
      text-align: center;
      position: fixed;
      margin: auto;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      background: #ffff00;
      color: #000;
      display: none;
    }
    
    #div2.show {
      display: block;
    }
    
    #div3 {
      height: 1500px;
      color: #000;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <br>
    <br>
    <br>
    <br>
    <br> Scroll area before <b>div1</b> appears
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <div id="div1">
      <div id="div2">This is <b>div2</b></div>
      This is <b>div1</b>
      <br>
      <i>(Toggle show/hide <b>div2</b> when the top of <b>div2</b> passes the top of <b>div1</b>)</i>
    </div>
    <div id="div3">
      This is <b>div3</b>
      <br>
      <i>(Toggle show/hide <b>div2</b> when the bottom of <b>div2</b> reaches the top of <b>div3</b>)</i>
    </div>
    1 回复  |  直到 7 年前
        1
  •  1
  •   Terry Wei    7 年前

    这就是你想要的吗?

    如果是,解决方法是考虑窗口顶部到div2(中间部分)的距离

    编辑

    添加注释所满足的功能

    $(document).ready(function() {
      var $window = $(window);
      var div2 = $('#div2');
      var div1 = $('#div1');
      $window.on('scroll', function() {
        var scrollTop = document.documentElement.scrollTop;
        var viewport_height = $window.height();
        var scrollTop_bottom = scrollTop + viewport_height;
        var window_top_to_div2 = ($window.height()-div2.height())/2;
        
        
        var div1_top = div1.offset().top;
        var div1_height = div1.height();
        var div1_bottom = div1_top + div1_height;
      
        div2.toggleClass('show', scrollTop >= (div1_top-window_top_to_div2) && (scrollTop + window.innerHeight) <= (div1_bottom+window_top_to_div2));
      });
    });
    body {
      background: #ccffcc;
      padding: 0;
      margin: 0;
      border: 0;
      text-align: center;
    }
    
    #div1 {
      background: #0099ff;
      height: 1500px;
      color: #fff;
    }
    
    #div2 {
      width: 100px;
      height: 100px;
      text-align: center;
      position: fixed;
      margin: auto;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      background: #ffff00;
      color: #000;
      display: none;
    }
    
    #div2.show {
      display: block;
    }
    
    #div3 {
      height: 1500px;
      color: #000;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <br>
    <br>
    <br>
    <br>
    <br> Scroll area before <b>div1</b> appears
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <div id="div1">
      <div id="div2">This is <b>div2</b></div>
      This is <b>div1</b>
      <br>
      <i>(Toggle show/hide <b>div2</b> when the top of <b>div2</b> passes the top of <b>div1</b>)</i>
    </div>
    <div id="div3">
      This is <b>div3</b>
      <br>
      <i>(Toggle show/hide <b>div2</b> when the bottom of <b>div2</b> reaches the top of <b>div3</b>)</i>
    </div>