Skip to content

Instantly share code, notes, and snippets.

@tripolskypetr
Created January 4, 2020 21:46
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 tripolskypetr/843f557c55f416db526ccb0e62fd9f5c to your computer and use it in GitHub Desktop.
Save tripolskypetr/843f557c55f416db526ccb0e62fd9f5c to your computer and use it in GitHub Desktop.
JavaScript DragAndDrop performance test https://jsfiddle.net/tripolskypetr/v437tjqs/
<!DOCTYPE html>
<html>
<head>
<title>Drag and drop</title>
<style>
.red { background-color: red; }
.green { background-color: green; }
.blue { background-color: blue; }
.root { position: fixed; }
.box {
display: flex;
align-items: stretch;
justify-content: stretch;
padding: 2px;
}
.box > * {
flex: 1;
}
</style>
</head>
<body>
<script>
(function() {
const generateDraggable = (side = 500) => {
const root = document.createElement('div');
root.className = "red box root";
[root.style.height, root.style.width] = new Array(2).fill(`${side}px`);
let parent = root;
for (let i = 0; i !== parseInt(side / 4); i += 1) {
const child = document.createElement('div');
const color = i % 2 === 0 ? 'red' : i % 3 === 0 ? 'green' : 'blue';
child.className = `${color} box`;
parent.appendChild(child);
parent = child;
}
document.body.appendChild(root);
document.addEventListener('mousemove', (e) => {
const [x, y] = [e.clientX, e.clientY];
[root.style.top, root.style.left] = [`${y}px`, `${x}px`];
console.log(x, y);
});
};
generateDraggable();
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment