Javascript drag-and-drop simple application--line analysis code, let you easily drag the principle of

Source: Internet
Author: User

Today, let's look at how to drag and drop an object without pulling out a div and dragging the adsorption function.

The last time we dragged our drag is not to drag out the scope of the visual area, on this basis we add a parent div, do not let him drag out of the parent. The principle is as simple as before.

<id= "Div2">            <id= "Div1"  >            </div>        </Div >
<style type= "Text/css" > #div1{width:100px;Height:100px;background:Red;position:Absolute; }#div2{width:400px;Height:300px;background:#CCCCCC;position:relative; }</style>
<script type= "Text/javascript" >//drag empty div lower version of Firefox has bugWindow.onload =function() {                varOdiv = document.getElementById ("Div1"); varODiv2 = document.getElementById ("Div2"); varDISX = 0; varDisy = 0; Odiv.onmousedown=function(EV) {varoevent = EV | |event; DISX= Oevent.clientx-Odiv.offsetleft; Disy= Oevent.clienty-Odiv.offsettop; Document.onmousemove=function(EV) {varoevent = EV | |event; //Store Div's current location                        varOdivleft = Oevent.clientx-Disx; varOdivtop = Oevent.clienty-Disy; if(Odivleft < 0) {Odivleft= 0; } Else if(Odivleft > Odiv2.offsetwidth-odiv.offsetwidth) {odivleft= Odiv2.offsetwidth-Odiv.offsetwidth; }                        if(Odivtop < 0) {Odivtop= 0; } Else if(Odivtop > Odiv2.offsetheight-odiv.offsetheight) {odivtop= Odiv2.offsetheight-Odiv.offsetheight; } oDiv.style.left= Odivleft + ' px '; ODiv.style.top= odivtop + ' px ';                    }; Document.onmouseup=function() {Document.onmousemove=NULL; Document.onmouseup=NULL;                    }; return false;//block default events, fix bugs in Firefox                };        }; </script>

as follows: simple.

The next step is how to get him to automatically snap.

In fact, this people will often use, such as PS inside a small window dragged dragged to the edge of the page, he will automatically adsorption up.

How can we drag and drop such a function?

In fact, before the movement of the time mentioned, like a taxi you can not exactly the driver to stop there, he must be parked near the destination of the place.

That procedure is the same, almost soon to be directly assigned to the value. Assuming that the object I dragged was 50px from the left, I thought he was on the left, and the direct assignment was 0, and he would automatically snap it up.

The principle is simple, see how the code is implemented. You can change it slightly

<script type= "Text/javascript" >window.onload=function() {                varOdiv = document.getElementById ("Div1"); varODiv2 = document.getElementById ("Div2"); varDISX = 0; varDisy = 0; Odiv.onmousedown=function(EV) {varoevent = EV | |event; DISX= Oevent.clientx-Odiv.offsetleft; Disy= Oevent.clienty-Odiv.offsettop; Document.onmousemove=function(EV) {varoevent = EV | |event; varOdivleft = Oevent.clientx-Disx; varOdivtop = Oevent.clienty-Disy; //when left is less than 50, it is automatically returned to 0 to achieve adsorption .                        if(Odivleft < 50) {Odivleft= 0; } Else if(Odivleft > Odiv2.offsetwidth-odiv.offsetwidth) {odivleft= Odiv2.offsetwidth-Odiv.offsetwidth; }                        if(Odivtop < 0) {Odivtop= 0; } Else if(Odivtop > Odiv2.offsetheight-odiv.offsetheight) {odivtop= Odiv2.offsetheight-Odiv.offsetheight; } oDiv.style.left= Odivleft + ' px '; ODiv.style.top= odivtop + ' px ';                    }; Document.onmouseup=function() {Document.onmousemove=NULL; Document.onmouseup=NULL;                    }; return false;            };        }; </script>

The next time you talk about advanced applications, these will be more responsible and more useful. We have perfected our drag and drop function.

For example, the drag of the picture and the selection of text. For example, we now have a drag on the page is only a div, in peacetime development is not encountered.

In fact, in the case of something on the page, what is the problem with this drag-and-drop???

Next time will solve ~ please look forward to

Javascript drag-and-drop simple application--line analysis code, let you easily drag the principle of

Related Article

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.