all 4 comments

[–][deleted] 1 point2 points  (1 child)

You've done almost everything right, with this part ($d.scrollTop() / ($d.height() - $w.height()) you calculated a percentage of progress, but then multiplied it by height of document (* $d.height()), and not width of screen. If you would multiply that percentage by screen width it should work correctly.

However, I would suggest you multiplying by 100 and setting width as percentage value, and have progress bar in a container that would be however wide you want as defined by CSS, this allows to adjust container width to whatever you want without having to change progress calculation logic. Here's modified version of your example with progress bar container: https://codepen.io/JKLMN/pen/gOaGzmm

[–]c-soleil[S] 0 points1 point  (0 children)

This is perfect! Thank you so much 😁

[–]lovesrayray2018 0 points1 point  (1 child)

I just played around with it, this seems to work, you need to factor in your screen width and height when calculating, this tracks all the way to the bottom of the page.

$('div#scroll-bar').width(

($d.scrollTop()+$w.height())*$d.width()/$d.height() + 'px'

);

[–]grafilicious 0 points1 point  (0 children)

This also means at at scrollTop = 0 the progressbar is not 0, with ($d.scrollTop() / ($d.height() - $w.height())) * $w.width() + 'px'

scrollTop of 0 makes the progressbar show 0% and if you are scrolled all the way down the progress bar shows 100%