2015-04-28 86 views
0

我想知道連續網站的代碼,如果我滾動到底部並返回頂部,否則。永不結束的滾動頁面(滾動頁面頂部和底部或其他)

我已經在半路上,我使用此代碼爲底頁:

$(document).ready(function() { 
    $(document).scroll(function(){ 
     if (document.documentElement.clientHeight + $(window).scrollTop() >= $(document).height()) { 
     $(document).scrollTop(0); 
     } 
    }); 
    }); 

我怎樣才能讓反之? 從頂部到底部?

編輯:

$(document).ready(function() { 
       $(document).scroll(function(){ 
        if (document.documentElement.clientHeight + $(window).scrollTop() >= $(document).height()) { 
        $(document).scrollTop(0); 
        } 
        else if ($(window).scrollTop() <= 0) { 
        $(document).scrollTop($(document).height()); 
        } 
       }); 
       }); 

我想,代碼和它的作品,但我認爲這是一個小錯誤在那裏,我需要滾動多一點,從頂部到達底部,或者以其他方式,這是爲什麼發生?

感謝

回答

0

scrollTop的不能低於0,所以你可能希望在滾動時它上面,它發送給你的底頁的頂部留下一個緩衝。

事情是這樣的:

var BUFFER = 10; 

$(document).ready(function() { 
    $(document).scrollTop(BUFFER); 
    $(document).scroll(function(){ 
    var scrollPos = document.documentElement.clientHeight + $(window).scrollTop(); 
    if (scrollPos >= $(document).height()) { 
     $(document).scrollTop(BUFFER); 
    } else if (scrollPos < BUFFER) { 
     $(document).scrollTop($(document).height()); 
    } 
    }); 
}); 
+0

我想這得到它,但它沒有工作,和我編輯我的代碼,檢查它的兄弟 – Mumir

0

我認爲這將有助於

window.scrollTo(0,document.body.scrollHeight); 

,或者如果你想使用jQuery從this thread