Skip to content

Instantly share code, notes, and snippets.

@pokorson
Created June 12, 2019 17:40
Show Gist options
  • Save pokorson/b6db7b9bdfe4cc4ab04e1e2df9bc5158 to your computer and use it in GitHub Desktop.
Save pokorson/b6db7b9bdfe4cc4ab04e1e2df9bc5158 to your computer and use it in GitHub Desktop.
Stick div to cursor
<html>
<head></head>
<body>
<div></div>
<span id="drag-test"></span>
</body>
<script>
document.addEventListener(
'mousemove',
function(ev) {
document.getElementById('drag-test').style.transform =
'translateY(' + ev.clientY + 'px)';
document.getElementById('drag-test').style.transform +=
'translateX(' + ev.clientX + 'px)';
},
false
);
</script>
<style>
#drag-test {
width: 25px;
height: 25px;
display: block;
background-color: red;
/* transition: transform 0.2s; */
}
</style>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment