Created
May 19, 2016 18:14
-
-
Save gskema/57869bff1407e9efb3ab9a2a6d4ca64e to your computer and use it in GitHub Desktop.
Bootstrap screen width change event JS
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
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