<div onmouserdown="drag(event, this)" style="background:red; height:100px; width:100px"></div>
function drag(a, b)
{
a = a || window.event;
b.clicked = true;
b.mousePosX = a.clientX;
b.mousePosY = a.clientY;
if(a.preventDefault) a.preventDefault();
else a.returnValue = false;
document.onmouseup = function()
{
b.clicked = false;
}
document.onmousemove = function(a)
{
a = a || window.event;
if(b.clicked)
{
posLeft = !b.style.marginLeft ? b.offsetLeft : parseInt(b.style.marginLeft);
posTop = !b.style.marginTop ? b.offsetTop : parseInt(b.style.marginTop);
mousePosX = a.clientX;
mousePosY = a.clientY;
b.style.marginLeft = posLeft + mousePosX - b.mousePosX + 'px';
b.style.marginTop = posTop + mousePosY - b.mousePosY + 'px';
b.mousePosX = mousePosX;
b.mousePosY = mousePosY;
}
}
}