Reputation: 9347
I stumbled into this one whilst trying to scroll an element without invoking the normal event handlers
Using both Firefox and IE10 I'm seeing some really strange behaviour in how the scrollTop
method is operating. For example, if I set the scrollTop
on a div
, and aferwards, bind a scroll
event handler to the same element, the handler fires immediately. From my testing, this doesn't happen with Chrome, which leads me to think that FF and IE are applying the most miniscule of animations to their scrolls, or this is some kind of bug.
See JSFiddle example. Interestingly, if I set a timeout of 1ms before the assignment, the problem goes away. I'd love to know what's going on here, and what the best approach is to fix it.
Update: From the comments below it seems as though this might be recognised to be normal browser behaviour, so I'll update my question to ask what is going on here - please cite some interesting articles which explain this process in more detail.
Upvotes: 8
Views: 2500
Reputation: 4564
What goes on in IE and FF is the following:
This is unlike this code:
var dv = document.getElementsByTagName("div")[0];
dv.scrollTop = dv.scrollHeight;
setTimeout(function(){
dv.onscroll = function() {
console.log("scrolled!");
}, 0);
Where the following happpens:
More information on the subject can be found here and here.
Upvotes: 9