Skip to content

Instantly share code, notes, and snippets.

@Craga89
Created November 3, 2010 22:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Craga89/661844 to your computer and use it in GitHub Desktop.
Save Craga89/661844 to your computer and use it in GitHub Desktop.
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;
}
}
@ChristianOellers
Copy link

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