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

后端 未结 6 1728
温柔的废话
温柔的废话 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:12

    You need to set pageX and pageY directly before triggering the event. To set these properties, make a jQuery.Event object.

    // create a jQuery event
    e = $.Event('mousemove');
    
    // set coordinates
    e.pageX = 100;
    e.pageY = 100;
    
    // trigger event - must trigger on document
    $(document).trigger(e);
    

    See it in jsFiddle.

提交回复
热议问题