简体   繁体   English

粘边栏可在页脚上方停止30px?

[英]Sticky sidebar to stop 30px above footer?

I have a sticky sidebar, however I want to make it stop when he is 30px above #footer. 我有一个粘性的侧边栏,但是当他在#footer上方30px处时,我想使其停止。 How can I do this? 我怎样才能做到这一点?

$(function(){ // document ready
  if (!!$('.sticky').offset()) { // make sure ".sticky" element exists
    var stickyTop = $('.sticky').offset().top; // returns number 
    $(window).scroll(function(){ // scroll event
      var windowTop = $(window).scrollTop(); // returns number 
      var CurrentWidth = $('.sidebar').width();
      if (stickyTop < windowTop){
        $('.sticky').css({ position: 'fixed', top: 0, width:CurrentWidth });
      } else {
        $('.sticky').css('position','static');
      }
    });
  }
});




        <div class="col-md-3">
<aside class="sidebar  sticky">

          <nav>
            <ul class="sidebar-links">

<li><a href="#">Privacy policy</a></li>
<li><a href="#">Privacy policy</a></li>
<li><a href="#">Privacy policy</a></li>
<li><a href="#">Privacy policy</a></li>
<li><a href="#">Privacy policy</a></li>
<li><a href="#">Privacy policy</a></li>
<li><a href="#">Privacy policy</a></li>
<li><a href="#">Privacy policy</a></li>
            </ul>
          </nav>


        </aside>


        </div>

Just need to add the $(element).offset().top to detect where the top of your footer is, then add a check to your scroll function. 只需添加$(element).offset().top即可检测页脚的顶部,然后在滚动功能中添加一个检查。

This is the section we're going to add to the scroll function: 这是我们要添加到滚动功能的部分:

var footerAboveThirty = $('footer').offset().top - 30;

if (windowTop > footerAboveThirty) {
  $('.sticky').css({ position: 'absolute', top: footerAboveThirty, width: CurrentWidth });
} else {
  $('.sticky').css({ position: 'fixed', top: 0, width:CurrentWidth });
}

Here it is in context: 在上下文中:

$(function(){ // document ready
  if (!!$('.sticky').offset()) { // make sure ".sticky" element exists
    var stickyTop = $('.sticky').offset().top; // returns number 
    $(window).scroll(function(){ // scroll event
      var windowTop = $(window).scrollTop(); // returns number 
      var CurrentWidth = $('.sidebar').width();
      if (stickyTop < windowTop){
        //NEW SECTION
        var footerAboveThirty = $('footer').offset().top - 30;
        if (windowTop > footerAboveThirty) {
          $('.sticky').css({ position: 'absolute', top: footerAboveThirty, width: CurrentWidth });
        } else {
          $('.sticky').css({ position: 'fixed', top: 0, width:CurrentWidth });
        }
        //END NEW SECTION
      } else {
        $('.sticky').css('position','static');
      }
    });
  }
});

Update: 更新:

Also, be sure to include the height of your sticky element. 另外,请确保包括粘性元素的高度。 So this line: 所以这行:

var footerAboveThirty = $('footer').offset().top - 30;

changes to 更改为

var footerAboveThirty = $('footer').offset().top - $('.sticky').height() - 30;

JSFiddle example JSFiddle示例

You should check if the bottom of the sticky element is above the footer with 30px . 您应该检查sticky元素的底部是否在30px的页脚上方。 And modify your check accordingly. 并相应地修改您的支票。

The code (make sure you update the FOOTER_SELECTOR according to your setup): 代码(确保您根据设置更新FOOTER_SELECTOR):

var FOOTER_SELECTOR = '.footer';

$(function(){ // document ready
  if (!!$('.sticky').offset()) { // make sure ".sticky" element exists
    var stickyTop = $('.sticky').offset().top; // returns number 
    $(window).scroll(function(){ // scroll event
      var windowTop = $(window).scrollTop(); // returns number 
      var CurrentWidth = $('.sidebar').width();
      var $sticky =  $('.sticky');

      var stickyBottom = $sticky.offset().top + $sticky.outerHeight();
      var footerTop = $(FOOTER_SELECTOR).offest().top;
      var moreThan30PxAboveFooter = footerTop - 30 > stickyBottom;

      if (stickyTop < windowTop){
        if (moreThan30PxAboveFooter) {
          $sticky.css({ position: 'fixed', top: 0, width:CurrentWidth });
        }
      } else {
         $sticky.css('position','static');
      }
    });
  }
});

.

.

.... stickyBottom .... stickyBottom

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

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