Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8e9b2c3

Browse filesBrowse files
committed
First commit
0 parents  commit 8e9b2c3
Copy full SHA for 8e9b2c3

File tree

Expand file treeCollapse file tree

6 files changed

+331
-0
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+331
-0
lines changed

‎LICENSE

Copy file name to clipboard
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2020 Kailash Nadh. https://nadh.in
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

‎README.md

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# dragmove.js
2+
3+
A super tiny Javascript library to make DOM elements draggable and movable. Has touch screen support. Zero dependencies and 500 bytes Gzipped. [Demo here](https://knadh.github.io/dragmove.js/docs/).
4+
5+
## Usage
6+
7+
```shell
8+
npm install @knadh/dragmove
9+
```
10+
11+
```javascript
12+
import { dragmove } from @knadh/dragmove;
13+
14+
// (target, handler, onStart(target, x, y), onEnd(target, x, y)).
15+
// onStart and onEnd are optional callbacks that receive target element, and x, y co-oordinates.
16+
17+
dragmove(document.querySelector("#box"), document.querySelector("#box .drag-handle"));
18+
```
19+
20+
Licensed under the MIT License.

‎docs/dragmove.js

Copy file name to clipboard
+87Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* eslint-disable */
2+
// Global states.
3+
let _loaded = false;
4+
let _callbacks = [];
5+
const _isTouch = window.ontouchstart !== undefined;
6+
7+
export const dragmove = function(target, handler, onStart, onEnd) {
8+
// Register a global event to capture mouse moves (once).
9+
if (!_loaded) {
10+
document.addEventListener(_isTouch ? "touchmove" : "mousemove", function(e) {
11+
let c = e;
12+
if (e.touches) {
13+
c = e.touches[0];
14+
}
15+
16+
// On mouse move, dispatch the coords to all registered callbacks.
17+
for (var i = 0; i < _callbacks.length; i++) {
18+
_callbacks[i](c.clientX, c.clientY);
19+
}
20+
});
21+
}
22+
23+
_loaded = true;
24+
let isMoving = false, hasStarted = false;
25+
let startX = 0, startY = 0, lastX = 0, lastY = 0;
26+
27+
// On the first click and hold, record the offset of the pointer in relation
28+
// to the point of click inside the element.
29+
handler.addEventListener(_isTouch ? "touchstart" : "mousedown", function(e) {
30+
e.stopPropagation();
31+
e.preventDefault();
32+
if (target.dataset.dragEnabled === "false") {
33+
return;
34+
}
35+
36+
let c = e;
37+
if (e.touches) {
38+
c = e.touches[0];
39+
}
40+
41+
isMoving = true;
42+
startX = target.offsetLeft - c.clientX;
43+
startY = target.offsetTop - c.clientY;
44+
});
45+
46+
// On leaving click, stop moving.
47+
document.addEventListener(_isTouch ? "touchend" : "mouseup", function() {
48+
isMoving = false;
49+
hasStarted = false;
50+
51+
if (onEnd) {
52+
onEnd(target, parseInt(target.style.left), parseInt(target.style.top));
53+
}
54+
});
55+
56+
// Register mouse-move callback to move the element.
57+
_callbacks.push(function move(x, y) {
58+
if (!isMoving) {
59+
return;
60+
}
61+
62+
if (!hasStarted) {
63+
hasStarted = true;
64+
if (onStart) {
65+
onStart(target, lastX, lastY);
66+
}
67+
}
68+
69+
lastX = x + startX;
70+
lastY = y + startY;
71+
72+
// If boundary checking is on, don't let the element cross the viewport.
73+
if (target.dataset.dragBoundary === "true") {
74+
if (lastX < 1 || lastX >= window.innerWidth - target.offsetWidth) {
75+
return;
76+
}
77+
if (lastY < 1 || lastY >= window.innerHeight - target.offsetHeight) {
78+
return;
79+
}
80+
}
81+
82+
target.style.left = lastX + "px";
83+
target.style.top = lastY + "px";
84+
});
85+
}
86+
87+
export { dragmove as default };

‎docs/index.html

Copy file name to clipboard
+84Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charSet="utf-8"/>
5+
<style>
6+
body {
7+
font-family: "Helvetica Neue", "Segoe UI", sans-serif;
8+
}
9+
.parent {
10+
background: #ddd;
11+
position: fixed;
12+
top: 25%;
13+
left: 10%;
14+
width: 150px;
15+
height: 150px;
16+
}
17+
.handle {
18+
background: #888;
19+
cursor: move;
20+
width: 130px;
21+
color: #fff;
22+
padding: 10px;
23+
}
24+
25+
#test2 {
26+
background: #888;
27+
left: auto;
28+
right: 10%;
29+
}
30+
#test2 .handle {
31+
background: #ddd;
32+
color: #888;
33+
}
34+
</style>
35+
</head>
36+
<body>
37+
<p><a href="https://github.com/knadh/dragmove.js"><small>dragmove.js</small></a></p>
38+
<div id="test" class="parent" data-drag-bound="true">
39+
<div class="handle" style="">Drag here</div>
40+
</div>
41+
<div id="test2" class="parent">
42+
<div class="handle" style="">Drag here</div>
43+
</div>
44+
45+
<script type="module">
46+
import { dragmove } from './dragmove.js';
47+
48+
// Use the start/end events to simulate "snap to edge" behaviour.
49+
const snapThreshold = 50;
50+
function onStart(el, x, y) {
51+
// On drag start, remove the fixed bottom style to prevent the bottom
52+
// from sticking on the screen.
53+
el.style.top = el.offsetTop + "px"
54+
el.style.bottom = "auto"
55+
}
56+
57+
function onEnd(el, x, y) {
58+
// Automatically snap to corners.
59+
if (window.innerHeight - (el.offsetTop + el.offsetHeight) < snapThreshold) {
60+
el.style.top = "auto"
61+
el.style.bottom = "0px"
62+
}
63+
if (window.innerWidth - (el.offsetLeft + el.offsetWidth) < snapThreshold) {
64+
el.style.left = "auto"
65+
el.style.right = "0px"
66+
}
67+
if (el.offsetTop < snapThreshold) {
68+
el.style.top = "0px"
69+
}
70+
if (el.offsetLeft < snapThreshold) {
71+
el.style.left = "0px"
72+
}
73+
}
74+
75+
(function() {
76+
dragmove(document.querySelector("#test"),
77+
document.querySelector("#test .handle"), onStart, onEnd);
78+
79+
dragmove(document.querySelector("#test2"),
80+
document.querySelector("#test2 .handle"), onStart, onEnd);
81+
})();
82+
</script>
83+
</body>
84+
</html>

‎dragmove.js

Copy file name to clipboard
+89Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// https://github.com/knadh/dragmove.js
2+
// Kailash Nadh (c) 2020.
3+
// MIT License.
4+
5+
let _loaded = false;
6+
let _callbacks = [];
7+
const _isTouch = window.ontouchstart !== undefined;
8+
9+
export const dragmove = function(target, handler, onStart, onEnd) {
10+
// Register a global event to capture mouse moves (once).
11+
if (!_loaded) {
12+
document.addEventListener(_isTouch ? "touchmove" : "mousemove", function(e) {
13+
let c = e;
14+
if (e.touches) {
15+
c = e.touches[0];
16+
}
17+
18+
// On mouse move, dispatch the coords to all registered callbacks.
19+
for (var i = 0; i < _callbacks.length; i++) {
20+
_callbacks[i](c.clientX, c.clientY);
21+
}
22+
});
23+
}
24+
25+
_loaded = true;
26+
let isMoving = false, hasStarted = false;
27+
let startX = 0, startY = 0, lastX = 0, lastY = 0;
28+
29+
// On the first click and hold, record the offset of the pointer in relation
30+
// to the point of click inside the element.
31+
handler.addEventListener(_isTouch ? "touchstart" : "mousedown", function(e) {
32+
e.stopPropagation();
33+
e.preventDefault();
34+
if (target.dataset.dragEnabled === "false") {
35+
return;
36+
}
37+
38+
let c = e;
39+
if (e.touches) {
40+
c = e.touches[0];
41+
}
42+
43+
isMoving = true;
44+
startX = target.offsetLeft - c.clientX;
45+
startY = target.offsetTop - c.clientY;
46+
});
47+
48+
// On leaving click, stop moving.
49+
document.addEventListener(_isTouch ? "touchend" : "mouseup", function() {
50+
isMoving = false;
51+
hasStarted = false;
52+
53+
if (onEnd) {
54+
onEnd(target, parseInt(target.style.left), parseInt(target.style.top));
55+
}
56+
});
57+
58+
// Register mouse-move callback to move the element.
59+
_callbacks.push(function move(x, y) {
60+
if (!isMoving) {
61+
return;
62+
}
63+
64+
if (!hasStarted) {
65+
hasStarted = true;
66+
if (onStart) {
67+
onStart(target, lastX, lastY);
68+
}
69+
}
70+
71+
lastX = x + startX;
72+
lastY = y + startY;
73+
74+
// If boundary checking is on, don't let the element cross the viewport.
75+
if (target.dataset.dragBoundary === "true") {
76+
if (lastX < 1 || lastX >= window.innerWidth - target.offsetWidth) {
77+
return;
78+
}
79+
if (lastY < 1 || lastY >= window.innerHeight - target.offsetHeight) {
80+
return;
81+
}
82+
}
83+
84+
target.style.left = lastX + "px";
85+
target.style.top = lastY + "px";
86+
});
87+
}
88+
89+
export { dragmove as default };

‎package.json

Copy file name to clipboard
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "@knadh/dragmove",
3+
"version": "0.1.0",
4+
"description": "A tiny, zero-dependency library for making DOM elements draggable.",
5+
"main": "dragmove.js",
6+
"files": [
7+
"dragmove.js"
8+
],
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/knadh/dragmove.js.git"
15+
},
16+
"keywords": [
17+
"drag",
18+
"drop",
19+
"move"
20+
],
21+
"author": "Kailash Nadh",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/knadh/dragmove.js/issues"
25+
},
26+
"homepage": "https://github.com/knadh/dragmove.js#readme",
27+
"publishConfig": {
28+
"access": "public"
29+
}
30+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.