简体   繁体   English

在iOS Safari上,window.scrollTo在方向更改后不起作用

[英]On iOS Safari, window.scrollTo doesn't work after orientation change

In short: 简而言之:

In iOS Safari, window.scrollTo method doesn't do anything if called from orientationchange event handler. 在iOS Safari中,如果从orientationchange事件处理程序调用,window.scrollTo方法不会执行任何操作。 It seems like a certain amount of time (500-1000ms) must pass before you can modify scroll position after orientation change. 在方向更改后修改滚动位置之前,似乎必须经过一定的时间(500-1000ms)。

Is there a workaround to change scroll position immediately and avoid the problem when user can see old scroll position for a moment after orientation change? 是否有一种解决方法可以立即更改滚动位置,并避免在用户可以在方向更改后看到旧滚动位置时出现问题?

Detailed problem description: 详细问题描述:

I need to implement the following feature for mobile browsers: 我需要为移动浏览器实现以下功能:

When the user switches to landscape mode, he should see fullscreen video. 当用户切换到横向模式时,他应该看到全屏视频。 When he switches back to portrait, he should be returned to exact same place where he left off. 当他切换回肖像时,他应该被送回他离开时的同一个地方。

The second part is the problem. 第二部分是问题所在。 Both iOS and Android will keep scroll position if you switch orientation back and forth, but only if you dont scroll the screen and dont make any adjustments to DOM. 如果你来回切换方向,iOS和Android都会保持滚动位置,但前提是你不滚动屏幕而不对DOM进行任何调整。 So if you just switch from portrait to landscape and back, everything works as expected. 因此,如果您只是从纵向切换到横向和背面,一切都按预期工作。 If you switch from portrait to landscape, adjust scroll position even by 1 pixel or make any changes to DOM, you will return to a different scroll position. 如果您从纵向切换到横向,即使按1像素调整滚动位置或对DOM进行任何更改,您将返回到不同的滚动位置。

So I'm trying to pragmatically restore scroll position once the user returns to portrait orientation. 因此,当用户返回纵向时,我正在尝试以实际方式恢复滚动位置。 Here's the simplified code I use: 这是我使用的简化代码:

    var scrollPosition;
    var savedScrollPosition;

    window.addEventListener('scroll', function() {
        scrollPosition = window.scrollY;
    });

    window.addEventListener('orientationchange', function(event) {
        if (Math.abs(window.orientation) === 90) {
            // This line will correctly save last scroll position for portrait orientation
            savedScrollPosition = scrollPosition;
        } else {
            // This line will correctly try to restore previously saved scroll position
            window.scrollTo(0, savedScrollPosition);
        }
    });

This works on android, but on iOS it doesn't. 这适用于Android,但在iOS上却没有。 The problem is, window.scrollTo just doesn't seem to do anything until the certain time after orientation change has passed. 问题是,window.scrollTo似乎没有做任何事情,直到定向更改过去后的某个时间。

So if I change 所以,如果我改变

window.scrollTo(0, savedScrollPosition);

to

setTimeout(function() {
    window.scrollTo(0, savedScrollPosition);
}, 1000);

it works on iOS, but the user can see wrong portion of the page for a few moments, which leads to a poor user experience. 它适用于iOS,但用户可能会在一段时间内看到页面的错误部分,从而导致糟糕的用户体验。

I was hoping that somebody knows a way to change scroll position on iOS immediately after orientationchage event. 我希望有人知道在orientationchage事件之后立即改变iOS上滚动位置的方法。

Thank you. 谢谢。

In the end, I was forced to go with the following code: 最后,我被迫使用以下代码:

var scrollPosition;
var savedScrollPosition;

window.addEventListener('scroll', function() {
    scrollPosition = window.scrollY;
});

window.addEventListener('orientationchange', function(event) {
    if (Math.abs(window.orientation) === 90) {
        savedScrollPosition = scrollPosition;
    } else {
        if (isIOS()) {
            var retryScroll = setInterval(function () {
                if (window.scrollX === 0 && window.scrollY == savedScrollPosition) {
                    clearInterval(retryScroll);
                } else {
                    window.scrollTo(0, savedScrollPosition);
                }
            }, 10 );
        } else {
            window.scrollTo(0, savedScrollPosition);
        }
    }
});

The user will see a small visual glitch, but it's still the best solution. 用户会看到一个小的视觉故障,但它仍然是最好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM