Last active
December 28, 2015 11:09
-
-
Save tomkrush/7491605 to your computer and use it in GitHub Desktop.
Respond To for javascript
This file contains 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 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