Drag an HTML element _javascript tips

Source: Internet
Author: User
<script content= "Text/javascript" >
var imousedown = false;
var dragobject = null;
Number.prototype.nan0=function () {return isNaN (this) 0:this;}

Demo 0 Variables
var dragdrops = [];
var curtarget = null;
var lasttarget = null;

function Makedraggable (item) {
if (!item) return;
Item.onmousedown = function (EV) {
Dragobject = this;
Mouseoffset = Getmouseoffset (this, EV);
return false;
}
}

function Getmouseoffset (target, Ev) {
EV = EV | | window.event;

var docpos = getPosition (target);
var mousepos = mousecoords (EV);
return {x:mousepos.x-docpos.x, y:mousepos.y-docpos.y};
}

function GetPosition (e) {
var left = 0;
var top = 0;
while (e.offsetparent) {
Left + = E.offsetleft + (E.currentstyle? parseint (E.currentstyle.borderleftwidth)). NaN0 (): 0);
Top + = E.offsettop + (E.currentstyle? parseint (E.currentstyle.bordertopwidth)). NaN0 (): 0);
e = e.offsetparent;
}

Left + = E.offsetleft + (E.currentstyle? parseint (E.currentstyle.borderleftwidth)). NaN0 (): 0);
Top + = E.offsettop + (E.currentstyle? parseint (E.currentstyle.bordertopwidth)). NaN0 (): 0);

return {x:left, y:top};

}

function Mousecoords (EV) {
if (Ev.pagex | | ev.pagey) {
return {x:ev.pagex, y:ev.pagey};
}
return {
X:ev.clientx + Document.body.scrollleft-document.body.clientleft,
Y:ev.clienty + document.body.scrolltop-document.body.clienttop
};
}

function MouseDown (EV) {
EV = EV | | window.event;
var target = Ev.target | | Ev.srcelement;

if (Target.onmousedown | | target.getattribute (' dragobj ')) {
return false;
}
}

function mouseUp (EV) {

Dragobject = null;

if (dragobject) {
EV = EV | | window.event;
var mousepos = mousecoords (EV);

var DT = Dragobject.getattribute (' DropTarget ');
if (DT) {
var targobj = document.getElementById (DT);
var objpos = getPosition (targobj);
if ((Mousepos.x > Objpos.x) && (Mousepos.y > Objpos.y)
&& (mousepos.x< (Objpos.x+parseint (targobj.offsetwidth))
&& (mousepos.y< (Objpos.y+parseint (targobj.offsetheight))) {
var nsrc = Targobj.getattribute (' newsrc ');
if (NSRC) {
DRAGOBJECT.SRC = NSRC;
settimeout (function () {
if (!dragobject | |!dragobject.parentnode) return;
DragObject.parentNode.removeChild (Dragobject);
Dragobject = null;
}, parseint (Targobj.getattribute (' timeout '));
} else {
DragObject.parentNode.removeChild (Dragobject);
}
}
}
}
Dragobject = null;

Imousedown = false;
}

function MouseMove (EV) {
EV = EV | | window.event;

/*
We are setting target to whatever item the mouse was currently on

Firefox uses event.target here, MSIE uses event.srcelement
*/
var target = Ev.target | | Ev.srcelement;
var mousepos = mousecoords (EV);

if (dragobject) {
dragObject.style.position = ' absolute ';
DragObject.style.top = Mousepos.y-mouseoffset.y;
DragObject.style.left = Mousepos.x-mouseoffset.x;
}

Track the current mouse ' so we can compare ' against it next time
Lmousestate = Imousedown;

This prevents items on the page from being highlighted while dragging
if (Curtarget | | dragobject) return FALSE;
}

function Adddroptarget (item, target) {
Item.setattribute (' DropTarget ', target);
}

Document.onmousemove = MouseMove;
Document.onmousedown = MouseDown;
Document.onmouseup = mouseUp;

Window.onload = function () {
Makedraggable (document.getElementById (' DragImage9 '));
Makedraggable (document.getElementById (' DragImage10 '));
Makedraggable (document.getElementById (' DragImage11 '));
Makedraggable (document.getElementById (' DragImage12 '));

Adddroptarget (document.getElementById (' DragImage9 '), ' TrashImage1 ');
Adddroptarget (document.getElementById (' DragImage10 '), ' TrashImage1 ');
Adddroptarget (document.getElementById (' DragImage11 '), ' TrashImage1 ');
Adddroptarget (document.getElementById (' DragImage12 '), ' TrashImage1 ');
}
</script>
<body>

<fieldset id= "Demo6" style= "height:70px" >




</fieldset>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.