Skip to content

Instantly share code, notes, and snippets.

@nikolowry
Created March 30, 2015 16:54
Show Gist options
  • Save nikolowry/2639732262ddd8407be6 to your computer and use it in GitHub Desktop.
Save nikolowry/2639732262ddd8407be6 to your computer and use it in GitHub Desktop.
Zepto.polyfills.js
/*
* Sick of using jQuery - but still want to utilize it's selector engine
* and third-party plugins? Me too!
*
* Make sure you add Zepto's source first from http://zeptojs.com/.
*
* If you only need the minified version of Zepto and are using bower you can install it by:
* $ bower install http://zeptojs.com/zepto.min.js --save
*
* I will document with comments what specific methods are intended for below.
*/
//*---------------------------------------
// Global - Non Library Specific
//jQuery catchall
window.jQuery = Zepto;
//data function
$.data = function(elem, key, value){ return $(elem).data(key, value);}
//*---------------------------------------
// Needed by Slick.js
//css psuedo selectors
$.expr = {':': {}};
//outerWidth && outerHeight
['width', 'height'].forEach(function(dimension) {
var offset, Dimension = dimension.replace(/./, function(m) { return m[0].toUpperCase() });
$.fn['outer' + Dimension] = function(margin) {
var elem = this;
if (elem) {
var size = elem[dimension]();
var sides = {'width': ['left', 'right'], 'height': ['top', 'bottom']};
sides[dimension].forEach(function(side) {
if (margin) size += parseInt(elem.css('margin-' + side), 10);
});
return size;
} else {
return null;
}
};
});
//Override Zepto's broken proxy
$.proxy = function( fn, context ) {
if ( typeof context === "string" ) {
var tmp = fn[ context ];
context = fn;
fn = tmp;
}
if (!$.isFunction(fn)) return undefined;
var args = Array.prototype.slice.call( arguments, 2 ),
proxy = function() {
return fn.apply( context, args.concat( Array.prototype.slice.call( arguments ) ) );
};
proxy.guid = fn.guid = fn.guid || proxy.guid || $.guid++;
return proxy;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment