Skip to content

Instantly share code, notes, and snippets.

@e-river
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e-river/8e3930c534e17a3cf50c to your computer and use it in GitHub Desktop.
Save e-river/8e3930c534e17a3cf50c to your computer and use it in GitHub Desktop.
Add / Remove class without using jQuery
/*
* This code is to add or remove class depends on the direction like Smartphone.
* The classname is 'isActive'.
*/
function toogleClass(){
this.node = document.querySelector(target);
}
toogleClass.prototype.init = function(){
var self = this;
window.addEventListener('resize', function(){ self.detection() }, false);
window.addEventListener('load', function(){ self.detection() }, false);
};
toogleClass.prototype.detection = function(){
var self = this;
if( window.innerHeight > window.innerWidth ){
self.removeClass();
} else {
self.addClass();
}
};
toogleClass.prototype.addClass = function(){
var self = this;
if (self.node.classList) {
self.node.classList.add('isActive');
} else {
self.node.className += ' ' + 'isActive';
}
};
toogleClass.prototype.removeClass = function(){
var self = this;
if (self.node.classList) {
self.node.classList.remove('isActive');
} else {
self.node.className = self.node.className.replace(new RegExp('(^|\\b)' + 'isActive'.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment