Created
May 1, 2014 11:41
-
-
Save perminder-klair/11449792 to your computer and use it in GitHub Desktop.
Resize container as Window size (accepts percentage) for AngularJs
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
'use strict'; | |
/** | |
* Usage: ng-style="style()" resize heightpercetange="50" widthpercetange="50" | |
*/ | |
angular.module('app') | |
.directive('resize', function ($window) { | |
return { | |
restrict: 'A', | |
link: function (scope, elem, attrs) { | |
var w = angular.element($window); | |
scope.getWindowDimensions = function () { | |
return { 'h': w.height(), 'w': w.width() }; | |
}; | |
scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) { | |
var newHeight = newValue.h; | |
if (typeof attrs.heightpercetange !== "undefined") { | |
newHeight = (attrs.heightpercetange / 100) * newValue.h; | |
} | |
elem.css('height', newHeight + 'px'); | |
var newWidth = newValue.w; | |
if (typeof attrs.widthpercetange !== "undefined") { | |
newWidth = (attrs.widthpercetange / 100) * newValue.h; | |
} | |
elem.css('width', newWidth + 'px'); | |
}, 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