Skip to content

Commit

Permalink
Use classList if the browser support it
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvein committed Dec 7, 2011
1 parent d934d46 commit 2451414
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions masonry.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,34 @@

// from bonzo.js, by Dustin Diaz - https://github.com/ded/bonzo

// use classList if supported because it's way faster: http://jsperf.com/classlist-vs-bonzo - by <@calvein>
var supportClassList = (function() {
var div = document.createElement('div');
return 'classList' in div;
})();

function classReg(c) {
return new RegExp("(^|\\s+)" + c + "(\\s+|$)");
}

function hasClass(el, c) {
return classReg(c).test(el.className);
return supportClassList ? el.classList.contains(c) : classReg(c).test(el.className);
}
function addClass(el, c) {
if ( !hasClass(el, c) ) {
el.className = el.className + ' ' + c;
if ( supportClassList ) {
el.classList.add(c);
} else {
if ( !hasClass(el, c) ) {
el.className = el.className + ' ' + c;
}
}
}
function removeClass(el, c) {
el.className = el.className.replace(classReg(c), ' ');
if ( supportClassList ) {
el.classList.remove(c);
} else {
el.className = el.className.replace(classReg(c), ' ');
}
}

// -------------------------- getStyle -------------------------- //
Expand Down

0 comments on commit 2451414

Please sign in to comment.