Skip to content

Instantly share code, notes, and snippets.

Last active February 9, 2016 01:15
What would you like to do?
offsetX / offsetY
<!DOCTYPE html>
<meta charset="utf-8">
body {
border: solid 3px steelblue;
#parent {
cursor: crosshair;
border: solid 7px steelblue;
position: relative;
margin: 29px auto;
padding: 31px;
width: 800px;
height: 400px;
background: #eee;
#child {
position: absolute;
border: solid 13px steelblue;
margin: 17px;
padding: 23px;
top: 100px;
left: 100px;
width: 200px;
height: 100px;
background: #ccc;
#circle {
position: absolute;
pointer-events: none;
border-radius: 12px;
margin-left: -12px;
margin-top: -12px;
width: 24px;
height: 24px;
background: red;
#small-circle {
position: absolute;
pointer-events: none;
background: black;
border-radius: 6px;
top: 12px;
left: 12px;
margin-left: -4px;
margin-top: -4px;
width: 8px;
height: 8px;
<div id="parent">
<div id="offset"></div>
<div id="child">
<div id="circle">
<div id="small-circle"></div>
<script src="//"></script>
<script>"#child").on("mousemove", function() {
var ox, oy;
if ("offsetX" in d3.event) {
ox = d3.event.offsetX - this.clientLeft;
oy = d3.event.offsetY - this.clientTop;
} else {
ox = d3.event.layerX - this.clientLeft;
oy = d3.event.layerY - this.clientTop;
}"#offset").text(ox + "," + oy);"#circle")
.style("left", ox + "px")
.style("top", oy + "px");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment