JSFiddle - React, Tailwind, and code Playground
by mrmartineau
HTML
<div style="position:relative;width:200px;height:200px;left:20px;top:20px;border:solid black 1px;">
<img class="dragclass" src="http://www.izzonet.com/images/newsite/features_new/newDesign/featuresIcon/full_drag&drop.jpg" width="50" height="50" style="position:absolute;left:20px;top:20px;" />
</div>
JavaScript
if (document.getElementById){
(function(){
//Stop Opera selecting anything whilst dragging.
if (window.opera){
document.write("<input type='hidden' id='Q' value=' '>");
}
var n = 500;
var dragok = false;
var y,x,d,dy,dx;
function move(e){
if (!e) e = window.event;
if (dragok){
var lft=dx + e.clientX - x,top=dy + e.clientY - y;
if (lft>0&&lft<d.parentNode.offsetWidth-d.offsetWidth-2&&top>0&&top<d.parentNode.offsetHeight-d.offsetHeight-2){
d.style.left = lft + "px";
d.style.top = top + "px";
}
return false;
}
}
function down(e){
if (!e) e = window.event;
var temp = (typeof e.target != "undefined")?e.target:e.srcElement;
if (temp.tagName != "HTML"|"BODY" && temp.className != "dragclass"){
temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
}
if (temp.className == "dragclass"){
if (window.opera){
document.getElementById("Q").focus();
}
dragok = true;
temp.style.zIndex = n++;
d = temp;
dx = parseInt(temp.style.left+0);
dy = parseInt(temp.style.top+0);
x = e.clientX;
y = e.clientY;
document.onmousemove = move;
return false;
}
}
function up(){
dragok = false;
document.onmousemove = null;
}
document.onmousedown = down;
document.onmouseup = up;
})();
}//End.