I want to use html 5 drag and drop in vue js .
I see the w3schools tutorial about drag and drop . It works in a simple html file but doesn’t work in my vue project
My tutorials code and link is : w3schools – drag : https://www.w3schools.com/jsref/event_ondrag.asp and my error says : Uncaught ReferenceError: allowDrop is not defined
I define all methods in method scope in vue js.
Advertisement
Answer
you just need to call the vue event not the html event v-on:drop
instead of drop
for example
here is the implementation of the example you provided in the link with vue
JavaScript
x
74
74
1
<html>
2
<head>
3
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
4
<style>
5
.droptarget {
6
float: left;
7
width: 100px;
8
height: 35px;
9
margin: 15px;
10
padding: 10px;
11
border: 1px solid #aaaaaa;
12
}
13
</style>
14
</style>
15
</head>
16
<body>
17
<div id="app">
18
<p>Drag the p element back and forth between the two rectangles:</p>
19
<div
20
class="droptarget"
21
v-on:drop="drop"
22
v-on:dragover="allowDrop"
23
>
24
<p
25
v-on:dragstart="dragStart"
26
v-on:drag="dragging"
27
draggable="true"
28
id="dragtarget"
29
>
30
Drag me!
31
</p>
32
</div>
33
34
<div
35
class="droptarget"
36
v-on:drop="drop"
37
v-on:dragover="allowDrop"
38
></div>
39
40
<p style="clear:both;">
41
<strong>Note:</strong> drag events are not supported in Internet
42
Explorer 8 and earlier versions or Safari 5.1 and earlier versions.
43
</p>
44
45
<p id="demo"></p>
46
</div>
47
<script>
48
var app = new Vue({
49
el: "#app",
50
51
methods: {
52
dragStart:function(event) {
53
event.dataTransfer.setData("Text", event.target.id);
54
},
55
dragging:function(event) {
56
document.getElementById("demo").innerHTML =
57
"The p element is being dragged";
58
},
59
allowDrop:function(event) {
60
event.preventDefault();
61
},
62
drop:function(event) {
63
event.preventDefault();
64
var data = event.dataTransfer.getData("Text");
65
event.target.appendChild(document.getElementById(data));
66
document.getElementById("demo").innerHTML =
67
"The p element was dropped";
68
}
69
70
}
71
});
72
</script>
73
</body>
74
</html>