JS: Simple Drag effect

Source: Internet
Author: User

Effect Demo: https://jsfiddle.net/dwqs/b5ywws9f/embedded/result/

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) {    //Gets the captured object    o = obj;    //document.all (IE) binds using setcapture method, others such as FF use
//window Object snapping to eventsDocument.all?o.setcapture (): Window.captureevents (Event.mousemove); X = E.clientx-parseint (o.style.left);//Get width,Y = E.clienty-parseint (o.style.top);//Get height,}document.getelementbyid ("Boxmove"). onmousedown =function(e) {
Boxmove captures events and processes  E-->ff  Window.event-->ie
getobject (this , e| |       event ); };d ocument.onmousemove = function  (dis) { if  (!o) {return; } if  (!dis) {//event  dis = event ; }
//settings Boxmovebox style changes with mouse movement  
O.style.left = dis.clientx-x + "px" ;  
"px";};
function (){   
//Mouse release event handling   
if return; }   
document.all (IE) use releasecapture to unbind;
The rest, for example, FF uses the Window object to capture the event
    Document.all?o.releasecapture (): Window.captureevents (
event.mousemove| Event.mouseup)    ';   //Empty object };

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

Original: http://www.ido321.com/1489.html

Reference Documentation:

Https://developer.mozilla.org/en-US/docs/Web/API/Window/captureEvents

Https://developer.mozilla.org/zh-CN/docs/Web/API/element/setCapture

Https://developer.mozilla.org/zh-CN/docs/Web/API/Document/releaseCapture

JS: Simple Drag effect

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.