Snippets

Timothy Anderson $.resizeEnd

Updated by Timothy Anderson

File readme.md Modified

  • Ignore whitespace
  • Hide word diff
-Use on resize to test media query:
+Use to wait until window has finished resizing before executing scripts:
 ```
-$(window).on('resize', function() {
-    if($.media_is('xs', 'sm')) {
-        // do stuff for small screens
-    }
-    else if($.media_is('md')) {
-        // do stuff for notebook
-    }
-    else {
-        // do stuff for large screens
-    }
+$(window).on('width.resizeEnd.zion', function() {
+    // do stuff when the window width changes
 });
+
+$(window).on('height.resizeEnd.zion', function() {
+    // do stuff when the window height changes
+})
+
+$(window).on('resizeEnd.zion', function() {
+    // do stuff when the window is resized in any direction
+})
 ```
Created by Timothy Anderson

File readme.md Added

  • Ignore whitespace
  • Hide word diff
+Use on resize to test media query:
+```
+$(window).on('resize', function() {
+    if($.media_is('xs', 'sm')) {
+        // do stuff for small screens
+    }
+    else if($.media_is('md')) {
+        // do stuff for notebook
+    }
+    else {
+        // do stuff for large screens
+    }
+});
+```

File resizeEnd.js Added

  • Ignore whitespace
  • Hide word diff
+(function($) {
+    var resizeTimer = setTimeout(function(){}, 1); // utility timer
+    var windowWidth = $(window ).width();
+    var windowHeight = $(window ).height();
+    $(window).add($('html, body')).on('resize', function() {
+        var $window = $(window);
+        clearTimeout(resizeTimer);
+        resizeTimer = setTimeout(function() {
+            if($window.width() !== windowWidth || $window.height() !== windowHeight) {
+                $window.trigger('resizeEnd.zion');
+            }
+            if($window.height() !== windowHeight) {
+                windowHeight = $window.height();
+                $window.trigger( 'height.resizeEnd.zion' );
+            }
+            if($window.width() !== windowWidth) {
+                windowWidth = $window.width();
+                $window.trigger('width.resizeEnd.zion');
+            }
+        }, 200);
+    });
+})(jQuery);
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.