Last active
January 16, 2016 00:53
-
-
Save bjmiller121/38642be87e6c2769e1d5 to your computer and use it in GitHub Desktop.
Breakpoint body classes
This file contains hidden or 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 breakpoint classes to the body as a utility. | |
* | |
* Requires jQuery and underscore | |
*/ | |
Breakpoints = { | |
'mobileMax': 639, | |
'tabletMin': 640, | |
'tabletMax': 960, | |
'desktopMin': 961 | |
}; | |
(function ($) { | |
$(document).ready(function () { | |
breakpointClass(); | |
$(window).on('resize orientationchange', _.debounce(breakpointClass, 100)); | |
}); | |
function breakpointClass() { | |
var width = window.outerWidth, | |
$body = $('body'); | |
// Remove any previously set breakpoint class. | |
$body.removeClass('breakpoint-mobile breakpoint-tablet breakpoint-desktop'); | |
// Set breakpoint class based on window width. | |
if (width >= Breakpoints.desktopMin) { | |
$body.addClass('breakpoint-desktop'); | |
} else if (width <= Breakpoints.mobileMax) { | |
$body.addClass('breakpoint-mobile'); | |
} else { | |
$body.addClass('breakpoint-tablet'); | |
} | |
} | |
})(jQuery, window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment