繁体   English   中英

如何在VueJS中获取鼠标坐标

[英]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;
    }
  }
})

还有clientXscreenX ,它们根据视口、屏幕或呈现的内容返回有些不同的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM