Created
December 16, 2013 15:10
-
-
Save adrianoxavier/7988559 to your computer and use it in GitHub Desktop.
AngularJS resize directive
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
| /* | |
| Usage | |
| <div resize="{'attr': 'height', '*': 0.375}"> ... | |
| */ | |
| 'use strict'; | |
| Application.directive('resize', function ($window) { | |
| return { | |
| restrict: 'A', | |
| scope: { | |
| 'resize' : '@' | |
| }, | |
| link: function(scope, element, attrs) { | |
| var params = eval('(' + scope.resize + ')'); | |
| var w = angular.element($window); | |
| scope.getWindowDimensions = function () { | |
| return { 'h': $window.innerHeight, 'w': $window.innerWidth }; | |
| }; | |
| scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) { | |
| var value = (params.attr == 'height') ? newValue.h : newValue.w; | |
| if (params['-'] && !isNaN(params['-'])) { | |
| value = value - params['-']; | |
| } | |
| if (params['+'] && !isNaN(params['+'])) { | |
| value = value + params['+']; | |
| } | |
| if (params['*'] && !isNaN(params['*'])) { | |
| value = parseInt(value * params['*']); | |
| } | |
| if (params['/'] && !isNaN(params['/'])) { | |
| value = parseInt(value / params['/']); | |
| } | |
| if (params.attr == 'height' || params.attr == 'both') { | |
| angular.element(element).height(value); | |
| } | |
| if (params.attr == 'width' || params.attr == 'both') { | |
| angular.element(element).width(value); | |
| } | |
| }, true); | |
| w.bind('resize', function () { | |
| scope.$apply(); | |
| }); | |
| } | |
| }; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment