Get mouse position when focus/blur events are fired?

前端 未结 2 1148
一整个雨季
一整个雨季 2021-01-18 02:04

I\'m using jQuery to capture an event:

$(\'input\').focus(function(e){

    console.log( e.pageX, e.pageY );

});

This doesn\'t seem to work.

2条回答
  •  借酒劲吻你
    2021-01-18 02:57

    You can only get mouse coordinates using mouse events. If you want to capture the position of the mouse, you can use a global mousemove event listener, and store the coordinates in a set of variables, which can later be accessed by the focus function. Example:

    var pageX, pageY; //Declare these globally
    $(window).mousemove(function(e){
        pagex = e.pageX;
        pageY = e.pageY;
    });
    
    $('input').focus(function(){
        console.log(pageX, pageY); // These variables have been defined by the global
                                   //  mousemove event
    });
    

提交回复
热议问题