angularJS - getting x & y positions from mouseclick in div

前端 未结 3 1253
深忆病人
深忆病人 2020-12-29 19:20

I made a function that should add an item on the position I clicked inside a div. Now the problem with this function is, every time I click, it takes the x & y position

相关标签:
3条回答
  • 2020-12-29 20:01

    You need to change event.x to event.offsetX and event.y to event.offsetY

    You didn't add the template definition but I'll add it just in case:

    <div ng-click="addOnClick($event)"></div>
    
    0 讨论(0)
  • 2020-12-29 20:15

    For those on angular who are using dragging libraries and want the position of the element that was dragged:

    <div ng-click="OnDrag($event)"></div>
    

    and in the controller

    $scope.onDrag($event){
          console.log("X ->",$event.originalEvent.pageX,"Y ->",$event.originalEvent.pageY)
    
     }
    
    0 讨论(0)
  • 2020-12-29 20:23

    In html file, use:

    <div (click)="test($event)"></div>
    

    And in the function in the ts file::

    function test(e){
     console.log(e.clientX);
     console.log(e.clientY);
    }
    
    0 讨论(0)
提交回复
热议问题