Created
June 7, 2017 15:34
-
-
Save tomfordweb/6ae9f8ac84f4eba3578345f43ccd8746 to your computer and use it in GitHub Desktop.
JQuery + Bootstrap 3 - Responsive 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
function addResponsivePixelToBody() | |
{ | |
$('body').append('<div id="xs-res" class="hidden-sm hidden-md hidden-lg"></div><div id="small-res" class="hidden-md hidden-xs hidden-lg"></div><div id="medium-res" class="hidden-sm hidden-xs hidden-lg"></div><div id="large-res" class="hidden-sm hidden-xs hidden-md"></div><div class="col-sm-12 col-md-4 text-center">'); | |
} | |
function responsiveBreakpoints() | |
{ | |
var xs = $('#xs-res'); | |
var small = $('#small-res'); | |
var med = $('#medium-res'); | |
var large = $('#large-res'); | |
var body = $('body'); | |
if(xs.is(':visible')) { | |
body.addClass('bootstrap-xs').removeClass('bootstrap-small').removeClass('bootstrap-medium').removeClass('bootstrap-large'); | |
} | |
if(small.is(':visible')) { | |
body.addClass('bootstrap-small').removeClass('bootstrap-medium').removeClass('bootstrap-large').removeClass('bootstrap-xs'); | |
} | |
if(med.is(':visible')) { | |
body.addClass('bootstrap-medium').removeClass('bootstrap-small').removeClass('bootstrap-large').removeClass('bootstrap-xs'); | |
} | |
if(large.is(':visible')) { | |
body.addClass('bootstrap-large').removeClass('bootstrap-small').removeClass('bootstrap-medium').removeClass('bootstrap-xs'); | |
} | |
} | |
$(document).ready(function() { | |
addResponsivePixelToBody(); | |
responsiveBreakpoints(); // if needed to change class on resize, you should also call this function with a debouncer/throttler | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment