繁体   English   中英

滚动位置在底部时更改为固定位置

[英]Changing to fixed positioning when scroll position is at bottom

基本上,我正在检查滚动位置是否在页面底部,并基于此添加和删除类。 但是,在删除固定类时,我无法滚动到页面底部。 浏览器已经假定我在底部。 我该如何纠正? 如果这没有意义,请告诉我。 下面是我的代码:

JavaScript:

function fixedToRelative(){
    var scrollPos = $(window).scrollTop() + $(window).height();
    if(scrollPos == $(document).height()) {
        $('.mobile.full').removeClass('fixed');
    } else {
        $('.mobile.full').addClass('fixed');
    }
}

CSS:

.mobile { position:relative; }
.mobile.fixed { position:fixed; bottom:0; left:0; right:0; }

在此处输入图片说明

我认为您在滚动到页面底部时尝试添加.fixed类。 如果是这样,您可以执行以下操作:

码笔

$(window).on('scroll', function(){
    var scrollPos = $(this).scrollTop() + $(this).height(); // Current Scroll position plus height of window
    var atBottom = (scrollPos == $(document).height()); // Returns true/false based on if at bottom
    $('.mobile').toggleClass('fixed', atBottom); // If at bottom of page, fixed class is appended
});

暂无
暂无

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

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