Skip to content

Instantly share code, notes, and snippets.

@savokiss
Created November 17, 2017 08:14
Show Gist options
  • Save savokiss/c9bbb9a3bbb4840476ca0ffab5bae8ee to your computer and use it in GitHub Desktop.
Save savokiss/c9bbb9a3bbb4840476ca0ffab5bae8ee to your computer and use it in GitHub Desktop.
cube-ui dom.js
export function hasClass(el, className) {
const reg = new RegExp('(^|\\s)' + className + '(\\s|$)')
return reg.test(el.className)
}
export function addClass(el, className) {
if (hasClass(el, className)) {
return
}
const newClass = el.className.split(' ')
newClass.push(className)
el.className = newClass.join(' ')
}
export function removeClass(el, className) {
if (!hasClass(el, className)) {
return
}
const reg = new RegExp('(^|\\s)' + className + '(\\s|$)', 'g')
el.className = el.className.replace(reg, ' ')
}
export function getData(el, name) {
const prefix = 'data-'
return el.getAttribute(prefix + name)
}
export function getRect(el) {
return {
top: el.offsetTop,
left: el.offsetLeft,
width: el.offsetWidth,
height: el.offsetHeight
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment