Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery计算百分比,最大值为100%_Jquery - Fatal编程技术网

jquery计算百分比,最大值为100%

jquery计算百分比,最大值为100%,jquery,Jquery,简单进度条,显示用户向下滚动页面特定部分的百分比,在本例中,文章页面(.post)的主体在到达文章结尾/注释开头时达到100% $(window).scroll(function() { var conheight = $('.post').height(); var startDistance = 0; var scrollTop = $(document).scrollTop(); var scrollAmount = $(window).scrollTop(

简单进度条,显示用户向下滚动页面特定部分的百分比,在本例中,文章页面(.post)的主体在到达文章结尾/注释开头时达到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);
    }

});
问题是,尽管在到达.post结尾时它达到了100%,但随着注释部分的滚动,它会不断扩展


如何将scrollPercent限制为100%作为最大值?

所以只有在百分比小于100%时才进行滚动?啊,是的,我想这是最简单的方法。我想Occam剃须刀。。。
// 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;
}