Skip to content

Instantly share code, notes, and snippets.

@tomkrush
Last active December 28, 2015 11:09
Show Gist options
  • Save tomkrush/7491605 to your computer and use it in GitHub Desktop.
Save tomkrush/7491605 to your computer and use it in GitHub Desktop.
Respond To for javascript
function respondsTo(respondToSize, callback, undoCallback) {
if ( typeof respondToSize === 'string' )
{
respondToSize = [respondToSize];
}
function calculateSize()
{
var width = jQuery('body').width();
var size = 'desktop';
// Is Tablet
if ( width <= 960 && width >= 740 )
{
size = 'tablet';
}
// Is Small Tablet
else if ( width <= 740 && width >= 600 )
{
size = 'small-tablet';
}
// Is Wide Mobile
else if ( width <= 600 && width >= 480 )
{
size = 'wide-mobile';
}
// Is Mobile
else if ( width <= 480 )
{
size = 'mobile';
}
return size;
}
jQuery(window).on('resize.breakpoint', function() {
var size = calculateSize();
var sizeChanged = window.respondToSize != size;
var isSize = jQuery.inArray(size, respondToSize) > -1;
if ( sizeChanged && isSize)
{
window.respondToSize = size;
callback(size);
}
else if ( sizeChanged )
{
undoCallback(size);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment