angularJS-获取x& div中鼠标单击的y个位置 [英] angularJS - getting x & y positions from mouseclick in div

查看:98
本文介绍了angularJS-获取x& div中鼠标单击的y个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个函数,该函数应该在div中单击的位置上添加一个项目.现在,此功能存在的问题是,每次单击时,它都会使用x&文档的y位置,而不是x& div内的y位置.因此,我真正想要的是div的左上角应为x = 0& y = 0,但是我不知道这是否可能吗?我想还有另一种方法可以做到这一点.

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 of the document, not the x & y position inside the div. So what I actually want, is that the top-left corner of my div should give x=0 & y=0, but I don't know if this is possible? And I guess there is another way to do this..

$scope.addOnClick = function(event) {
        $scope.items.push( {
            "label": "Click",
            "value": 100,
            "x": event.x-50,
            "y": event.y-50,
        })
}

推荐答案

您需要将event.x更改为event.offsetX,将event.y更改为event.offsetY

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>

这篇关于angularJS-获取x&amp; div中鼠标单击的y个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆