Created
November 17, 2017 08:14
-
-
Save savokiss/c9bbb9a3bbb4840476ca0ffab5bae8ee to your computer and use it in GitHub Desktop.
cube-ui dom.js
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
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