Take a look at Fiddle . [Edited]
You need to get the coordinates of the mouse, and then subtract the offset of the element to get the relative value.
HTML:
<div class="test"></div>
JS:
$(document).on('mousemove','.test', function(e){
var offset = $(this).offset();
var relativeX = e.pageX - offset.left;
var relativeY = e.pageY - offset.top;
var wide = $(this).width();
var tall = $(this).height();
var percentX = (relativeX*100)/wide;
var percentY = (relativeY*100)/tall;
console.log(percentX+", "+percentY);
});
CSS
.test{
background-color: #000000;
width: 50%;
height: 50px;
margin-left: 40px;
margin-top: 20px;
}
source
share