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
// Simple example of using platform in a jQuery plugin:
 
// $('#block').anim8({ top:50, left:350 }, 250);
 
// For Webkit browsers it will use CSS animations, $.fn.animate for other browsers
$.fn.anim8 = (function($){
return platform({
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);
});
},
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 47
/**
 
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;
var tv = platform.browser.type_version, t = platform.browser.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.