简体   繁体   English

在某一点停止固定元素滚动?

[英]Stop fixed element scrolling at certain point?

I have a fixed position nav that fades in at a set scroll point. 我有一个固定的位置导航,在设定的滚动点淡入。

I now need to stop it scrolling just before the page footer (about 400px from the bottom). 我现在需要在页脚之前滚动它(从底部大约400px)。 I know the way to do this would be to change the position from fixed to absolute but I'm not sure how to implement that through jquery? 我知道这样做的方法是将位置从固定更改为绝对但是我不确定如何通过jquery实现它?

Live Example on the jsFiddle jsFiddle上的实例

jQuery: jQuery的:

var isVisible = false;

$(window).scroll(function(){
    var shouldBeVisible = $(window).scrollTop()>1000;
        if (shouldBeVisible && !isVisible) {
        isVisible = true;
    $('#floatingnav').fadeIn('slow');
    } else if (isVisible && !shouldBeVisible) {
        isVisible = false;
        $('#floatingnav').fadeOut('fast');
    }
});

CSS: CSS:

#floatingnav{
    position: fixed;
    top: 40px;
    display: none;
}

Check if the bottom position of the navigation is below the footer top position. 检查导航的底部位置是否低于页脚顶部位置。 If this is the case, set a class or a specific css-prop. 如果是这种情况,请设置类或特定的css-prop。

$(window).scroll(function(){
  var windowTopPos = $(window).scrollTop();
  var footerTopPos = $('#footer').offset().top;
  var navBottomPos = $('#floatingnav').offset().top + $('#floatingnav').outerHeight();

  if(navBottomPos >= footerTopPos) {
    $('#floatingnav').css('position', 'absolute');
  }
});

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

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