Get current position of the viewport in Mobile (iPhone) Safari

独自空忆成欢 提交于 2019-12-12 08:22:30

问题


I'm trying to position a div in the center of the iphone viewport. Basically so that when you hit an image, it pops up in the middle of the screen regardless of where you had scrolled to on the page before you clicked.

It's not as simple as just making it position: fixed; as that will cause the origin to be the top of the page, not the top of the viewport.

I also can't do something like detecting scroll events and keeping a track of where I'm currently scrolled to, as the Javascript I'm writing is only injected into the page and executed when the popup is triggered.

Is there an iPhone specific JavaScript API or something I can use to get the current viewport coordinates?

Thanks, Toby


回答1:


Stupidly easy.

var scrollX = window.pageXOffset;
var scrollY = window.pageYOffset;

...gives you the current window position.



来源:https://stackoverflow.com/questions/2256432/get-current-position-of-the-viewport-in-mobile-iphone-safari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!