-
-
Save bitflower/f1f5c2acacf4ec4c7a33 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
/*! | |
* 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 ); |
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
{ | |
"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