Created
April 25, 2010 08:19
-
-
Save mattmccray/378270 to your computer and use it in GitHub Desktop.
platform.js -- Memoize functions base on browser type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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] | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment