Skip to content
Permalink
Browse files

Mobile WebKit browsers don't support accessing the scroll position of…

… the document/window.
  • Loading branch information
jeresig committed Mar 25, 2010
1 parent 298c81a commit dab1d7467789c7092366f8231dceee0dc20d2553
Showing with 28 additions and 7 deletions.
  1. +28 −7 test/unit/offset.js
@@ -1,5 +1,7 @@
module("offset");

var supportsScroll = false;

testoffset("absolute"/* in iframe */, function($, iframe) {
expect(4);

@@ -10,6 +12,12 @@ testoffset("absolute"/* in iframe */, function($, iframe) {
// if the offset method is using the scroll offset
// of the parent window
var forceScroll = jQuery('<div>', { width: 2000, height: 2000 }).appendTo('body');
window.scrollTo(200, 200);

if ( document.documentElement.scrollTop || document.body.scrollTop ) {
supportsScroll = true;
}

window.scrollTo(1, 1);

// get offset
@@ -245,8 +253,13 @@ testoffset("fixed", function( jQuery ) {
{ id: '#fixed-1', top: 1001, left: 1001 },
{ id: '#fixed-2', top: 1021, left: 1021 }
];

jQuery.each( tests, function() {
if ( jQuery.offset.supportsFixedPosition ) {
if ( !supportsScroll ) {
ok( true, "Browser doesn't support scroll position." );
ok( true, "Browser doesn't support scroll position." );

} else if ( jQuery.offset.supportsFixedPosition ) {
equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset().top" );
equals( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset().left" );
} else {
@@ -324,12 +337,20 @@ testoffset("scroll", function( jQuery, win ) {
// equals( jQuery('body').scrollLeft(), 0, "jQuery('body').scrollTop()" );

win.name = "test";

equals( jQuery(win).scrollTop(), 1000, "jQuery(window).scrollTop()" );
equals( jQuery(win).scrollLeft(), 1000, "jQuery(window).scrollLeft()" );

equals( jQuery(win.document).scrollTop(), 1000, "jQuery(document).scrollTop()" );
equals( jQuery(win.document).scrollLeft(), 1000, "jQuery(document).scrollLeft()" );

if ( !supportsScroll ) {
ok( true, "Browser doesn't support scroll position." );
ok( true, "Browser doesn't support scroll position." );

ok( true, "Browser doesn't support scroll position." );
ok( true, "Browser doesn't support scroll position." );
} else {
equals( jQuery(win).scrollTop(), 1000, "jQuery(window).scrollTop()" );
equals( jQuery(win).scrollLeft(), 1000, "jQuery(window).scrollLeft()" );

equals( jQuery(win.document).scrollTop(), 1000, "jQuery(document).scrollTop()" );
equals( jQuery(win.document).scrollLeft(), 1000, "jQuery(document).scrollLeft()" );
}

// test jQuery using parent window/document
// jQuery reference here is in the iframe

5 comments on commit dab1d74

@jdalton

This comment has been minimized.

Copy link
Member

@jdalton jdalton replied Mar 25, 2010

wonder if this is the issue of what it considers the scrolling element. Like for example some Opera versions dig the body for scrolling others dig the docEl

@jeresig

This comment has been minimized.

Copy link
Member Author

@jeresig jeresig replied Mar 25, 2010

@jdalton: Doesn't appear to be. Searching around the 'net yields no known solution (including a Scriptaculous ticket that's been open since '07). Naturally, if a solution is known it would be greatly appreciated.

@darwin

This comment has been minimized.

Copy link

@darwin darwin replied Mar 25, 2010

Hmm, don't know the details, but window.scrollX / window.scrollY worked for me on iPhone (real device) to read current position of 'viewport' (ok, it is not the same thing as scroll position in classic html page, but can be treated as it if you need to simulate position:fixed on mobile safari).

@jeresig

This comment has been minimized.

Copy link
Member Author

@jeresig jeresig replied Mar 25, 2010

@darwin: I did some additional testing. It looks like those properties work on the iPhone when you manually scroll - but not on the Palm Pre. And if you call window.scrollTo(200, 200); window.scrollX and window.scrollY will still be 0. I'll see if I can check into some other workarounds but this will be a real bear to unit test.

@darwin

This comment has been minimized.

Copy link

@darwin darwin replied Mar 25, 2010

@jeresig: yep, window.scrollTo() not working does not surprise me. Because on mobile safari you have the notion of "viewport" which is decoupled from classic document scrolling. Document scrolling has no effect because page thinks window size is equal to rendered document size. But viewport may be smaller and floats on top of the document/window. What about setting window.scrollY = 200;?

Please sign in to comment.
You can’t perform that action at this time.