Drag the Html element set Drag and Drop any item

Source: Internet
Author: User

<Style content = "text/css">
Li {
MARGIN-BOTTOM: 10px
}
Ul {
MARGIN-TOP: 5px
}
. DragContainer {
BORDER-RIGHT: #669999 2px solid; PADDING-RIGHT: 5px; BORDER-TOP: #669999 2px solid; PADDING-LEFT: 5px; FLOAT: left; PADDING-BOTTOM: 0px; MARGIN: 3px; BORDER-LEFT: #669999 2px solid; WIDTH: 100px; PADDING-TOP: 5px; BORDER-BOTTOM: #669999 2px solid
}
. OverDragContainer {
BORDER-RIGHT: #669999 2px solid; PADDING-RIGHT: 5px; BORDER-TOP: #669999 2px solid; PADDING-LEFT: 5px; FLOAT: left; PADDING-BOTTOM: 0px; MARGIN: 3px; BORDER-LEFT: #669999 2px solid; WIDTH: 100px; PADDING-TOP: 5px; BORDER-BOTTOM: #669999 2px solid
}
. OverDragContainer {
BACKGROUND-COLOR: # eee
}
. DragBox {
BORDER-RIGHT: #000 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #000 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10px; MARGIN-BOTTOM: 5px; PADDING-BOTTOM: 2px; BORDER-LEFT: #000 1px solid; WIDTH: 94px; CURSOR: pointer; PADDING-TOP: 2px; BORDER-BOTTOM: #000 1px solid; FONT-FAMILY: verdana, tahoma, arial; BACKGROUND-COLOR: # eee
}
. OverDragBox {
BORDER-RIGHT: #000 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #000 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10px; MARGIN-BOTTOM: 5px; PADDING-BOTTOM: 2px; BORDER-LEFT: #000 1px solid; WIDTH: 94px; CURSOR: pointer; PADDING-TOP: 2px; BORDER-BOTTOM: #000 1px solid; FONT-FAMILY: verdana, tahoma, arial; BACKGROUND-COLOR: # eee
}
. DragDragBox {
BORDER-RIGHT: #000 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #000 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10px; MARGIN-BOTTOM: 5px; PADDING-BOTTOM: 2px; BORDER-LEFT: #000 1px solid; WIDTH: 94px; CURSOR: pointer; PADDING-TOP: 2px; BORDER-BOTTOM: #000 1px solid; FONT-FAMILY: verdana, tahoma, arial; BACKGROUND-COLOR: # eee
}
. MiniDragBox {
BORDER-RIGHT: #000 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #000 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10px; MARGIN-BOTTOM: 5px; PADDING-BOTTOM: 2px; BORDER-LEFT: #000 1px solid; WIDTH: 94px; CURSOR: pointer; PADDING-TOP: 2px; BORDER-BOTTOM: #000 1px solid; FONT-FAMILY: verdana, tahoma, arial; BACKGROUND-COLOR: # eee
}
. OverDragBox {
BACKGROUND-COLOR: # ffff99
}
. DragDragBox {
BACKGROUND-COLOR: # ffff99
}
. DragDragBox {
FILTER: alpha (opacity = 50); BACKGROUND-COLOR: # ff99cc
}
LEGEND {
FONT-WEIGHT: bold; FONT-SIZE: 12px; COLOR: #666699; FONT-FAMILY: verdana, tahoma, arial
}
FIELDSET {
PADDING-RIGHT: 3px; PADDING-LEFT: 3px; PADDING-BOTTOM: 3px; PADDING-TOP: 3px
}
. History {
FONT-SIZE: 10px; OVERFLOW: auto; WIDTH: 100%; FONT-FAMILY: verdana, tahoma, arial; HEIGHT: 82px
}
# DragContainer8 {
BORDER-RIGHT: #669999 1px solid; PADDING-RIGHT: 0px; BORDER-TOP: #669999 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 0px; BORDER-LEFT: #669999 1px solid; WIDTH: 110px; PADDING-TOP: 5px; BORDER-BOTTOM: #669999 1px solid; HEIGHT: 110px
}
. MiniDragBox {
FLOAT: left; MARGIN: 0px 5px 5px 0px; WIDTH: 20px; HEIGHT: 20px
}
Pre {border: 1 solid # CCC; background-color: # F8F8F0; padding: 10px ;}
</Style>
<Script content = "text/javascript">

// IMouseDown represents the current mouse button state: up or down
/*
LMouseState represents the previous mouse button state so that we can
Check for button clicks and button releases:

If (iMouseDown &&! LMouseState) // button just clicked!
If (! IMouseDown & lMouseState) // button just released!
*/
Var mouseOffset = null;
Var iMouseDown = false;
Var lMouseState = false;
Var dragObject = null;

// Demo 0 variables
Var DragDrops = [];
Var curTarget = null;
Var lastTarget = null;
Var dragHelper = null;
Var tempDiv = null;
Var rootParent = null;
Var rootSibling = null;

Number. prototype. NaN0 = function () {return isNaN (this )? 0: this ;}

Function CreateDragContainer (){
/*
Create a new "Container Instance" so that items from one "Set" can not
Be dragged into items from another "Set"
*/
Var cDrag = DragDrops. length;
DragDrops [cDrag] = [];

/*
Each item passed to this function shocould be a "container". Store each
Of these items in our current container
*/
For (var I = 0; I <arguments. length; I ++ ){
Var cObj = arguments [I];
DragDrops [cDrag]. push (cObj );
CObj. setAttribute ('dropobj ', cDrag );

/*
Every top level item in these containers shoshould be draggable. Do this
By setting the DragObj attribute on each item and then later checking
This attribute in the mouseMove function
*/
For (var j = 0; j <cObj. childNodes. length; j ++ ){

// Firefox puts in lots of # text nodes... skip these
If (cObj. childNodes [j]. nodeName = '# text') continue;

CObj. childNodes [j]. setAttribute ('dragobj ', cDrag );
}
}
}

Function mouseCoords (ev ){
If (ev. pageX | ev. pageY ){
Return {x: ev. pageX, y: ev. pageY };
}
Return {
X: ev. clientX + document. body. scrollLeft-document. body. clientLeft,
Y: ev. clientY + document. body. scrollTop-document. body. clientTop
};
}

Function getMouseOffset (target, ev ){
Ev = ev | window. event;

Var docPos = getPosition (target );
Var mousePos = mouseCoords (ev );
Return {x: mousePos. x-docPos. x, y: mousePos. y-docPos. y };
}

Function getPosition (e ){
Var left = 0;
Var top = 0;
While (e. offsetParent ){
Left + = e. offsetLeft + (e. currentStyle? (ParseInt (e. currentStyle. borderLeftWidth). NaN0 (): 0 );
Top + = e. offsetTop + (e. currentStyle? (ParseInt (e. currentStyle. borderTopWidth). NaN0 (): 0 );
E = e. offsetParent;
}

Left + = e. offsetLeft + (e. currentStyle? (ParseInt (e. currentStyle. borderLeftWidth). NaN0 (): 0 );
Top + = e. offsetTop + (e. currentStyle? (ParseInt (e. currentStyle. borderTopWidth). NaN0 (): 0 );

Return {x: left, y: top };

}

Function mouseMove (ev ){
Ev = ev | window. event;

/*
We are setting target to whatever item the mouse is currently on

Firefox uses event.tar get here, MSIE uses event. srcElement
*/
Var target = ev.tar get | ev. srcElement;
Var mousePos = mouseCoords (ev );

// MouseOut event-fires if the item the mouse is on has changed
If (lastTarget & (target! = LastTarget )){
// Reset the classname for the target element
Var origClass = lastTarget. getAttribute ('origclass ');
If (origClass) lastTarget. className = origClass;
}

/*
DragObj is the grouping our item is in (set from the createDragContainer function ).
If the item is not in a grouping we ignore it since it can't be dragged with this
Script.
*/
Var dragObj = target. getAttribute ('dragobj ');

// If the mouse was moved over an element that is draggable
If (dragObj! = Null ){

// MouseOver event-Change the item's class if necessary
If (target! = LastTarget ){
Var oClass = target. getAttribute ('overclass ');
If (oClass ){
Target. setAttribute ('originclass', target. className );
Target. className = oClass;
}
}

// If the user is just starting to drag the element
If (iMouseDown &&! LMouseState ){
// MouseDown target
CurTarget = target;

// Record the mouse x and y offset for the element
RootParent = curTarget. parentNode;
RootSibling = curTarget. nextSibling;

MouseOffset = getMouseOffset (target, ev );

// We remove anything that is in our dragHelper DIV so we can put a new item in it.
For (var I = 0; I <dragHelper. childNodes. length; I ++) dragHelper. removeChild (dragHelper. childNodes [I]);

// Make a copy of the current item and put it in our drag helper.
DragHelper. appendChild (curTarget. cloneNode (true ));
DragHelper. style. display = 'block ';

// Set the class on our helper DIV if necessary
Var dragClass = curTarget. getAttribute ('dragclass ');
If (dragClass ){
DragHelper. firstChild. className = dragClass;
}

// Disable dragging from our helper DIV (it's already being dragged)
DragHelper. firstChild. removeAttribute ('dragobj ');

/*
Record the current position of all drag/drop targets related
To the element. We do this here so that we do not have to do
It on the general mouse move event which fires when the mouse
Moves even 1 pixel. If we don't do this here the script
Wocould run much slower.
*/
Var dragConts = DragDrops [dragObj];

/*
First record the width/height of our drag item. Then hide it since
It is going to (potentially) be moved out of its parent.
*/
CurTarget. setAttribute ('startwidth', parseInt (curTarget. offsetWidth ));
CurTarget. setAttribute ('startheight ', parseInt (curTarget. offsetHeight ));
CurTarget. style. display = 'none ';

// Loop through each possible drop container
For (var I = 0; I <dragConts. length; I ++ ){
With (dragConts [I]) {
Var pos = getPosition (dragConts [I]);

/*
Save the width, height and position of each container.

Even though we are saving the width and height of each
Container back to the container this is much faster because
We are saving the number and do not have to run through
Any calculations again. Also, offsetHeight and offsetWidth
Are both fairly slow. You wowould never normally notice any
Performance hit from these two functions but our code is
Going to be running hundreds of times each second so every
Little bit helps!

Note that the biggest performance gain here, by far, comes
From not having to run through the getPosition function
Hundreds of times.
*/
SetAttribute ('startwidth', parseInt (offsetWidth ));
SetAttribute ('startheight ', parseInt (offsetHeight ));
SetAttribute ('startleft', pos. x );
SetAttribute ('starttop ', pos. y );
}

// Loop through each child element of each container
For (var j = 0; j <dragConts [I]. childNodes. length; j ++ ){
With (dragConts [I]. childNodes [j]) {
If (nodeName = '# text') | (dragConts [I]. childNodes [j] = curTarget) continue;

Var pos = getPosition (dragConts [I]. childNodes [j]);

// Save the width, height and position of each element
SetAttribute ('startwidth', parseInt (offsetWidth ));
SetAttribute ('startheight ', parseInt (offsetHeight ));
SetAttribute ('startleft', pos. x );
SetAttribute ('starttop ', pos. y );
}
}
}
}
}

// If we get in here we are dragging something
If (curTarget ){
// Move our helper div to wherever the mouse is (adjusted by mouseOffset)
DragHelper. style. top = mousePos. y-mouseOffset. y;
DragHelper. style. left = mousePos. x-mouseOffset. x;

Var dragConts = DragDrops [curTarget. getAttribute ('dragobj ')];
Var activeCont = null;

Var xPos = mousePos. x-mouseOffset. x + (parseInt (curTarget. getAttribute ('startwidths')/2 );
Var yPos = mousePos. y-mouseOffset. y + (parseInt (curTarget. getAttribute ('startheight ')/2 );

// Check each drop container to see if our target object is "inside" the container
For (var I = 0; I <dragConts. length; I ++ ){
With (dragConts [I]) {
If (getAttribute ('startleft') <xPos )&&
(GetAttribute ('starttop') <yPos )&&
(GetAttribute ('startleft') + getAttribute ('startwidth')> xPos )&&
(GetAttribute ('starttop') + getAttribute ('startheight ')> yPos )){

/*
Our target is inside of our container so save the container
The activeCont variable and then exit the loop since we no longer
Need to check the rest of the containers
*/
ActiveCont = dragConts [I];

// Exit the for loop
Break;
}
}
}

// Our target object is in one of our containers. Check to see where our div belongs
If (activeCont ){
// BeforeNode will hold the first node AFTER where our div belongs
Var beforeNode = null;

// Loop through each child node (skipping text nodes ).
For (var I = activeCont. childNodes. length-1; I> = 0; I --){
With (activeCont. childNodes [I]) {
If (nodeName = '# text') continue;

// If the current item is "After" the item being dragged
If (
CurTarget! = ActiveCont. childNodes [I] &
(GetAttribute ('startleft') + getAttribute ('startwidth')> xPos )&&
(GetAttribute ('starttop') + getAttribute ('startheight ')> yPos )){
BeforeNode = activeCont. childNodes [I];
}
}
}

// The item being dragged belongs before another item
If (beforeNode ){
If (beforeNode! = CurTarget. nextSibling ){
ActiveCont. insertBefore (curTarget, beforeNode );
}

// The item being dragged belongs at the end of the current container
} Else {
If (curTarget. nextSibling) | (curTarget. parentNode! = ActiveCont )){
ActiveCont. appendChild (curTarget );
}
}

// Make our drag item visible
If (curTarget. style. display! = ''){
CurTarget. style. display = '';
}
} Else {

// Our drag item is not in a container, so hide it.
If (curTarget. style. display! = 'None '){
CurTarget. style. display = 'none ';
}
}
}

// Track the current mouse state so we can compare against it next time
LMouseState = iMouseDown;

// MouseMove target
LastTarget = target;

// Track the current mouse state so we can compare against it next time
LMouseState = iMouseDown;

// This helps prevent items on the page from being highlighted while dragging
Return false;
}

Function mouseUp (ev ){
If (curTarget ){
// Hide our helper object-it is no longer needed
DragHelper. style. display = 'none ';

// If the drag item is invisible put it back where it was before moving it
If (curTarget. style. display = 'None '){
If (rootSibling ){
RootParent. insertBefore (curTarget, rootSibling );
} Else {
RootParent. appendChild (curTarget );
}
}

// Make sure the drag item is visible
CurTarget. style. display = '';
}
CurTarget = null;
IMouseDown = false;
}

Function mouseDown (){
IMouseDown = true;
If (lastTarget ){
Return false;
}
}

Document. onmousemove = mouseMove;
Document. onmousedown = mouseDown;
Document. onmouseup = mouseUp;

Window. onload = function (){
// Create our helper object that will show the item while dragging
DragHelper = document. createElement ('div ');
DragHelper.style.css Text = 'position: absolute; display: none ;';

CreateDragContainer (
Document. getElementById ('dragininer1 '),
Document. getElementById ('dragininer2 '),
Document. getElementById ('dragcontainer3 ')
);

Document. body. appendChild (dragHelper );
}

</Script>

<! -- The mouse over and dragging class are defined on each item -->

<Div class = "DragContainer" id = "DragContainer1">
<Div class = "DragBox" id = "Item1" overClass = "OverDragBox" dragClass = "DragDragBox"> Item #1 </div>
<Div class = "DragBox" id = "Item2" overClass = "OverDragBox" dragClass = "DragDragBox"> Item #2 </div>
<Div class = "DragBox" id = "Item3" overClass = "OverDragBox" dragClass = "DragDragBox"> Item #3 </div>
<Div class = "DragBox" id = "Item4" overClass = "OverDragBox" dragClass = "DragDragBox"> Item #4 </div>
</Div>
<Div class = "DragContainer" id = "DragContainer2">
<Div class = "DragBox" id = "Item5" overClass = "OverDragBox" dragClass = "DragDragBox"> Item #5 </div>
<Div class = "DragBox" id = "Item6" overClass = "OverDragBox" dragClass = "DragDragBox"> Item #6 </div>
<Div class = "DragBox" id = "Item7" overClass = "OverDragBox" dragClass = "DragDragBox"> Item #7 </div>
<Div class = "DragBox" id = "Item8" overClass = "OverDragBox" dragClass = "DragDragBox"> Item #8 </div>
</Div>
<Div class = "DragContainer" id = "DragContainer3">
<Div class = "DragBox" id = "Item9" overClass = "OverDragBox" dragClass = "DragDragBox"> Item #9 </div>
<Div class = "DragBox" id = "Item10" overClass = "OverDragBox" dragClass = "DragDragBox"> Item #10 </div>
<Div class = "DragBox" id = "Item11" overClass = "OverDragBox" dragClass = "DragDragBox"> Item #11 </div>
<Div class = "DragBox" id = "Item12" overClass = "OverDragBox" dragClass = "DragDragBox"> Item #12 </div>
</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.