Skip to content

Instantly share code, notes, and snippets.

@gskema
Created May 19, 2016 18:14
Show Gist options
  • Save gskema/57869bff1407e9efb3ab9a2a6d4ca64e to your computer and use it in GitHub Desktop.
Save gskema/57869bff1407e9efb3ab9a2a6d4ca64e to your computer and use it in GitHub Desktop.
Bootstrap screen width change event JS
var resizeBuffer = null;
var prevWidthName = getWindowWidthName();
$(window).on('resize', function () {
clearTimeout(resizeBuffer);
resizeBuffer = setTimeout(function() {
var newWidthName = getWindowWidthName();
if (newWidthName != prevWidthName) {
prevWidthName = newWidthName;
$(document).trigger(newWidthName);
}
}, 300);
});
function getWindowWidthName() {
var w = $(window).width();
switch (true) {
case w < 768:
return 'width-xs';
case w < 992:
return 'width-sm';
case w < 1200:
return 'width-md';
default:
return 'width-lg';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment