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

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

    Think I know your problem. You're trying to query the mouse position programmatically. Perhaps you have code something like this:

    $(document).one('mousemove.myModule', function (e) {
        // e.pageY and e.pageX is undefined.
        console.log('e.pageY: ' + e.pageY + ', e.pageX: ' + e.pageX); }
            ).trigger('mousemove.myModule');
    

    Then you're absolutely right. The properties pageY and pageX on the event object won't be defined. Actually, there's a hole lot of stuff in the event object that won't be there, not just .pageY and .pageX. Could be some bug in jQuery. Anyways, just don't chain that call to trigger and trigger the event on $(window) instead. Don't ask me why it works though but for me it did:

    $(document).one('mousemove.myModule', function (e) {
        console.log('e.pageY: ' + e.pageY + ', e.pageX: ' + e.pageX); } );
    
    // Now e.pageY and e.pageX will be defined!
    $(window).trigger('mousemove.myModule');
    

提交回复
热议问题