I have a type of progress tracker sitting fixed on the side of my page and as you scroll, I want the line to gain height (in percentages) as the user scrolls down the page. I am having trouble getting the height to increase by just one as the user scrolls. Here is my current code.
JS
$(window).scroll(function(){
var number = 0; /* I'd like to increment this by one on scroll */
// Only start the process when the first section comes into view
if($("#progress-tracker-nav li").hasClass('active-section')) {
number++; /* I'd like to increment this by one on scroll */
$(".progress-line").css('height', number + '%');
}
})
You have to declare
number
variable outside of thescroll
event handler, because every time whenscroll
event is fired, the variable value isrefreshed
.In your current code, you assign every time
0
value fornumber
variable.The problem is that you define
number
inside the scroll event. You will need to define it outside in order for the amount to be incremented.The current way you are doing it means
number
is reset to 0 each time the event fires.I think the height of the progress bar should be relative to the
scrollTop
of the window divided by the maximum scroll possible (the height of the document minus the height of the window). Using this formula:Example:
To increase the number on scroll down, you can do
Likewise, you can replace the
>
to<
if you need to reduce the number when user scroll up.If you want to reduce the height on scroll up and increase the height on scroll down, you can do this as well.
These events will fire VERY frequently, so you might want to move your selectors into a closure so they don't have to be recalculated each time:
Also, be aware of issues on IOS related to scroll events only firing after the scrolling completes:
https://github.com/twbs/bootstrap/issues/16202
Also, not sure how you are planning on resetting. The scroll events will fire whether scrolling up or down so the number is just going to keep increasing. Is that what you want?