2013-11-22 69 views
0

簡單的進度條顯示用戶向下滾動頁面的特定部分的百分比,在這種情況下,文章頁面(.post)的主體正在向下滾動文章的結尾/評論的開始已經達到。jquery計算百分比,最大值爲100%

$(window).scroll(function() { 
    var conheight = $('.post').height(); 
    var startDistance = 0; 
    var scrollTop = $(document).scrollTop(); 
    var scrollAmount = $(window).scrollTop(); 
    var scrollArea = $('.commentswrapper').offset().top - $('.post').offset().top; 


    // calculate the percentage the user has scrolled down the page and substract comment section 
    var scrollPercent = 100 * $(window).scrollTop()/(scrollArea); 

    if (scrollTop > 0) { 
     $('.progress-bar').css('width', scrollPercent +"%" ); 
    } else { 
     $('.progress-bar').css('width', startDistance); 
    } 

}); 

的問題是,雖然它達到100%內。員額結束的時候,它一直作爲註釋部分滾動擴大。

如何將scrollPercent限制爲最大100%?

+1

所以只有滾動的東西,如果%小於100%? – Huangism

+0

嗯,我想這是最簡單的方法。 – andy

+0

奧卡姆的剃鬚刀我認爲... – Huangism

回答

0
// calculate the percentage the user has scrolled down the page and substract comment section 
var scrollPercent = 100 * $(window).scrollTop()/(scrollArea); 

if (scrollPercent > 100) { 
    scrollPercent = 100; 
}