Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Created December 13, 2012 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidWells/4274990 to your computer and use it in GitHub Desktop.
Save DavidWells/4274990 to your computer and use it in GitHub Desktop.
jQuery :: Responsive Design Media Query Helper Function
<script>
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$(function() {
var pause = 100; // will only process code within delay(function() { ... }) every 100ms.
$(window).resize(function() {
delay(function() {
var width = $(window).width();
if( width >= 768 && width <= 959 ) {
// code for tablet view
} else if( width >= 480 && width <= 767 ) {
// code for mobile landscape
} else if( width <= 479 ) {
// code for mobile portrait
}
}, pause );
});
$(window).resize();
});
<script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment