JSFiddle - React, Tailwind, and code Playground
by mesak
HTML
<div id="draggable1" class="draggable">可以移動喔!</a></div>
<div id="draggable2" class="draggable">可以移動喔!</a></div>
<div id="draggable3" class="draggable">可以移動喔!</a></div>
<div id="draggable4" class="draggable">可以移動喔!</a></div>
<div id="draggable5" class="draggable">可以移動喔!</a></div>
CSS
.draggable{
background-color:green;
font-size:9pt;
padding:30px;
color:white;
width:360px;
height:224px;
position:absolute;
}
JavaScript
var rDrag = {
o: null,
init: function (o) {
alert( typeof(o) )
o.onmousedown = this.start;
},
start: function (e) {
var o;
e = rDrag.fixEvent(e);
e.preventDefault && e.preventDefault();
rDrag.o = o = this;
o.x = e.clientX - rDrag.o.offsetLeft;
o.y = e.clientY - rDrag.o.offsetTop;
document.onmousemove = rDrag.move;
document.onmouseup = rDrag.end;
},
move: function (e) {
e = rDrag.fixEvent(e);
var oLeft, oTop;
oLeft = e.clientX - rDrag.o.x;
oTop = e.clientY - rDrag.o.y;
rDrag.o.style.left = oLeft + 'px';
rDrag.o.style.top = oTop + 'px';
},
end: function (e) {
e = rDrag.fixEvent(e);
rDrag.o = document.onmousemove = document.onmouseup = null;
},
fixEvent: function (e) {
if (!e) {
e = window.event;
e.target = e.srcElement;
e.layerX = e.offsetX;
e.layerY = e.offsetY;
}
return e;
}
}
window.onload = function () {
var obj = document.getElementById('draggable1');
rDrag.init(obj);
}