-
-
Save maheshbabu/a8ca25b4eeaac0d2ab9415422087c57c to your computer and use it in GitHub Desktop.
Angular Directive for Window Resize Event
This file contains hidden or 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
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