Continuous Looping Page (Not Infinite Scroll)

前端 未结 6 1406
忘掉有多难
忘掉有多难 2020-12-02 21:16

I\'m looking for resources that create scrolling functions like the ones found on these sites:
Outpost Journal
Unfold

Once the scroll bar hits the bottom

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-02 22:00

    Adding loop scroll backwards, upgrading @clankill3r answer. It should be something like this.

    $('document').ready(function() {
    
     // We need to duplicate the whole body of the website so if you scroll down you can see both the bottom and the top at the same time. Before we do this we need to know the original height of the website.
     var origDocHeight = document.body.offsetHeight;
    
     // now we know the height we can duplicate the body    
     $("body").contents().clone().appendTo("body");
    
    
     $(document).scroll(function(){ // detect scrolling
    
         var scrollWindowPos = $(document).scrollTop(); // store how far we have scrolled
    
         if(scrollWindowPos >= origDocHeight ) { // if we scrolled further then the original doc height
             $(document).scrollTop(scrollWindowPos + origDocHeight); // then scroll to the top
         } else if (scrollWindowPos == 0) { // if we scrolled backwards
             $(document).scrollTop(origDocHeight);
         }
     });
    });
    

    I'm using it horizontally and it's working just fine. Hope someone finds it useful.

提交回复
热议问题