1

Im binding an event handler that does an if check to find the presence of an element in the viewport of screen and adjust width of 4-5 elements.

Im not really sure how scroll event works in javascript. But my gut feeling is that, scroll event is fired for even the 1px scroll we do -- in which case scroll sounds more like a continuous event once we start scrolling fast(correct me if Im wrong here). If thats the case, wouldnt calculating viewport and checking the presence of an element inside it and doing dom manipulation would be too much of hit on the performance?

Please help me understand how the scroll event works if my assumption is wrong here!

Thanks.

Ivin
  • 4,435
  • 8
  • 46
  • 65
  • 2
    You're correct. It's best to wait until the user stops scrolling, using a timeout. See http://stackoverflow.com/questions/4289473/javascript-do-an-action-after-user-is-done-scrolling – Barmar Jul 02 '13 at 06:13
  • Another [debouncer](http://stackoverflow.com/a/4298672/1169519). – Teemu Jul 02 '13 at 06:17
  • Mobile you will find enforces this naturally and so it is only when you stop scrolling that the event registers. This is I understand to enable the efficient scrolling of long pages. I don't think the performance hit will be too great unless you have a very critical app or webpage. Adding a timeout is indeed adding another process that you will later need to kill and if its helping I would say might be better might not. You can run some tests or just do it and if its a problem then revisit. – landed Mar 24 '14 at 22:55

0 Answers0