Skip to content

Instantly share code, notes, and snippets.

@maheshbabu
Forked from chrisjordanme/angular-resize.js
Created May 27, 2016 18:54
Show Gist options
  • Save maheshbabu/a8ca25b4eeaac0d2ab9415422087c57c to your computer and use it in GitHub Desktop.
Save maheshbabu/a8ca25b4eeaac0d2ab9415422087c57c to your computer and use it in GitHub Desktop.
Angular Directive for Window Resize Event
angular.module('your-module-name', [])
.directive('onResize', ['$window', function ($window) {
return {
link: function (scope, el, attrs) {
var initialWidth = $window.innerWidth,
smallClass = attrs.resizeClass || 'yourDefault',
smallAttr = attrs.resizeAttr || 'yourDefault',
smallWidth = attrs.resizeWidth || 1024;
var setSmall = function () {
el.addClass(smallClass);
el.attr(smallAttr, smallAttr);
};
var setLarge = function () {
el.removeClass(smallClass);
el.removeAttr(smallAttr);
};
if (initialWidth < smallWidth) {
setSmall();
} else {
setLarge();
}
angular.element($window).on('resize', function () {
if ($window.innerWidth <= smallWidth) {
setSmall();
} else {
setLarge();
}
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment