-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
143 lines (130 loc) · 3.22 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<!doctype html>
<head>
<meta charset="utf-8">
<style>
body > :first-child {
font-family: sans-serif;
}
.outer .inner {
background: #ddd;
padding: 20px;
}
#svg-target {
min-height: 20px;
min-width: 20px;
outline: 3px solid cyan;
}
.draggable {
background-color: #ddd;
padding: 20px;
display: inline-block;
font-size: 50px;
border-radius: 10px;
font-family: sans-serif;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner">
<svg width="20" height="20" style="vertical-align: middle">
<circle cx="10" cy="10" r="8" stroke="black" stroke-width="3" fill="green" />
</svg>
<span style="display: inline-block; vertical-align: middle">
Drag me!
</span>
</div>
</div>
<button onclick="toggleDrag(window.draggable)">Toggle drag</button>
<div>
<h3>Svg source</h3>
<svg width="100" height="100" id="svg-source">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>
</div>
<div>
<div class="draggable">
A
</div>
<div class="draggable">
B
</div>
<div class="draggable">
C
</div>
<div class="draggable">
D
</div>
<div class="draggable">
E
</div>
<div class="draggable">
F
</div>
</div>
<div>
<h3>Svg target (created using cloneNode)</h3>
<div id="svg-target"></div>
</div>
<div id="lorem"></div>
<script type="module">
import { polyfill, cloneNode } from './src/index.js'
polyfill()
window.draggable = document.querySelector('.outer')
class Move {
onStart(event) {
console.log('onStart')
if (event.cancelable) {
event.preventDefault()
}
this.snapshot = document.createSnapshot(this.element)
this.element.style.opacity = 0
this.element.style.pointerEvents = 'none'
this.initialX = event.snapshotX
this.initialY = event.snapshotY
this.snapshot.place({
x: event.snapshotX,
y: event.snapshotY
})
}
onMove(event) {
console.log('onMove')
this.snapshot.move({
x: event.snapshotX,
y: event.snapshotY,
})
}
onEnd(event) {
console.log('onEnd')
this.snapshot.move({
x: this.initialX,
y: this.initialY,
transition: 300,
})
setTimeout(() => {
this.element.style.pointerEvents = null
this.element.style.opacity = null
this.snapshot.remove()
}, 300)
}
}
document.querySelectorAll('.draggable').forEach(e => {
e.moveHandler = Move
})
draggable.moveHandler = Move
window.toggleDrag = function toggleDrag(draggable) {
if (draggable.moveHandler)
draggable.moveHandler = null
else
draggable.moveHandler = Move
}
document.getElementById('svg-target').appendChild(cloneNode(document.getElementById('svg-source')))
document.getElementById('svg-source').moveHandler = Move
const lorem = document.getElementById('lorem')
for (let i = 0; i < 100; i++) {
const child = document.createElement('div')
child.textContent = 'Lorem ipsum'
lorem.appendChild(child)
}
</script>
</body>