Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Progress Bar Value based on Scrolling

I would like to be able to make my progress bar increase, based on how far I've scrolled and how much is left.

I've tried this: jsFiddle and it doesn't seem to work, I based my script off someone's script that made a block move horizontally based on scroll %.

My code:

<progress id="progressbar" value="0" max="100"></progress>
<br />
<br />
<br />
Lorem<br />
Ipsum<br />
Dolor<br />
.
.
.
.

JS:

$(document).ready(function () {
    $(window).scroll(function () {
        var s = $(this).scrollTop(),
            d = $(document).height(),
            scrollPercent = (s / d);
        var position = (scrollPercent);
        $("#progressbar").progressbar('value', position);
    });
});
like image 914
hakarune Avatar asked Oct 31 '13 06:10

hakarune


People also ask

How do I move the progress bar in HTML?

To move your survey's Progress Bar, head over to the Style tab of your survey and scroll to the bottom of the survey preview to access the link for the HTML/CSS Editor. On the Custom HTML tab scroll to the very bottom of the HTML and find the following code: [template("progress bar")].

How do I add a page to my progress bar?

Click Add-ons > ProgressBar > Show Progress Bar.Click Continue.

How do you make a progress chart in HTML?

Use the <progress> tag to create a progress bar in HTML. The HTML <progress> tag specifies a completion progress of a task. It is displayed as a progress bar. The value of progress bar can be manipulated by JavaScript.


2 Answers

The logic is like this

totalValue  = (documentHeight - windowHeight);
currntValue = scrolledValue;
percentage =  (currntValue/ totalValue  ) * 100

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);
     });
 });
like image 147
Sarath Avatar answered Oct 08 '22 01:10

Sarath


Working DEMO

Try this

$(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);

});

Hope this helps, Thank you

like image 30
SarathSprakash Avatar answered Oct 08 '22 03:10

SarathSprakash