Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fixes the jQuery.fn.offset() function in iOS to correct for fixed positioning issues (v3.1-4.0, v4.3-4.3.3)
(function() {
var iOS = parseFloat(
('' + (/CPU.*OS ([0-9_]{1,5})|(CPU like).*AppleWebKit.*Mobile/i.exec(navigator.userAgent) || [0,''])[1])
.replace('undefined', '3_2').replace('_', '.').replace('_', '')
) || FALSE;
if((iOS > 3.1 && iOS < 4.1) || (iOS >= 4.3 && iOS < 4.33)){
$.fn._oldOffset = $.fn.offset;
$.fn.offset = function() {
var result = $.fn._oldOffset.call(this);
result.top -= window.scrollY;
result.left -= window.scrollX;
return result;
}
}
}())
if( parseFloat(((/CPU.+OS ([0-9_]{3}).*AppleWebkit.*Mobile/i.exec(navigator.userAgent)) || [0,'4_2'])[1].replace('_','.')) < 4.1) {
$.fn.Oldoffset = $.fn.offset;
$.fn.offset = function() {
var result = $(this).Oldoffset();
result.top -= window.scrollY;
result.left -= window.scrollX;
return result;
}
}
@Craga89

This comment has been minimized.

Copy link
Owner Author

Craga89 commented Nov 3, 2010

Test against several iPad/iPhone strings:

Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B405 Safari/531.21.10
Mozilla/5.0 (iPad; U; CPU OS OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10
Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10
Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10
Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; de-de) AppleWebKit/528.18 (KHTML, like Gecko) Mobile/7D11
Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.20 (KHTML, like Gecko) Mobile/7B298g
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; en-us) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10
Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10
Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) version/4.0.4 Mobile/7B367 Safari/531.21.10
Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10

@ChristianOellers

This comment has been minimized.

Copy link

ChristianOellers commented May 27, 2015

Fix update

The current version of iOS (~8.x) has the issues too, so we could fix it by updating the snippet:

if (   (iOS >  3.1 && iOS < 4.1)
    || (iOS >= 4.3 && iOS < 4.33)
    || (iOS >= 8   && iOS < 8.4)
    ) {
    // Not sure if it will be fixed by 8.4, we keep an eye on it ...
}

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.