简体   繁体   English

jQuery滚动无无限循环

[英]jQuery Scroll without infinite loop

I created a simple page with content and a sidebar. 我创建了一个包含内容和侧边栏的简单页面。 When the user scrolls, the sidebar goes down. 当用户滚动时,侧栏会下降。 When the sidebar is small it works right, but when the sidebar is large it gets down infinitely. 当侧边栏较小时,它可以正常工作,但是当侧边栏较大时,它会无限下降。

I created a demo page for better explanation. 我创建了一个演示页面以进行更好的解释。

In this first example, the sidebar is small so it works correct ( http://jsbin.com/ojasuj/2/ ) 在第一个示例中,边栏很小,因此可以正常使用( http://jsbin.com/ojasuj/2/

In this second example, the sidebar is large then it goes infinitely down ( http://jsbin.com/ojasuj/ ). 在第二个示例中,侧边栏很大,然后会无限下降( http://jsbin.com/ojasuj/ )。

I want to stop the animation at the bottom. 我想在底部停止动画。 I am using this javascript: 我正在使用此javascript:

jQuery.noConflict();

jQuery(document).ready(function() {
    jQuery( document ).scroll( function() {
        if( jQuery(document).scrollTop() <= ( jQuery(document).height() - jQuery(window).height() ) ) {
            jQuery(".sidebar").stop().animate({"marginTop": ( jQuery(document).scrollTop() + 10 ) + "px"}, "slow" );
        } else {
            jQuery(".sidebar").css({'margin-bottom': '100px'});
        }
    });
});

I tried several ways but could not solve this problem. 我尝试了几种方法,但无法解决此问题。 I tried to use some scripts, but they also did not work. 我尝试使用一些脚本,但是它们也没有用。 I hope someone can help me. 我希望有一个人可以帮助我。

You should calculate the div's height instead of the document's height since your sidebar will always add "height" when it's larger than your content. 您应该计算div的高度而不是文档的高度,因为当侧边栏大于内容时,它总是会添加“ height”。

Therefor a couple minor changes: 因此,进行了一些小的更改:

HTML HTML

<div id="content">
    <p>all your text etc...</p>
    <div class="clr"></div>
</div>

This will enable your content to wrap around the p tags and give height to the content ID. 这将使您的内容可以环绕p标签,并为内容ID提供高度。 Your sidebar should be kept out of this content div since we don't want it to add height. 您的侧边栏应远离此内容div,因为我们不希望它增加高度。

CSS CSS

.clr {clear:both;}

Makes the content wrap ... 使内容包装...

JS JS

if( jQuery(document).scrollTop() <= ( jQuery("#content").height() - jQuery(window).height() - 150) ) 

Hope you don't mind I used fiddle for this one http://jsfiddle.net/tive/ByNde/ since jsbin was a bit slow for me. 希望你不要介意我为这个http://jsfiddle.net/tive/ByNde/使用了小提琴,因为jsbin对我来说有点慢。

The way I handle this is with a bit of JS. 我处理此问题的方法是使用一些JS。 I check to see if the height of the sidebar is larger than the window height. 我检查边栏的高度是否大于窗口高度。 If it is, disable the 'floating' or 'moving' of the sidebar and allow it to scroll with the rest of the page. 如果是,请禁用侧边栏的“浮动”或“移动”,并使其与页面的其余部分一起滚动。

If you have a sidebar that is not very small it doesn't make sense to scroll it like this anyway. 如果侧边栏不是非常小,那么像这样滚动它是没有意义的。 What if a user wants to see what is at the bottom of the sidebar? 如果用户想查看侧边栏底部的内容怎么办? They would have to scroll all the way to the bottom of the document. 他们将不得不一直滚动到文档的底部。

However to get this working, you should be checking against the height of your main content <div> and not the height of the entire document (because that gets larger every time you scroll). 但是,要使此工作正常进行,应检查主要内容<div>的高度,而不是整个文档的高度(因为每次滚动时,它都会变大)。

if ($(document).scrollTop() <= ( $('#content').height() - $(window).height()) {
    $('.sidebar').stop().animate({'marginTop': ($(document).scrollTop() + 10) + 'px'}, 'slow');
}

I would recommend @Dutchie432's solution: 我会推荐@ Dutchie432的解决方案:

$(document).ready(function() {
    if ($('.sidebar').height() < $(window).height()) {
        // init scrolling scrip here
    }
});

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

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