【问题标题】:Stop fixed div from scroll in a certain point. What is missing in my code? Jquery在某个点停止滚动固定 div。我的代码中缺少什么? jQuery
【发布时间】:2016-07-20 22:51:29
【问题描述】:

我有一个名为“浮动”的 div,它必须从屏幕底部开始,但是当用户滚动时,它必须(友好地)移动到顶部并保持固定,直到到达下方的另一个 div。

到目前为止,我所做的是使 div 正确停止,但当用户开始滚动时,它并没有移动到屏幕顶部。这里缺少什么?

HTML

<div class="container">
  <a id="floating" href="#stop"></a>
  <div id="dummy">some content</div>
  <div id="stop">other content near the footer</div>
  <footer>footer</footer>
</div>

CSS

#floating{
  display: block;
  position: fixed;
  bottom: 5%;
  right: 5%;
  width: 115px;
  height: 115px;
  color: #fff;
  z-index: 1;
  background-color: #0055db;
}

JQUERY

function checkOffset() {
  var a=$(document).scrollTop()+window.innerHeight;
  var b=$('#stop').offset().top;
  if (a<b) {
    $('#floating').css('bottom', '5%');
  } else {
    $('#floating').css('bottom', (20+(a-b))+'px');
  }
}
$(document).ready(checkOffset);
$(document).scroll(checkOffset);

感谢所有帮助!谢谢!

【问题讨论】:

    标签: javascript jquery animation scroll fixed


    【解决方案1】:
    Try using JQuery Animate
    
     <script>
       $(document).ready(function(){
           $(document).scroll(function(){
              $("#floating").animate({top: '10px'}, "slow");
        });
      });
     </script>
    

    【讨论】:

    • 你好@罗伯特!谢谢您的帮助!我让 div 移动到顶部,但是当到达#stop div 时我无法让它停止。我尝试了类似的方法:function checkOffset() { var a=$(document).scrollTop()+window.内部高度; var b=$('#stop').offset().top; if (a
    猜你喜欢
    • 1970-01-01
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 1970-01-01
    • 2013-06-09
    • 2021-04-24
    相关资源
    最近更新 更多