Skip to content

Instantly share code, notes, and snippets.

@lilywang711
Created April 25, 2019 03:43
Show Gist options
  • Save lilywang711/1e772eb0c51aaf114c700b7e8db3c800 to your computer and use it in GitHub Desktop.
Save lilywang711/1e772eb0c51aaf114c700b7e8db3c800 to your computer and use it in GitHub Desktop.
export const hasClass = function(obj, cls) {
return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
};
export const addClass = function(obj, cls) {
if (!hasClass(obj, cls)) obj.className += ' ' + cls;
};
export const removeClass = function(obj, cls) {
if (hasClass(obj, cls)) {
const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
obj.className = obj.className.replace(reg, ' ');
}
};
export const toggleClass = function(obj, cls) {
if (hasClass(obj, cls)) {
removeClass(obj, cls);
} else {
addClass(obj, cls);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment