You can use one element by passing in $event as a parameter to your ng-click function. Then you can get the position of the mouse click relative to the document
app.directive('container', function () { var offset = { left: 40, top: -20 } return function (scope, element, attributes) { var $oLay = angular.element(document.getElementById('overlay')) scope.showOptions = function (item,$event) { var overlayDisplay; if (scope.currentItem === item) { scope.currentItem = null; overlayDisplay='none' }else{ scope.currentItem = item; overlayDisplay='block' } var overLayCSS = { left: $event.clientX + offset.left + 'px', top: $event.clientY + offset.top + 'px', display: overlayDisplay } $oLay.css(overLayCSS) } } });
Not sure if angular normalizes clientX and clientY jQuery way for different browsers. Not all browsers use the same convention for event position properties. We removed the ng-show element from the overlay element so that the style attributes could be controlled from the directive and not using the angular compiler due to the time and giving it an identifier.
DEMO: http://jsfiddle.net/jJyTf/
source share