简体   繁体   English

jQuery:使用addClass和removeClass时无法滚动到页面底部

[英]JQuery: Unable scroll to bottom of page when using addClass & removeClass

I have the following code that is adding the class "sticky" on scroll and removing the class "sticky" when the element reaches the anchor "#search-anchor". 我有以下代码,它在滚动时添加类“ sticky”,并在元素到达锚点“#search-anchor”时删除类“ sticky”。

However, I'm unable to scroll all the way down to see the full footer. 但是,我无法一直向下滚动以查看完整的页脚。 Any ideas why? 有什么想法吗?

https://jsfiddle.net/coldfusion/6z6q3kxm/2/ https://jsfiddle.net/coldfusion/6z6q3kxm/2/

<style>
header,footer {height:50px;background-color:red;}
section {height: 50px; background-color: yellow;}
#search-anchor {background-color:blue;}
.body {height:1600px;background-color:black;}
.sticky {
   position: fixed;
   bottom: 0px;
   width: 1000px;
   margin-right: auto !important;
   margin-left: auto !important;
   z-index: 999;
}
</style>

<script>

    jQuery(document).ready(function() {
    var a = jQuery(".inline-search").offset().top,
        n = function() {
            var n = jQuery(window).scrollTop(),
                n = n + 10;
            n > a ? jQuery(".inline-search").addClass("sticky") : jQuery(".inline-search").removeClass("sticky")
        };
    n(), jQuery(window).scroll(function() {
        n()
    })
}),

jQuery(document).ready(function() {
    var search = $(".inline-search");
    $(window).scroll(function() {
        var anchorPosition = $("#search-anchor").offset().top;
        var navHeight = $(".inline-search").height();
        var navPosition = $(".inline-search").offset().top;

        if ((navPosition + navHeight) >= anchorPosition) {
            search.removeClass('sticky');
        }

    })
});

</script>

<header>HEADER</header>
<section class="inline-search">
</section>
<div class="body"></div>
<section id="search-anchor">
</section>
<footer>FOOTER</footer>

If you use the instruction debugger to pause the execution of JavaScript in the console, you get exactly what @mike-mccaughan is explaining. 如果使用指令debugger暂停控制台中JavaScript的执行,您将确切地了解@ mike-mccaughan的解释。

You script adds and removes the element .inline-search from the document flow because of the CSS position:fixed; 由于CSS position:fixed;您的脚本从文档流中添加了元素.inline-search并将其删除position:fixed; .

If you scroll down and re-add .inline-search to the document flow, the document height changes again and the scrollbar has some space, so your code that detects if we're at the bottom of the page doesn't work anymore. 如果向下滚动并向文档流中重新添加.inline-search ,文档高度将再次更改,并且滚动条具有一些空间,因此用于检测我们是否位于页面底部的代码将不再起作用。 What happens is an infinite and very fast loop of adding and re-adding your fixed positioning: because of that, it seems as if the page "stops" scrolling before getting to the footer. 发生的事情是无限快速地循环添加和重新添加您的固定位置:因此,页面似乎在到达页脚之前“停止”滚动。

In order to create such an effect (fixed positioning upon scroll), I'd recommend using Bootstrap's affix jQuery plugin. 为了创建这样的效果(滚动时固定定位),我建议使用Bootstrap的附加jQuery插件。 Even further: Bootstrap recently announced that they're dropping Affix because of sticky positioning coming to CSS, so the real "future-proof" recommendation here would be to use sticky positioning with a polyfill to allow older browsers to have the same effect. 更进一步:Bootstrap最近宣布,由于CSS的粘性定位,他们将放弃Affix,因此,真正的“面向未来”的建议是将粘性定位与polyfill结合使用,以使较旧的浏览器具有相同的效果。

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

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