Skip to content

Instantly share code, notes, and snippets.

@domderen
Created August 6, 2014 20:14
Show Gist options
  • Save domderen/3f92e12673802740d7d8 to your computer and use it in GitHub Desktop.
Save domderen/3f92e12673802740d7d8 to your computer and use it in GitHub Desktop.
Angular directive for creating dynamic overlays over any element. Directive is automatically preparing parent to have an overlay, and dynamically sets the height of the overlay if the height of the parent will change. Second directive allows to set any element in the vertical and horizontal center of the parent element. It dynamically reposition…
var overlayModule = angular.module('angular-overlay', []).directive('elementOverlay', [function () {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
$(elem[0].parentNode).css('position', 'relative');
scope.$watch(function () {
return $(elem[0].parentNode).height();
}, function (v) {
elem.css('height', v + 'px');
});
}
};
}]).directive('elementCenter', [function () {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
elem.css('position', 'absolute');
scope.$watch(function () {
return $(elem[0].parentNode).height();
}, function (h) {
var elementHeight = elem.height();
elem.css('top', (h - elementHeight)/2);
});
scope.$watch(function () {
return $(elem[0].parentNode).width();
}, function (w) {
var elementWidth = elem.width();
elem.css('left', (w - elementWidth)/2);
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment