Skip to content

Instantly share code, notes, and snippets.

@artemean
Created December 13, 2014 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artemean/7200d7bef12800625032 to your computer and use it in GitHub Desktop.
Save artemean/7200d7bef12800625032 to your computer and use it in GitHub Desktop.
XJdZOg
<div class="asd"><div class="ms"></div></div>
<div class="coor"></div>
$(".asd").mousemove(function(e){
var parentOffset = $(this).parent().offset();
//or $(this).offset(); if you really just want the current element's offset
var relX = e.pageX - parentOffset.left;
var relY = e.pageY - parentOffset.top;
$(".coor").empty().append(relX, "-", relY);
var crds={
left:relX,
top:relY
}
$(".ms").css(crds);
});
.asd {width: 500px; height: 200px; background: #ccc;}
.ms {position: absolute; width: 25px; height: 25px; background: #16f; border-radius: 15px; box-shadow: 3px 3px 5px #333; margin: -20px;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment