Skip to content

Instantly share code, notes, and snippets.

@minakaco0301
Forked from desandro/classie.js
Created January 3, 2014 13:26
Show Gist options
  • Select an option

  • Save minakaco0301/8237789 to your computer and use it in GitHub Desktop.

Select an option

Save minakaco0301/8237789 to your computer and use it in GitHub Desktop.

Revisions

  1. @desandro desandro revised this gist Jan 24, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion component.json
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    {
    "name": "classie",
    "main": "./classie.js"
    "main": "./classie.js",
    "version": "0.0.3"
    }
  2. @desandro desandro revised this gist Jan 24, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion component.json
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    {
    "name": "classie",
    "main": "classie.js"
    "main": "./classie.js"
    }
  3. @desandro desandro revised this gist Jan 24, 2013. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions component.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    {
    "name": "classie",
    "main": "classie.js"
    }
  4. @desandro desandro created this gist Jan 24, 2013.
    62 changes: 62 additions & 0 deletions classie.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    /*!
    * 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 );