Skip to content

Instantly share code, notes, and snippets.

@subimage
Created November 11, 2013 20:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subimage/1ee189740e14bd75e0ed to your computer and use it in GitHub Desktop.
Save subimage/1ee189740e14bd75e0ed to your computer and use it in GitHub Desktop.
Simple jquery / underscore.js event handler for doing shit when the window resizes.
$(function () {
var win, lazyResize;
function handleResize() {
var divWidth = $('#somediv').width();
// now do something with that...
}
// debounce is an underscore.js function.
// We debounce so it doesn't fire too many times, taking up
// cycles on the user's computer.
lazyResize = _.debounce(handleResize, 100);
// Hold reference to window for speed.
win = $(window);
// Apply event handler on resizing window...
win.on('resize', lazyResize);
// Call it once to start
lazyResize();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment