根据滚动更改进度条值

hak*_*une 8 javascript jquery scroll scrollbar progress-bar

我希望能够使我的进度条增加的基础上,如何到目前为止,我已经滚动和剩下多少.

我试过这样:的jsfiddle,它似乎没有工作,我根据我的脚本断别人的脚本,制作水平块移动基于滚动%.

我的代码:

<progress id="progressbar" value="0" max="100"></progress>
<br />
<br />
<br />
Lorem<br />
Ipsum<br />
Dolor<br />
.
.
.
.
Run Code Online (Sandbox Code Playgroud)

JS:

$(document).ready(function () {
    $(window).scroll(function () {
        var s = $(this).scrollTop(),
            d = $(document).height(),
            scrollPercent = (s / d);
        var position = (scrollPercent);
        $("#progressbar").progressbar('value', position);
    });
});
Run Code Online (Sandbox Code Playgroud)

Sar*_*ash 19

工作演示

试试这个

$(window).scroll(function () {
  var s = $(window).scrollTop(),
        d = $(document).height(),
        c = $(window).height();
        scrollPercent = (s / (d-c)) * 100;
        var position = scrollPercent;

   $("#progressbar").attr('value', position);

});
Run Code Online (Sandbox Code Playgroud)

希望这有帮助,谢谢

  • 这太棒了,甜蜜! (2认同)

Sar*_*ath 5

逻辑是这样的

totalValue  = (documentHeight - windowHeight);
currntValue = scrolledValue;
percentage =  (currntValue/ totalValue  ) * 100
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/PvVdq/71/

   $(document).ready(function () {
    $(window).scroll(function () {
        var s = $(this).scrollTop(),
            d = $(document).height()-$(window).height(),
            scrollPercent = (s / d)*100;       
        $("#progressbar").attr('value', scrollPercent);
     });
 });
Run Code Online (Sandbox Code Playgroud)