Last active
September 30, 2018 21:40
-
-
Save caub/437e5d98ee9260df811628f7900c1c84 to your computer and use it in GitHub Desktop.
v4.1
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 $(container, selector) { | |
const els = | |
typeof selector === 'string' | |
? container.querySelectorAll(selector) | |
: typeof container === 'string' ? document.querySelectorAll(container) : [container]; | |
const fns = { | |
removeClass(...cls) { | |
els.forEach(el => { | |
el.classList.remove(...cls); | |
}); | |
return this; | |
}, | |
addClass(...cls) { | |
els.forEach(el => { | |
el.classList.add(...cls); | |
}); | |
return this; | |
}, | |
toggleClass(cls, b) { | |
els.forEach(el => { | |
el.classList.toggle(cls, b); | |
}); | |
return this; | |
}, | |
on(type, cb, opts) { | |
els.forEach(el => { | |
el.addEventListener(type, cb, opts); | |
}); | |
return this; | |
}, | |
off(type, cb, opts) { | |
els.forEach(el => { | |
el.removeEventListener(type, cb, opts); | |
}); | |
return this; | |
}, | |
css(props) { | |
els.forEach(el => { | |
Object.assign(el.style, props); | |
}); | |
return this; | |
}, | |
}; | |
return new Proxy(els, { | |
get(targ, k) { | |
return els[k] || fns[k]; | |
}, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment