The movement of the Div

Source: Internet
Author: User

JQuery Drag the Ontology div, copy all of the following code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<style>
#Dialog {
width:200px;
Background: #CCC;
Border:solid 1px #666;
height:80px;
line-height:80px;
Text-align:center;
Position:absolute;
}
</style>
<script src= "Jquery-1.10.2.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
$ (function () {
Divertdialog ($ (' #Dialog '));
});

var divertdialog = function (obj) {
var OffsetX = Obj.offset (). Left;
var OffsetY = Obj.offset (). Top;
Obj.mousedown (function () {
OBJ.CSS (' cursor ', ' move ');
BOOL = true;
var OffsetX = Event.offsetx;
var OffsetY = event.offsety;
Obj.bind ("MouseMove", function () {
if (bool) {
var x = Event.clientx-offsetx;
var y = event.clienty-offsety;
Obj.css (' left ', x);
Obj.css (' top ', y);
}
});
}). MouseUp (function () {
OBJ.CSS (' cursor ', ' default ');
Obj.unbind ("MouseMove");
});
};

</script>


<body>
<div id= "Dialog" style= "float:left" > Drag layer </div>
</body>

JQuery drag Div, equivalent to re-copy a, the original Div does not move, and the clone of the Div dragged

Also copy all of the following code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

<title> Untitled Document </title>

<style>

#Drigging1 {

width:200px;

Background: #CCC;

Border:solid 1px #666;

height:80px;

line-height:80px;

Text-align:center;

Position:absolute;

}

#Drigging2 {

width:200px;

Background: #CCC;

Border:solid 1px #666;

height:80px;

line-height:80px;

Text-align:center;

Position:absolute;

left:16px;

top:105px;

}

</style>

<script src= "Jquery-1-5-2.js" type= "Text/javascript" ></script>

<script type= "Text/javascript" >

$ (function () {

var clonediv= "";

var Bool=false; Identifies whether the element is moved

var offsetx=0; Declares the left value of the div in the current window

var offsety=0; Declares the top value of the div in the current window

function clones ()

{

Clonediv = $ ("#Drigging"). Clone (True). AppendTo ("body");

}

$ ("#Drigging div"). MouseOver (function () {

$ (this). CSS (' cursor ', ' move '),//////When moving the mouse over a dragged Div, set the mouse's style to mobile (move)

})

$ ("#Drigging div"). MouseDown (function () {

Bool=true; set bool to True when the mouse is pressed on the move element

OffsetX = event.offsetx;//Gets the left value of the mouse at the relative offset of the current window and assigns a value to OffsetX

OffsetY = event.offsety; Gets the top value of the mouse at the relative offset of the current window and assigns a value to OffsetY

$ (this). CSS (' cursor ', ' move ');

if ($ (this). attr ("class") = = "Clones")

{

Clonediv = $ (this);

}else

{

Clonediv = $ (this). Clone (True). AddClass ("clones"). AppendTo ("body");

}

SetTimeout ("Clones ()", 1000);

}). MouseUp (function () {

bool=false;////set bool to False when the mouse moves the element up

})

$ (document). MouseMove (function () {

if (!bool)//if BOOL is false then return

Return

When BOOL is true, execute the following code

var x = Event.clientx-offsetx; Event.clientx gets the mouse offset from the client body area, and then subtracts offsetx to get the current push element's X value relative to the current window (minus the offset X in the current window when the mouse starts dragging)

var y = event.clienty-offsety; Event.clienty gets the offset of the mouse relative to the client body area, and then subtracts offsetx to get the current push element relative to the current window's Y value (minus the offset Y in the current window when the mouse has just started dragging)

Clonediv.css ({left:x,top:y});

$ ("#Drigging"). CSS ("left", X);

$ ("#Drigging"). CSS ("top", y);

$ ("#Drigging"). CSS (' cursor ', ' move ');

})

})

</script>

<body>

<div id= "Drigging" >

<div id= "Drigging1" style= "float:left" > Drag layer </div>

<div id= "Drigging2" style= "float:left" > Drag layer 1</div>

</div>

</body>

The movement of the Div

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.