Drag-and-drop object-oriented notation

Source: Internet
Author: User

<!---CSS Parts--->

#div1 {
width:200px;
height:300px;
Position:absolute; //To implement drag and drop you need to set div to drag layer
background:red;
}

#div2 {
width:200px;
height:300px;
Position:absolute; //To implement drag and drop you need to set div to drag layer
Background:green;

top:300px;

}

<!---HTML section--->

<div id= "Div1" ></div>
<div id= "Div2" >ddd</div>

<!--script-->

function Drag (ID) {

var _this = this;
THIS.DISX = 0;
This.disy = 0;
This.odiv = document.getElementById (ID);
This.oDiv.onmousedown = function (EV) {//need to save the EV here for use with Firefox
_this.fndown (EV); You need to save the EV to use for Firefox.
};
}

Drag.prototype.fnDown = function (EV) {
var _this = this;
var oevent = EV | | Event
THIS.DISX = Oevent.clientx-this.odiv.offsetleft; //Oevent.clientx Get the current mouse x position this.oDiv.offsetLeft get the left value of the current position floating
This.disy = Oevent.clienty-this.odiv.offsettop; //Oevent.clienty get the current position of the mouse in the Y position this.oDiv.offsetTop get the top value of the current location floating

Document.onmousemove = function (EV) {
_this.fnmove (EV);
};

Document.onmouseup = function () {
_this.fnup ();
};
};

Drag.prototype.fnMove = function (EV) {

var oevent = EV | | Event
This.oDiv.style.left = oevent.clientx-this.disx + ' px '; Oevent.clientx get the current x position of the mouse This.disx get the original x position of the current floating layer
This.oDiv.style.top = Oevent.clienty-this.disy + ' px '; Oevent.clientx get the current x position of the mouse This.disy get the original Y position of the current floating layer
};


Drag.prototype.fnUp = function () {
Document.onmousemove = null;
Document.onmouseup = null;
};

Window.onload = function () {
New Drag (' Div1 ');
New Drag (' Div2 ');
};

<!--script-->

Drag-and-drop object-oriented notation

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.