javascript window.scrollTo 不适用于手机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11845371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.scrollTo is not working in mobile phones
提问by Safari
on my mobile view of my webpage i can scroll in vertical and horizontal direction, but this starts always at the top left corner. Now i want to set the viewport to my custom position with window.scrollTo or something equivalent. window.scroll seem just to work for desktop browser.
在我的网页的移动视图中,我可以在垂直和水平方向滚动,但这总是从左上角开始。现在我想使用 window.scrollTo 或等效的东西将视口设置为我的自定义位置。window.scroll 似乎只适用于桌面浏览器。
any idea how i could solve that?
知道我该如何解决吗?
采纳答案by Safari
I got it finally working.
我终于让它工作了。
i had to use additionally the setTimeout function
我不得不另外使用 setTimeout 函数
setTimeout(window.scrollTo(x,y),100);
回答by Torsten Walter
As I said in my comment, this works if your content is large enough. This means larger than the viewport. Without knowing specifics, did you look into setting the viewport meta tag?
正如我在评论中所说,如果您的内容足够大,这会起作用。这意味着大于视口。在不知道具体细节的情况下,您是否考虑过设置视口元标记?
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Now, if your content div or image or whatever exceeds the size of the viewport width (320 dips on iPhone) you can scroll on the x axis. The same is true for the y axis with different values though.
现在,如果您的内容 div 或图像或任何超出视口宽度的大小(iPhone 上的 320 倾角),您可以在 x 轴上滚动。但是,对于具有不同值的 y 轴也是如此。