JavaScript to achieve simple drag effect

Source: Internet
Author: User


Html:

<div class= "wrap" > <div id= "boxmove" style= "left:150px;top:150px"
;        > Movebox </div>
</div>
css
#boxmove {
position:absolute;
width:200px;
border:1px solid #ccc;
height:200px;
color:red;
Background-color:blue;
Text-align:center;
font-size:30px;
}


Js

var o,   //captured event       x,  //boxmove horizontal width        y;  //boxmove Vertical Height Function getobject (obj,e) {   

 //get captured Objects     o = obj;     // document.all (IE) is bound using the SetCapture method; others such as FF use window objects for event capture      document.all?o.setcapture ()  : window.captureevents (event.mousemove);        x = e.clientx - parseint (o.style.left);    //gain width,   

  y = e.clienty - parseint (o.style.top);    //get height,} document.getElementById ("Boxmove"). Onmousedown = function (e) {      getobject (this,e| | event);        //boxmove capturing events and handling   e-->FF  

Window.event-->ie}; Document.onmousemove = function (DIS) {    //Mouse Mobile Event handling     if (!o) {        

   return; &NBSP;&NBSP;&NBSP;&NBSP}     if (!dis) {  //event       

  dis = event ; &NBSP;&NBSP;&NBSP;&NBSP}     o.style.left = dis.clientx - x + "px" ;      //set Boxmovebox style changes with mouse movement     o.style.top = 

dis.clienty - y +  "px";

}; Document.onmouseup = function () {    //Mouse release event handling     if (!o) {

            return;     }     //  document.all (IE) unbind using ReleaseCapture, others such as FF using Window objects for event capture     document.all? O.releasecapture ()  : window.captureevents (event.mousemove| Event.mouseup)     o =  ';    //empty Object}; 

Note that you need to change the left and top of the div. These two properties need to be given inline, otherwise no.

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.