cferdinandi / smooth-scroll

A lightweight script to animate scrolling to anchor links.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Infinite loop FireFox when scrolling to top

willemliu opened this issue · comments

Hi,

I've encountered a problem in FireFox caused by a line of code. Line 86 in smooth-scroll.js present in commit: ad413a4 and probably some earlier commits as well.

The following IF-construction causes an infinite loop if you placed an anchor at the top of the page and you want to scroll to that anchor.

if ( travelled <= (endLocation(anchor) || 0) )

When looking into that if statement it's actually expected behaviour for it to cause an infinite loop. ((endLocation(anchor) || 0)) will actually be "false" when the anchor is at position 0 or smaller. So the body of the IF statement will only be executed when "travelled" is smaller or equals "false" which most of the time is never.
And as a result of the use of easing functions "travelled" will most likely never be exactly 0.

You can replace that with the following line to fix the bug.

if ( travelled <= (endLocation(anchor)) || travelled <= 0 )

Regards,

Willem

@willemliu - Is there a way for me to test this? I've used it to successfully scroll to the top of the page before. If there were an infinite loop, wouldn't it either:

  1. Not scroll, or
  2. Stick at the top and not allow you to do anything else?

I've not encountered either of those.

I had this problem on my not yet complete website. My site using the Zurb Foundation responsive framework. So it's hard to pinpoint what combination of code would exactly cause the bug to appear.
What happens is what you mention at point 2. The site will relentlessly keep scrolling to the top. So I can't scroll down anymore.
I've been tracing the distance parameter and it becomes a value smaller than 0. From what I can see is that it happens when distance is 0 or smaller. And it only happens in FireFox. Other browsers seem to break out of the infinite loop.

I'll try to recreate the problem with a simple page tomorrow.

@willemliu - If the code you provided solves the problem for you, no need. I'll make the updates and test later. Thanks!

Yes,

The line of code fixes the bug. Thanks!

Regards,

Willem

Added in version 2.9. Thanks for the bug fix @willemliu!