public
Last active

platform.js -- Memoize functions base on browser type.

  • Download Gist
jquery.anim8.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// (Simple example of using platform.js in a jQuery plugin.)
 
// Usage: $('#block').anim8({ top:50, left:350 }, 250);
$.fn.anim8 = (function($){ return platform({
 
// For Webkit browsers it will use CSS animations
webkit: function(props, speed, transition) {
return this.each(function(){
var elem = $(this),
transition = transition || 'ease-out',
speed = speed ? ((speed / 1000)+'s') : '.25s';
props['-webkit-transition'] = 'all '+ speed +' '+ transition;
elem.css(props);
});
},
 
// For the rest, it'll just use $.fn.animate
other: function(props, speed, transition) {
return this.each(function(){
var elem = $(this);
elem.animate(props, speed, transition);
})
}
 
})})(jQuery);
platform.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/**
 
Good for memoing functions based on browser type.
 
Example:
 
platform({
webkit: function(){
// If it's webkit, this function is returned...
},
mozilla: function() {
// If it's mozilla (Firefox)...
},
opera: function() {
// If it's opera...
},
msie: function() {
// If it's ie
},
msie6: function() {
// If it's ie version 6
},
other: function() {
// If a brower type isn't found that matches, use this one...
}
});
*/
 
function platform(funcs) {
var b = platform.browser, tv = b.type_version, t = b.type;
return funcs[tv] || funcs[t] || funcs['other'] || function(){ /*SHOULD THIS THROW AN ERROR? */ };
}
 
platform.browser = (function(){ // "Borrowed" from jQuery
var ua = navigator.userAgent.toLowerCase(),
match = /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
/(opera)(?:.*version)?[ \/]([\w.]+)/.exec( ua ) ||
/(msie) ([\w.]+)/.exec( ua ) ||
!/compatible/.test( ua ) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec( ua ) ||
['','other', '0'];
return {
type: match[1],
version: parseFloat(match[2]),
type_version: match[1]+match[2]
};
})();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.