作者:Gravitymann | 来源:互联网 | 2023-09-10 09:57
RecentlyIfoundverystrange(inmyopinion)window.scrollTobehaviourinSafari(6.0.5(8536.30.1),
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.
最近我发现窗户很奇怪(在我看来)。Safari中的scrollTo行为(6.0.5 (8536.30.1),MacOS 10.8.4)。它似乎是异步运行的。
My task sounds like:
我的任务听起来像:
- make some absolute positioned div to be fixed positioned (pin it)
- 将一些绝对定位div设置为固定位置(pin it)
- do some page scroll
- 做一些页面滚动
- make previously modified div to be absolutely positioned back (unpin it)
- 将先前修改过的div设置为绝对的后置(unpin)
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.
因此,要解除这个div,我必须在完成滚动修改之后执行unpin例程。我遇到了这个问题。除了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
- 打开Safari中的任何网页,确保它至少可以滚动100px,并且它的初始滚动偏移量为0
- Open js console in dev tools
- 在开发工具中打开js控制台
- execute:
window.scrollTo(0, 100); console.log(document.body.scrollTop);
- 执行:窗口。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.
输出是0。但是当我把代码改成窗口时。scrollTo(0,100);window.setTimeout(函数(){ console.log(document.body.scrollTop)},1);输出是100,如预期的那样。
Here are all other browsers I've tested(where it works fine):
以下是我测试过的所有其他浏览器(运行良好):
- Chrome 27.0.1453.110 (MacOS 10.8.4)
- Chrome 27.0.1453.110(MacOS 10.8.4)
- Firefox 21.0 (MacOS 10.8.4)
- Firefox 21.0(MacOS 10.8.4)
- Opera 12.15 b1748 (MacOS 10.8.4)
- 歌剧12.15 b1748 (MacOS 10.8.4)
- IE 8.0.7601.17514 (Win7)
- IE 8.0.7601.17514(这个)
Well, as soon as my code sample is not cross browser, it's easier to check this behaviour on any web page with jQuery:
好吧,只要我的代码示例不是跨浏览器的,就可以用jQuery在任何网页上检查这种行为:
var $w = $(window);
$w.scrollTop(100);
console.log($w.scrollTop());
VS
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).
这种行为是ok的还是bug?如何处理它?(现在我修改.fn美元。scrollTop返回$。在除Safari之外的所有浏览器的主线程中,延迟而不是链接并立即解析它)。
2 个解决方案