Recently I found very strange(in my opinion) window.scrollTo behaviour in Safari(6.0.5 (8536.30.1), MacOS 10.8.4). It seems it works asynchronously.
My task sounds like:
- make some absolute positioned div to be fixed positioned (pin it)
- do some page scroll
- make previously modified div to be absolutely positioned back (unpin it)
So to unpin this div I have to execute unpin routine just after scroll modification is complete. And here I met the problem. Every browser I checked does it correctly except Safari.
Steps to reproduce:
- Open any web page in Safari and make sure it is scrollable at least for 100px and it's initial scroll offset is 0
- Open js console in dev tools
- execute:
window.scrollTo(0, 100); console.log(document.body.scrollTop);
The output is 0. But when I change this code to window.scrollTo(0, 100); window.setTimeout(function() {console.log(document.body.scrollTop)}, 1);
the output is 100, as expected.
Here are all other browsers I've tested(where it works fine):
- Chrome 27.0.1453.110 (MacOS 10.8.4)
- Firefox 21.0 (MacOS 10.8.4)
- Opera 12.15 b1748 (MacOS 10.8.4)
- IE 8.0.7601.17514 (Win7)
Well, as soon as my code sample is not cross browser, it's easier to check this behaviour on any web page with jQuery:
var $w = $(window);
$w.scrollTop(100);
console.log($w.scrollTop());
VS
var $w = $(window);
$w.scrollTop(100);
window.setTimeout(function() {
console.log($w.scrollTop())
}, 1);
Is this behavior is ok or is it a bug? How to handle it? (Now I modified $.fn.scrollTop
to return $.Deferred
instead of chaining and resolve it instantly in main thread in all browsers except Safari).