开发者

how to get the mouse's offset when i mouseover a element..use jquery

开发者_StackOverflowthis is my code and have a error:

    $('#map_canvas').mouseover(function(e){
        console.log(e.offset().left+'  '+e.offset().top)
        })

thanks


i do this ,and it is always log (0, 0):

$('#map_canvas').mouseover(function(e){
var offset = $('#map_canvas').offset(); 
console.log(offset.top+'  '+offset.left); //offset of 'realtiveDiv'
console.log(e.pageX +'  '+e.pageY); // mouse position absolute
})

why?

thanks


$('#map_canvas').mouseover(function(e){
    var offset = $('#map_canvas').offset(); 
    var x = e.pageX - offset.left;
    var y = e.pageY - offset.top;

    console.log('X: '+x+' Y: '+y); //you want this

    //console.log(offset.top+'  '+offset.left); //offset of 'realtiveDiv'
    //console.log(e.pageX +'  '+e.pageY); // mouse position absolute
});

Update:
if offset.top and offset.left log (0, 0), it means that the element whose offset you are logging, starts at (0, 0). In other words, the element is at the top left corner of the screen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜