Last active
August 29, 2015 14:01
-
-
Save e-river/8e3930c534e17a3cf50c to your computer and use it in GitHub Desktop.
Add / Remove class without using jQuery
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
/* | |
* 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