views:

29

answers:

1
 scroller1   scroller2
----------- -----------
|this is o| |this is a|
|ne elemen| |nother el|
|t, four l| |3 lines  |
|ines     | |         |
----------- -----------

That is a simple example set up. I have set up a system which will scroll to the nth element in each scroller, but also using fractions so that if scroller1 is at element 4 and halfway to five it will do the same for scroller2.

But when I call the function 'onscroll' it sets the other scroller to the correct positions, but then calls 'onscroll' because changing its scrollTop is scrolling. This in turn repeats itself until Javascript throws the Max Stack Call error, or both scrollers reach the bottom.

How can this be prevented?

+2  A: 

Create a global variable, currentlyHandlingScrollEvent, and conditionalize your onscroll code based on it:

var currentlyHandlingScrollEvent = false;
function handleScroll (e) {
  if (!currentlyHandlingScrollEvent) {
    currentlyHandlingScrollEvent = true;
    // do stuff ...
    currentlyHandlingScrollEvent = false;
  }
}

scroller1.onscroll = handleScroll;
scroller2.onscroll = handleScroll;
Brian Campbell
Works great, thanks!Took me a while to figure out how to change that variable when scrolling stopped, but it works flawlessly now.
Tom
Oops. Edited my answer to reset the variable to its initial value. Sorry for the mistake.
Brian Campbell
Actually, turns out this only works in Safari.Other browsers including Firefox revert to the old behavior... and this is killing me.Check it out @ http://www.tombarrasso.com/search/
Tom
That site doesn't seem to be working at all in Firefox, and I can't find the synchronized scroll areas you are talking about in Safari. Can you post a stripped down example of just what is necessary to demonstrate your problem?
Brian Campbell
Nevermind, I forgot I had noscript turned on in Firefox. I see the bug now, but it might still help for you to turn this into a minimal example demonstrating the problem you have, reduced from the fairly large amount of code and content you have on that page.
Brian Campbell