简体   繁体   English

在到达页脚时使固定位置div停止

[英]Making fixed position div stop when reaching footer

I have a side menu (.link-panel) . 我有一个侧边菜单(.link-panel) Inside .link-panel is a div (.cover) that hold the contents of the .link-panel . 里面.link-panel是一个div (.cover) ,它(.cover) .link-panel的内容。 .cover is a fixed div so that the links can move when the user scrolls. .cover是一个固定的div,以便在用户滚动时链接可以移动。 The only problem I am facing is that I have a footer at the bottom, and whenever I scroll down the cover div goes on top of the .footer . 我面临的唯一问题是我在底部有一个页脚,每当我向下滚动时, cover div就会出现在.footer顶部。 I am trying to make it so that the .cover stops when it reaches the footer. 我试图使它成为.cover到达页脚时停止。 That way the .footer and .cover don't overlap. 那样.footer.cover不会重叠。 I have tried using some jQuery in order to solve this, but my technique is not working. 我尝试使用一些jQuery来解决这个问题,但我的技术不起作用。 It's producing very weird results. 它产生了非常奇怪的结果。 Sometimes, some of the links are above the window and can't be shown, other times when you scroll down to the .footer , the .link-panel does not go up again when you scroll. 有时,某些链接位于窗口上方且无法显示,有时当您向下滚动到.footer.footer .link-panel在滚动时不会再次显示。 You can see and experiment here with the jsFiddle I created. 你可以在这里看到并试验我创建的jsFiddle

HTML HTML

<div class="container">

  <div class='control_panel'>
    <div class='control_title'>
      <h2>Your Settings</h2>
    </div>

    <div class='control_settings'>

    </div>
  </div>

  <div class="link-panel">
  <div class="cover">


    <ul>


      <li> Dashboard</li>
      <hr>
      <li> Blog</li>
      <hr>
      <li><span><b>|</b> Settings</span></li>
      <hr>
      <li> Contact Us</li>


    </ul>
      </div>
  </div>
  <!--End of link panel div-->
</div>

<div class='footer'>

</div>

CSS CSS

.container {
  display: block;
  margin: 0px auto;
  width: 100%;
  padding-left: 30%;
  box-sizing: border-box;
  position: relative;
}

.footer {
  display: block;
  width: 100%;
  height: 500px;
  background-color: black;
  margin-top: 0px;
}

html,
body {
  position: relative;
  height: 100%;
  background-color: #f2f2f2;
}

.control_panel {
  position: relative;
  display: inline-block;
  width: 60%;
  margin-left: 0px;
}

.control_title {
  display: block;
  background-color: white;
  height: 100px;
  margin-bottom: 30px;
}

.control_settings {
  display: block;
  background-color: white;
  height: 900px;
  width: 900px;
}

.link-panel {
  position: absolute;
  float: left;
  width: 30%;
  height: 100%;
  background-color: #333333;
  left: 0;
  top: 0;
}

.cover{
   position: fixed;
}
.link-panel ul {
  list-style-type: none;
  font-size: 19px;
  margin-top: 35px;
}

.link-panel li {
  margin-top: 15px;
}

jQuery jQuery的

function checkOffset() {
  var a=$(document).scrollTop()+window.innerHeight;
  var b=$('.footer').offset().top;
  if (a<b) {
    $('.cover').css('bottom', '-14');
  } else {
    $('.cover').css('bottom', (-14+(a-b))+'px');
  }
}
$(document).ready(checkOffset);
$(document).scroll(checkOffset);

How do I make it so that .cover moves up and down when the user scrolls, but stops when it reaches the .footer , but when the user scrolls up again so does the .cover ? 如何设置它以便.cover在用户滚动时上下移动,但在到达.footer时停止,但是当用户再次向上滚动时, .cover

EDIT: hopefully now it works on Chrome too. 编辑:希望现在它也适用于Chrome。

I've made a fiddle for your case: https://jsfiddle.net/g8ctha2o/5/ 我为你的案子做了一个小提琴: https//jsfiddle.net/g8ctha2o/5/

Tell me if it works for you. 告诉我它是否适合你。

Basically what you do is you check on the scroll event that the bottom edge of the menu has a smaller position value than the top edge of the footer. 基本上你要做的是检查滚动事件,菜单的下边缘的位置值小于页脚的上边缘。 Once this is no longer true, you reach a scroll threshold in which you change its position from fixed to absolute with jQuery. 一旦这不再成立,你就会达到一个滚动阈值,你可以用jQuery将其位置从fixed更改为absolute。

var menuBottom,
  footerTop = $('.footer').offset().top,
  positionChanged = false,
  scrollThreshold;

$('.scrollable').on('scroll', function() {
  menuBottom = $('.scrollable').scrollTop() + $('.menu').offset().top + $('.menu').outerHeight();
  if (!positionChanged) {
    if (menuBottom + 5 >= footerTop) {
      scrollThreshold = $('.scrollable').scrollTop();
      $('.menu').css({
        visibility: 'hidden',
        position: 'absolute',
        top: menuBottom - $('.menu').outerHeight() - 5,
        right: 35,
        visibility: 'visible'
      });
      positionChanged = true;
    }
  } else {
    if (scrollThreshold > $('.scrollable').scrollTop()) {
      $('.menu').css({
        position: 'fixed',
        top: 450,
        right: 60
      });
      positionChanged = false;
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM