Skip to content

Instantly share code, notes, and snippets.

@bitflower
Forked from desandro/classie.js
Last active August 29, 2015 14:12
Show Gist options
  • Save bitflower/f1f5c2acacf4ec4c7a33 to your computer and use it in GitHub Desktop.
Save bitflower/f1f5c2acacf4ec4c7a33 to your computer and use it in GitHub Desktop.
/*!
* classie - class helper functions
* from bonzo https://github.com/ded/bonzo
*
* classie.has( elem, 'my-class' ) -> true/false
* classie.add( elem, 'my-new-class' )
* classie.remove( elem, 'my-unwanted-class' )
*/
/*jshint browser: true, strict: true, undef: true */
( function( window ) {
'use strict';
// class helper functions from bonzo https://github.com/ded/bonzo
function classReg( className ) {
return new RegExp("(^|\\s+)" + className + "(\\s+|$)");
}
// classList support for class management
// altho to be fair, the api sucks because it won't accept multiple classes at once
var hasClass, addClass, removeClass;
if ( 'classList' in document.documentElement ) {
hasClass = function( elem, c ) {
return elem.classList.contains( c );
};
addClass = function( elem, c ) {
elem.classList.add( c );
};
removeClass = function( elem, c ) {
elem.classList.remove( c );
};
}
else {
hasClass = function( elem, c ) {
return classReg( c ).test( elem.className );
};
addClass = function( elem, c ) {
if ( !hasClass( elem, c ) ) {
elem.className = elem.className + ' ' + c;
}
};
removeClass = function( elem, c ) {
elem.className = elem.className.replace( classReg( c ), ' ' );
};
}
window.classie = {
// full names
hasClass: hasClass,
addClass: addClass,
removeClass: removeClass,
// short names
has: hasClass,
add: addClass,
remove: removeClass
};
})( window );
{
"name": "classie",
"main": "./classie.js",
"version": "0.0.3"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment