简体   繁体   English

如何在js中的多个ID上滚动页面

[英]How to scroll page on multiple ID in js

Please check what i did yet http://jsfiddle.net/dUVmh/1/ . 请检查我还没有做什么http://jsfiddle.net/dUVmh/1/

About the animation i want to achieve is that: 关于我要实现的动画是:

When you first scroll down the page then window scroll to #green DIV. 首次向下滚动页面时,窗口滚动到#green DIV。 After that if you again scroll down window scroll to #yellow DIV & same at the time of scrollup (fom #yellow to #green). 之后,如果您再次向下滚动窗口,则滚动到#yellow DIV并在向上滚动时将相同(从#yellow到#green)。

About the issue: You can see the animation it's stuck on #green DIV. 关于此问题:您可以看到动画粘贴在#green DIV上。

$(window).scroll(function(){
    if($(this).scrollTop() > 0) {
        $("html, body").animate({ scrollTop: $('#green').offset().top }, 1000);
    }
    else if($(this).scrollTop() > 1000) {
        $("html, body").animate({ scrollTop: $('#yellow').offset().top }, 1000);
    }
    else{
         $("html, body").animate({ scrollTop: $('#red').offset().top }, 1000);
    }

});

I didn't have much experience in JS. 我在JS方面没有太多经验。

Thanks i advance :) 谢谢我提前:)

This was a fun problem to work on. 这是一个很有趣的问题。

This solution places the divs into an array, and remembers the array index of the element that was last scrolled to. 此解决方案将div放置到数组中,并记住最后滚动到的元素的数组索引。 Once a scroll event is triggered it checks to see if the new scrollTop is above or below the current divs top offset and moves to the next or previous div in the array accordingly. 触发滚动事件后,它会检查新的scrollTop是否在当前div顶部偏移之上或之下,并相应地移动到数组中的下一个或上一个div。

This solution allows you to have many divs. 该解决方案使您可以拥有许多股利。 I tried to remove the flickering you get when you scroll to fast, but the only way to do that I believe would be to disable the scrollbars during animation. 我试图消除快速滚动时出现的闪烁,但是我认为唯一的方法是在动画播放期间禁用滚动条。

http://jsfiddle.net/dUVmh/35/ http://jsfiddle.net/dUVmh/35/

$(function() {
    var divs = [],
        body = $('body, html'),
        currentDiv = 0,
        timeout;

    $('div').each(function() {
        divs.push($(this));
    });

    // we only need to capture the first scroll event triggered and then
    // add another listener once we have done our animation
    var scrollListen = function() {
        $(window).one('scroll', function() {
            doScroll($(this).scrollTop());
        });
    };

    // Without the timeout, the scroll event would be triggered again too soon
    var scrollEnd = function() {
        clearTimeout(timeout);
        timeout = setTimeout(function() {
            scrollListen();
        }, 10);
    };

    // checks if the scroll direction was up and down and animates
    // the body scrollTop to the next or previous div
    var doScroll = function(scrollTop) {
        var direction = scrollTop - divs[currentDiv].offset().top;

        if (direction > 0 && currentDiv + 1 < divs.length) {
            nextDiv = currentDiv + 1;
        } else if (currentDiv - 1 > -1) {
            nextDiv = currentDiv - 1;
        }

        if (currentDiv === nextDiv) {
            scrollEnd();
        }

        body.animate({
            scrollTop: divs[nextDiv].offset().top
        }, 1000, function() {
            currentDiv = nextDiv;
            scrollEnd();
        });
    };

    scrollListen();
});

Edit : Firefox scrollTop required to be changed on html and not body. 编辑 :Firefox scrollTop需要在html而不是正文上进行更改。 Also fixed a problem with firefox calling scrollListen more than once at a time. 还解决了firefox一次多次调用scrollListen的问题。

The problem is that the $(window).scroll(function()) gets called over and over again when scrolling through the ScrollTop animation with jQuery. 问题是,当使用jQuery滚动ScrollTop动画时,会反复调用$(window).scroll(function())

Here is a possible solution that checks if it is currently scrolling or not and only executes the ScrollTop animation once. 这是一个可能的解决方案,它检查当前是否正在滚动并且仅执行一次ScrollTop动画。

http://jsfiddle.net/dUVmh/29/ http://jsfiddle.net/dUVmh/29/

Side note: It might be a good idea to check which direction the user is scrolling (up or down) and depending on that scroll to the next div to the top or to the down. 旁注:检查用户向哪个方向滚动(向上或向下),并取决于滚动到顶部还是向下的下一个div,这可能是一个好主意。
You can check that be saving the last scrollTop position and comparing it with the current one. 您可以检查是否保存了最后一个scrollTop位置,并将其与当前位置进行比较。

UPDATE: Here's a solution that takes the scroll direction into account: http://jsfiddle.net/dUVmh/36/ 更新:这是一个考虑滚动方向的解决方案: http : //jsfiddle.net/dUVmh/36/

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

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