Due to a bug in the native `requestAnimationFrame` function running inside `<iframe>` elements on iOS 6, I've decided to write up a custom function that implements a blacklist. Use this snippet and use `nextFrame` and `cancelFrame` on the `window` object to use this workaround with the same method signature as the spec. I've avoided overriding t…
// requestAnimationFrame implementation as a custom function to allow blacklisting | |
// devices with broken implementation. Currently needs timer-based fallbacks for iOS 6.x for | |
// code running inside <iframe> elements. | |
// Uses polyfills based on http://paulirish.com/2011/requestanimationframe-for-smart-animating/ | |
(function() { | |
var blacklisted = /iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent), | |
lastTime = 0, | |
nativeRequest = window.requestAnimationFrame || null, | |
nativeCancel = window.cancelAnimationFrame || null; | |
['webkit', 'moz'].forEach(function(prefix) { | |
nativeRequest = nativeRequest || window[prefix+'RequestAnimationFrame'] || null; | |
nativeCancel = nativeCancel || window[prefix+'CancelAnimationFrame'] || window[prefix+'CancelRequestAnimationFrame'] || null; | |
}); | |
function polyfillRequest(callback, element) { | |
var currTime = new Date().getTime(); | |
var timeToCall = Math.max(0, 16 - (currTime - lastTime)); | |
var id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall); | |
lastTime = currTime + timeToCall; | |
return id; | |
} | |
function polyfillCancel(id) { | |
clearTimeout(id); | |
} | |
this.nextFrame = (!blacklisted && nativeRequest != null) ? nativeRequest : polyfillRequest; | |
this.cancelFrame = (!blacklisted && nativeCancel != null) ? nativeCancel : polyfillCancel; | |
})(); |
This comment has been minimized.
This comment has been minimized.
Thank you so much. |
This comment has been minimized.
This comment has been minimized.
Can't you feature test it instead of version sniff? f.e. request once, if called back - then it works? |
This comment has been minimized.
This comment has been minimized.
I have found a workaround: using top.requestAnimationFrame. Does it works for you guys? I mean within an iframe. |
This comment has been minimized.
This comment has been minimized.
A better ios fix https://github.com/kof/animationFrame |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
I can confirm this fixed a bug on ios6 where raf would not run on subsequent pages after being invoked on one page. (no iframe, just simple page to page navigation) Same symptoms with the address bar as you have described (click on url bar then back to page and raf runs ok). I was surprised how little info came up on this. Thanks for the code.