代码之家  ›  专栏  ›  技术社区  ›  Luiz

如何在VueJS中获取鼠标坐标

  •  25
  • Luiz  · 技术社区  · 7 年前

    我有一个组件用 v-on:click="someMethod" .

    我如何获得这次点击的鼠标坐标(X,Y)?

    2 回复  |  直到 7 年前
        1
  •  46
  •   Anatoly    3 年前

    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);
            }
        }
    
        2
  •  6
  •   adeneo    7 年前

    就像在任何事件处理程序中一样

    new Vue({
      el: '#element',
      methods: {
        someMethod: function (event) {
          var x = event.pageX;
          var y = event.pageY;
        }
      }
    })
    

    clientX screenX