DEV Community


Discussion on: Make a parallax effect with 10 lines of JavaScript

alexparra profile image
Alex Parra

As a performance improvement, move the document.querySelector calls outside the scroll event listener, assigned to variables. That way, the browser only has to traverse the DOM once instead of on every scroll event which is what’s happening now. And then, look into throttle to limit the handler calls as scroll fires at a very high rate.

ibrahima92 profile image
Ibrahima Ndaw Author

You're absolutely right. I always use selectors in that way. For a real apps, i should control the scroll with a debounce function, but for this quick may be it's not mandatory. So thanks again for your very useful comment.