Created
February 18, 2015 19:22
-
-
Save pgarciacamou/d3e79ed28a08a352f616 to your computer and use it in GitHub Desktop.
Mini jQuery esque selector.
This file contains hidden or 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
function elem(selector){ return document.querySelectorAll(selector); } | |
Object.defineProperties(NodeList.prototype, { | |
'each': { | |
value: function(fn){ | |
var self = this; | |
Array.prototype.slice.call(this, 0).forEach(function (){ | |
fn.apply(arguments[0], arguments); | |
}); | |
return self; | |
} | |
} | |
// ELEMENT TRANSFORMATIONS | |
,'rotate': { | |
value: function (deg){ | |
this.each(function (element, index){ | |
element.style.webkitTransform = element.style.transform = "rotate(" + deg + "deg)"; | |
}); | |
} | |
} | |
// CLASSES | |
,'removeClass': { | |
value: function (_class, time){ | |
_class = _class.split(' '); | |
this.each(function (element, index){ | |
if(time) { | |
setTimeout(function(){ | |
_class.forEach(function (c) { | |
element.classList.remove(c); | |
}); | |
}, time); | |
} else _class.forEach(function (c) { | |
element.classList.remove(c); | |
}); | |
}); | |
return this; | |
} | |
} | |
,'addClass': { | |
value: function (_class, async, time){ | |
_class = _class.split(' '); | |
this.each(function (element, index){ | |
if(async){ | |
setTimeout(function (){ | |
_class.forEach(function (c) { | |
element.classList.add(c); | |
}); | |
}, time || 0); | |
} else _class.forEach(function (c) { | |
element.classList.add(c); | |
}); | |
}); | |
return this; | |
} | |
} | |
,'prop': { | |
value: function(propName){ | |
return this[0][propName]; | |
} | |
} | |
,'data': { | |
value: function(propName){ | |
return this[0].dataset[propName]; | |
} | |
} | |
,'styl': { | |
value: function(prop, value){ | |
if(!value) return this[0].style[prop]; | |
this[0].style[prop] = value; | |
return this; | |
} | |
} | |
// ELEMENT SELECTION | |
,'get': { | |
value: function (index){ | |
var nodeList; | |
if(!this[index]) return null; | |
this[index].setAttribute('wrapNodeList',''); | |
nodeList = document.querySelectorAll('[wrapNodeList]'); | |
this[index].removeAttribute('wrapNodeList'); | |
return nodeList; | |
} | |
} | |
// ELEMENT CONTENT | |
,'html': { | |
value: function(val){ | |
this.each(function (elem){ | |
if(!val) return elem.innerHTML; | |
elem.innerHTML = val; | |
return this; | |
}); | |
} | |
} | |
// EVENTS | |
,'on': { | |
value: function(evName, callback, useCapture){ | |
this.each(function (elem){ | |
if(elem.addEventListener) elem.addEventListener(evName, callback, useCapture || false); | |
else if(elem.attachEvent) elem.attachEvent('on' + evName, callback); | |
}); | |
return this; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment