Created
November 15, 2016 13:10
-
-
Save ricardozea/56e1b5f96cd8d28d8d95b84cf28e0265 to your computer and use it in GitHub Desktop.
Add/Remove a class in the <body> tag at specific breakpoints.
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
/* | |
* Add/Remove a class in the <body> tag at specific breakpoints | |
* Thanks to: | |
* Jon Daiello - https://twitter.com/jondaiello | |
* Nate Denlinger - https://twitter.com/natedenlinger | |
*/ | |
var breakpoints = [ | |
{ class: "accordion", width: 640 }, //edit | |
//{class: "another-class", width: 756} | |
]; | |
function setWidthClasses(element) { | |
var elementWidth = element.offsetWidth; | |
for (var x = 0; x < breakpoints.length; x++) { | |
if (window.innerWidth < breakpoints[x].width) { | |
element.classList.add(breakpoints[x].class) | |
} else { | |
element.classList.remove(breakpoints[x].class) | |
} | |
} | |
} | |
var targetElement = document.body; | |
window.onload = setWidthClasses(targetElement); | |
window.onresize = function() { | |
setWidthClasses(targetElement) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment