简体   繁体   English

Mobile Safari:为什么window.scrollTo(0,0)不能滚动到(0,0)?

[英]Mobile Safari: why is window.scrollTo(0, 0) not scrolling to (0, 0)?

I've built a small single-page web app using Bootstrap 3, Sammy.js and Knockout 3. I'm finding that when the page is scrolled down, I'm unable to get window.scrollTo(0, 0) to work on Mobile Safari when I also change location.hash - specifically on iOS 7 on an iPhone 5C (likely happens on other iPhone models though). 我使用Bootstrap 3,Sammy.js和Knockout 3构建了一个小型单页Web应用程序。我发现当页面向下滚动时,我无法获得window.scrollTo(0, 0)工作在Mobile Safari上我也改变了location.hash - 特别是在iPhone 5C上的iOS 7上(虽然可能在其他iPhone机型上发生)。

When I try something like this: 当我尝试这样的事情时:

self.changeState = function() {
    self.state(State.NewState);
    location.hash = 'somevalue';
    window.scrollTo(0, 0);
};

The page will scroll almost all the way to the top - it will usually be scrolled to Y = 12 or 13. It will never scroll to 0 if I call window.scrollTo(0, 0); 页面几乎一直滚动到顶部 - 它通常会滚动到Y = 12或13.如果我调用window.scrollTo(0, 0);它将永远不会滚动到0 window.scrollTo(0, 0); when the user is scrolled to the bottom of the page (and the content is sufficiently high to warrant a scroll to top). 当用户滚动到页面底部时(内容足够高以保证滚动到顶部)。

I've tried various StackOverflow answers/suggestions (eg The same old issue, .scrollTop(0) not working in Chrome & Safari ); 我尝试了各种StackOverflow的答案/建议(例如同样的旧问题,.scrollTop(0)在Chrome和Safari中无效 ); the usual remedies (wrap in a setTimeout ) do not change the behavior - it still scrolls to somewhere near the top of the page, but not quite to the top. 通常的补救措施(包装在一个setTimeout )不会改变行为 - 它仍然滚动到靠近页面顶部的某个地方,但不会到达顶部。

Header style: 标题样式:

<div class="navbar navbar-default navbar-inverse navbar-static-top">

Viewport settings: 视口设置:

<meta name="viewport" 
      content="width=device-width, 
               height=device-height, 
               initial-scale=1.0, 
               maximum-scale=1.0, 
               target-densityDpi=device-dpi" />

Scroll attempts: 滚动尝试:

self.changeState = function() {
    self.state(State.NewState);
    location.hash = 'somevalue';
    window.scrollTo(0, 0);
};

and: 和:

self.changeState = function() {
    self.state(State.NewState);
    location.hash = 'somevalue';
    window.setTimeout(function() {
        window.scrollTo(0, 0);
    }, 0);
};

I've dug into this with the Safari Web Inspector, and nothing seems out of place as far as the structure of the page. 我已经使用Safari Web Inspector对此进行了挖掘,就页面结构而言,似乎没有什么不合适的地方。 I'm just not sure what to look for here - any ideas? 我只是不确定在这里寻找什么 - 任何想法?

Edit: 编辑:

So far, I have not been able to reproduce this outside of my app's code. 到目前为止,我还没有能够在我的应用程序代码之外重现这一点。 I'm trying to get this to break in a JSFiddle - in this one, it works correctly (eg, it scrolls to 0,0 as it should): http://jsfiddle.net/rringham/n9evU/24/show . 我试图在JSFiddle中打破这个 - 在这一个,它正常工作(例如,它应该滚动到0,0): http//jsfiddle.net/rringham/n9evU/24/show

In my case, same issue, but it were coming from the fact I was in an iframe. 在我的情况下,同样的问题,但它来自我在iframe中的事实。 So doing window.top.scrollTo(0,0) made it!!!! 所以做window.top.scrollTo(0,0)就成了!!!!

I managed to recreate this issue by using height: 100vh or min-height: 100vh on elements. 我设法通过使用height: 100vhmin-height: 100vh元素重新创建此问题。 I changed my CSS to use 100% instead (which is a bit of a pain) and the problem disappeared. 我改变我的CSS使用100% (这有点痛苦),问题消失了。

Try setting scrollTo(-n, 0); 尝试设置scrollTo(-n,0); "n" being any number lower than 0 “n”是低于0的任何数字

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

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