Skip to content

Instantly share code, notes, and snippets.

@pyfisch
Last active June 15, 2017 19:06
Show Gist options
  • Save pyfisch/eb624bffa9d21177519099ebd065a040 to your computer and use it in GitHub Desktop.
Save pyfisch/eb624bffa9d21177519099ebd065a040 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0px;
}
#spacer {
width: 800px;
height: 500px;
background-color: grey;
}
#demo {
width: 400px;
height: 400px;
background-color: blue;
}
</style>
<script>
window.onload = function () {
var demo = document.getElementById('demo');
demo.onclick = function (e) {
var x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
var y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
x -= demo.offsetLeft;
y -= demo.offsetTop;
console.log(`document.body scroll: ${document.body.scrollLeft}, ${document.body.scrollTop}`);
console.log(`document.documentElement scroll: ${document.documentElement.scrollLeft}, ${document.documentElement.scrollTop}`);
console.log(`offset: ${demo.offsetLeft}, ${demo.offsetTop}`);
console.log(`Pos: (${x}|${y})`);
}
}
</script>
</head>
<body>
<div id="spacer"></div>
<div id="demo">
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment