[英]How to get mouse coordinates in VueJS
我有一个用v-on:click="someMethod"
触发的组件。
我如何获得这次点击的鼠标坐标(X,Y)?
附加信息:HTML5 Canvas 组件
Vue 将event
作为方法中的第一个参数传递。 如果有参数,请改用: someMethod(param1, param2, event)
methods: {
someMethod(event) {
// clientX/Y gives the coordinates relative to the viewport in CSS pixels.
console.log(event.clientX);
console.log(event.clientY);
// pageX/Y gives the coordinates relative to the <html> element in CSS pixels.
console.log(event.pageX);
console.log(event.pageY);
// screenX/Y gives the coordinates relative to the screen in device pixels.
console.log(event.screenX);
console.log(event.screenY);
}
}
就像在任何事件处理程序中一样
new Vue({
el: '#element',
methods: {
someMethod: function (event) {
var x = event.pageX;
var y = event.pageY;
}
}
})
还有clientX
和screenX
,它们根据视口、屏幕或呈现的内容返回有些不同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.