How to find (in javascript) the current “scroll” offset in mobile safari / iphone

▼魔方 西西 提交于 2020-01-10 08:50:48

问题


I'd like to know the x/y offset of the how far the user has "scrolled" within the viewport in mobile safari on the iphone.

Put another way, if I (through javascript) reloaded the current page, I'd like to find the values I'd need to pass into window.scrollTo(...) in order to reposition the document/viewport as it is currently.

window.pageXOffset always reports 0

jquery's $('body').scrollTop() always reports 0

events have a pageX, but this won't account for the scrolling of the page that happens after you release your finger if your gesture was to "flick" the page up/down. Namely, it'll give me a point when the finger leaves the screen, but that doesn't always match where the page will be after it's finished scrolling.

Any pointers?


回答1:


window.pageYOffset should give you the current scroll offset. There's window.pageXOffset if you need it too.




回答2:


This will indeed work:

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

If you are viewing content in an iFrame (which is common in WebViews for instance), then you will need to add parent:

var scrollX = parent.window.pageXOffset;

Also note that these values are not writeable. So to change the scroll position, you will need to use window.scrollTo method:

var scrollX = window.pageXOffset; 
var scrollY = window.pageYOffset;
window.scrollTo(scrollX -100, scrollY -100);



回答3:


I had the same problem... this did the trick:

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

Got it from this question: Get current position of the viewport in Mobile (iPhone) Safari




回答4:


Here is a simple code to find if the device is iphone and also to change the scroll position to specific position based on some action. I had an issue with iphone only when I click and open an image as a popup, because vertical scroll goes down than where I wanted to be. So I wrote this code and it solved the issue.

if((navigator.userAgent.match(/iPhone|iPad|iPod/i))){
    ('#tutorial-landlord').click(function(){
        window.scroll(100, 1500); // x, y (horizontal, vertical position) 
    });
}

To find the scroll position. Just go to console in chrome and write window.scrollY for vertical and see how the position changes so note that number and give it in place of x and y




回答5:


Have you tried the pure js way?

document.body.scrollTop



回答6:


I had the same issue on iPad. Just desactivate the console. The viewport height changes when the console is open in Safari.




回答7:


If for whatever reason pageXOffset and pageYOffset fail, the solution is straightforward, but rather silly:

// force an element to top left of the page
var topLeftMarker = document.createElement("span");
topLeftMarker.style.position = "absolute";
topLeftMarker.style.left = "0";
topLeftMarker.style.top = "0";

document.body.appendChild(topLeftMarker)

function scrollOffset() {
    // getBoundingClientRect() returns the rectangle of the element in viewport space,
    // which *is* scrollLeft and scrollTop 
    const rect = topLeftMarker.getBoundingClientRect();
    return { x: rect.left, y: rect.top }
}



回答8:


Hey ! Try to go through this link.

or try following code.

[myWebView stringByEvaluatingJavaScriptFromString: @"scrollY"];

actually - I got the answer from this question.



来源:https://stackoverflow.com/questions/2506958/how-to-find-in-javascript-the-current-scroll-offset-in-mobile-safari-iphon

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