Is there a way to trigger mousemove and get event.pageX, event.pageY?

后端 未结 6 1702
温柔的废话
温柔的废话 2020-12-05 23:23

So, like the question specifies, is there a way to trigger a mousemove event in jQuery which also sends the mouse coordinates to the event Object?

So far my code can

6条回答
  •  有刺的猬
    2020-12-06 00:10

    This is the best I could come up with, that doesn't inolve you setting global variables. The click event will still capture the coordinates, so you can pass that event to your mousemove.

    $(element).click(function(e){
      $(element).trigger('mousemove',e);
    });
    

    Then, that event object will be the second argument in your mousemove function; the first being either the actual mousemove event or the jQuery created event.

    $(element).mousemove(function(e,clickEvent){
      if typeof e.pageX === 'undefined' e = clickEvent;
      // or if you prefer, clickEvent.pageX;
    });
    

提交回复
热议问题