Get mouse position within div? [duplicate]

Using jQuery:

var divPos = {};
var offset = $("#divid").offset();
$(document).mousemove(function(e){
    divPos = {
        left: e.pageX - offset.left,
        top: e.pageY - offset.top
    };
});

Use event.layerX and event.layerY to get mouse position relative to the current element:

$('#canvas').mousemove(function(e){
  var mousePos = {'x': e.layerX, 'y': e.layerY};
});