Created
April 25, 2019 03:43
-
-
Save lilywang711/1e772eb0c51aaf114c700b7e8db3c800 to your computer and use it in GitHub Desktop.
This file contains 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 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