DIV任意拖动的问题

DRAG the DIV

*{font-size:12px}
.dragTable{
font-size:12px;
border-top:1px solid #3366cc;
margin-bottom: 10px;
width:100%;
background-color:#FFFFFF;
}
.dragTR{
cursor:move;
color:#7787cc;
background-color:#e5eef9;
}
td{vertical-align:top;}
#parentTable{
border-collapse:collapse;
letter-spacing:25px;
}

/****JoeLee************E-MAIL:hktx@163.com****QQ:48293707*****11:09 2006-2-9******/
var draged=false;
tdiv=null;
function dragStart(){
ao=event.srcElement;
if((ao.tagName=="TD")||(ao.tagName=="TR"))ao=ao.offsetParent;
else return;
draged=true;
tdiv=document.createElement("div");
tdiv.innerHTML=ao.outerHTML;
tdiv.style.display="block";
tdiv.style.position="absolute";
tdiv.style.filter="alpha(opacity=70)";
tdiv.style.cursor="move";
tdiv.style.width=ao.offsetWidth;
tdiv.style.height=ao.offsetHeight;
tdiv.style.top=getInfo(ao).top;
tdiv.style.left=getInfo(ao).left;
document.body.appendChild(tdiv);
lastX=event.clientX;
lastY=event.clientY;
lastLeft=tdiv.style.left;
lastTop=tdiv.style.top;
try{
ao.dragDrop();
}catch(e){}
}
function draging(){//重要:判断MOUSE的位置
if(!draged)return;
var tX=event.clientX;
var tY=event.clientY;
tdiv.style.left=parseInt(lastLeft)+tX-lastX;
tdiv.style.top=parseInt(lastTop)+tY-lastY;
for(var i=0;i=parentCell.left&&tX=parentCell.top&&tY=parentCell.left&&tX=parentCell.top&&tY=subTable.left&&tX=subTable.top&&tY

function dragEnd(){
if(!draged)return;
draged=false;
mm=ff(150,15);
}
function getInfo(o){//取得坐标
var to=new Object();
to.left=to.right=to.top=to.bottom=0;
var twidth=o.offsetWidth;
var theight=o.offsetHeight;
while(o!=document.body){
to.left+=o.offsetLeft;
to.top+=o.offsetTop;
o=o.offsetParent;
}
to.right=to.left+twidth;
to.bottom=to.top+theight;
return to;
}
function ff(aa,ab){//从GOOGLE网站来,用于恢复位置
var ac=parseInt(getInfo(tdiv).left);
var ad=parseInt(getInfo(tdiv).top);
var ae=(ac-getInfo(ao).left)/ab;
var af=(ad-getInfo(ao).top)/ab;
return setInterval(function(){if(ab

GMAIL
暂时无法显示GMAIL内容
新浪体育
解剖威队独门利器FW28 2万转引擎匹配超级变速器颁奖:辛吉斯欣喜能以冠军起步 印度搭档创下纪录法新社前瞻冬奥短道速滑:中韩唱主角 美加施冷箭
焦点
京广线中断4小时20临客返汉晚点
中国新闻网-湖北分社 - 所有 235 相关报道 ?哈马斯已有总理人选
解放日报报业集团 - 所有 489 相关报道 ?陈水扁是两岸关系麻烦制造者
武汉晨报 - 所有 179 相关报道 ?
中关村在线
新年行情速递 双敏板卡低价推荐 终于等到了,映泰6600GT一降降一百 罗技G15游戏键盘热力促销,代购价仅529元
网易商业
上海GDP增幅去年出现回落应对反倾销 中国鞋企联手对抗欧盟尹家绪操盘南方汽车 长安谋求曲线整体境外上市

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

div版的

var Demos = [];
var nDemos = 8;

// Demo variables
// 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;
var nImg = new Image();

nImg.src = 'images/drag_drop_poof.gif';

// Demo1 variables
var D1Target = 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 should be a "container". Store each
of these items in our current container
*/
for(var i=0; i

/*
Every top level item in these containers should 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

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

cObj.childNodes[j].setAttribute('DragObj', cDrag);
}
}
}

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 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 writeHistory(object, message){
if(!object || !object.parentNode || !object.parentNode.getAttribute) return;
var historyDiv = object.parentNode.getAttribute('history');
if(historyDiv){
historyDiv = document.getElementById(historyDiv);
historyDiv.appendChild(document.createTextNode(object.id+': '+message));
historyDiv.appendChild(document.createElement('BR'));

historyDiv.scrollTop += 50;
}
}

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 mouseMove(ev){
ev = ev || window.event;

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

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

if(Demos[0] || Demos[4]){
// mouseOut event - fires if the item the mouse is on has changed
if(lastTarget && (target!==lastTarget)){
writeHistory(lastTarget, 'Mouse Out Fired');

// 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){
writeHistory(target, 'Mouse Over Fired');

var oClass = target.getAttribute('overClass');
if(oClass){
target.setAttribute('origClass', target.className);
target.className = oClass;
}
}

// if the user is just starting to drag the element
if(iMouseDown && !lMouseState){
writeHistory(target, 'Start Dragging');

// 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

// 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
would 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.cssText = 'FILTER:alpha(opacity=20);border:1px dotted #ff0000';
// loop through each possible drop container
for(var i=0; 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 would 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

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('startWidth')) /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 xPos) &&
((parseInt(getAttribute('startTop')) + parseInt(getAttribute('startHeight'))) > yPos)){

/*
our target is inside of our container so save the container into
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){
if(activeCont!=curTarget.parentNode){
writeHistory(curTarget, 'Moved into '+activeCont.id);
}

// 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] &&
((parseInt(getAttribute('startLeft')) + parseInt(getAttribute('startWidth'))) > xPos) &&
((parseInt(getAttribute('startTop')) + parseInt(getAttribute('startHeight'))) > yPos)){
beforeNode = activeCont.childNodes[i];
}
}
}

// the item being dragged belongs before another item
if(beforeNode){
if(beforeNode!=curTarget.nextSibling){
writeHistory(curTarget, 'Inserted Before '+beforeNode.id);
activeCont.insertBefore(curTarget, beforeNode);
}

// the item being dragged belongs at the end of the current container
} else {
if((curTarget.nextSibling) || (curTarget.parentNode!=activeCont)){
writeHistory(curTarget, 'Inserted at end of '+activeCont.id);
activeCont.appendChild(curTarget);
}
}

// the timeout is here because the container doesn't "immediately" resize
setTimeout(function(){
var contPos = getPosition(activeCont);
activeCont.setAttribute('startWidth', parseInt(activeCont.offsetWidth));
activeCont.setAttribute('startHeight', parseInt(activeCont.offsetHeight));
activeCont.setAttribute('startLeft', contPos.x);
activeCont.setAttribute('startTop', contPos.y);}, 5);
// make our drag item visible
if(curTarget.style.display!=''){
writeHistory(curTarget, 'Made Visible');
curTarget.style.display = '';
curTarget.style.visibility = 'visible';
}
} else {

// our drag item is not in a container, so hide it.
if(curTarget.style.display!='none'){
writeHistory(curTarget, 'Hidden');
curTarget.style.display = 'none';
}
}
}

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

// mouseMove target
lastTarget = target;
}
if(Demos[2]){
document.getElementById('MouseXPosition').value = mousePos.x;
document.getElementById('MouseYPosition').value = mousePos.y;
}

if(dragObject){
dragObject.style.position = 'absolute';
dragObject.style.top = mousePos.y - mouseOffset.y;
dragObject.style.left = mousePos.x - mouseOffset.x;
}

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

// this prevents items on the page from being highlighted while dragging
if(curTarget || dragObject) return false;
}

function mouseUp(ev){

if(Demos[0] || Demos[4]){
if(curTarget){
writeHistory(curTarget, 'Mouse Up Fired');

dragHelper.style.display = 'none';
if(curTarget.style.display == 'none'){
if(rootSibling){
rootParent.insertBefore(curTarget, rootSibling);
} else {
rootParent.appendChild(curTarget);
}
}
curTarget.style.display = '';
curTarget.style.visibility = 'visible';
curTarget.style.cssText = 'FILTER:alpha(opacity=100);border:1px solid #000';
}
curTarget = null;
}
if(Demos[6] && dragObject){
ev = ev || window.event;
var mousePos = mouseCoords(ev);

var dT = dragObject.getAttribute('droptarget');
if(dT){
var targObj = document.getElementById(dT);
var objPos = getPosition(targObj);
if((mousePos.x > objPos.x) && (mousePos.y > objPos.y) && (mousePos.x

dragObject = null;

iMouseDown = false;
}

function mouseDown(ev){
ev = ev || window.event;
var target = ev.target || ev.srcElement;

iMouseDown = true;
if(Demos[0] || Demos[4]){
if(lastTarget){
writeHistory(lastTarget, 'Mouse Down Fired');
}
}
if(target.onmousedown || target.getAttribute('DragObj')){
return false;
}
}

function makeDraggable(item){
if(!item) return;
item.onmousedown = function(ev){
dragObject = this;
mouseOffset = getMouseOffset(this, ev);
return false;
}
}

function makeClickable(item){
if(!item) return;
item.onmousedown = function(ev){
document.getElementById('ClickImage').value = this.name;
}
}

function addDropTarget(item, target){
item.setAttribute('droptarget', target);
}

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

window.onload = function(){
for(var i=0; i

if(Demos[0]){
CreateDragContainer(document.getElementById('DragContainer1'), document.getElementById('DragContainer2'), document.getElementById('DragContainer3'));
CreateDragContainer(document.getElementById('DragContainer7'));
CreateDragContainer(document.getElementById('DragContainer8'));
}
if(Demos[4]){
CreateDragContainer(document.getElementById('DragContainer1'), document.getElementById('DragContainer2'));
}
if(Demos[0] || Demos[4]){
// Create our helper object that will show the item while dragging
dragHelper = document.createElement('DIV');
dragHelper.style.cssText = 'position:absolute;display:none;';

document.body.appendChild(dragHelper);
}
if(Demos[1]){
makeDraggable(document.getElementById('DragImage1'));
makeDraggable(document.getElementById('DragImage2'));
makeDraggable(document.getElementById('DragImage3'));
makeDraggable(document.getElementById('DragImage4'));
}
if(Demos[5]){
makeDraggable(document.getElementById('DragImage5'));
makeDraggable(document.getElementById('DragImage6'));
makeDraggable(document.getElementById('DragImage7'));
makeDraggable(document.getElementById('DragImage8'));
}
if(Demos[6]){
makeDraggable(document.getElementById('DragImage9'));
makeDraggable(document.getElementById('DragImage10'));
makeDraggable(document.getElementById('DragImage11'));
makeDraggable(document.getElementById('DragImage12'));

addDropTarget(document.getElementById('DragImage9'), 'TrashImage1');
addDropTarget(document.getElementById('DragImage10'), 'TrashImage1');
addDropTarget(document.getElementById('DragImage11'), 'TrashImage1');
addDropTarget(document.getElementById('DragImage12'), 'TrashImage1');
}
if(Demos[3]){
makeClickable(document.getElementById('ClickImage1'));
makeClickable(document.getElementById('ClickImage2'));
makeClickable(document.getElementById('ClickImage3'));
makeClickable(document.getElementById('ClickImage4'));
}
}

#Demo4{margin-left:14px;}
.DragContainer {float:left;width:360px;margin:5px;padding:5px;}
.OverDragContainer {border:#ccc 2px solid;}
.DragBox {border:#000 1px solid;width:350px;height:auto;background:#eee url(../images/right_title.gif) no-repeat left top;margin-bottom:5px;padding-top:5px;}
.OverDragBox {border: #000 1px solid;width:350px;cursor:move;background:#eee url(../images/right_title.gif) no-repeat left top;;margin-bottom:5px;padding-top:5px;}
.DragDragBox {border: #000 1px solid;width:350px;cursor:move;background:#eee url(../images/right_title.gif) no-repeat left top;;margin-bottom:5px;padding-top:5px;}
.DragDragBox {FILTER:alpha(opacity=70);background:#eee url(../images/right_title.gif) no-repeat left top;}
.DragBox img{margin-left:10px;}
.DragDragBox img{margin-left:10px;}
.OverDragBox img{margin-left:10px;}
egend {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}
.content{padding-left:8px;clear:both;height:25px;background:#fff;margin-top:5px;}
.content li{list-style:none;width:75px;line-height:25px;float:left;}
.history{background:#eee;}

本页面元素可以随意拖拽

顶级站点

  • 搜狐
  • 新浪
  • 网易
  • 腾讯QQ
  • TOM
  • 中华网
  • 21CN
  • 搜狗
  • Google
  • 百度
  • 雅虎
  • MSN中国
  • Item #2

    欢迎您来到www.achome.cn,以后要常来噢

    Item #3

    Item #4

    Item #5

    Item #6

    [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

    (0)

    相关推荐

    • DIV任意拖动的问题

      DRAG the DIV *{font-size:12px} .dragTable{ font-size:12px; border-top:1px solid #3366cc; margin-bottom: 10px; width:100%; background-color:#FFFFFF; } .dragTR{ cursor:move; color:#7787cc; background-color:#e5eef9; } td{vertical-align:top;} #parentTabl

    • react-dnd实现任意拖动与互换位置

      本文实例为大家分享了react-dnd实现任意拖动与互换位置的具体代码,供大家参考,具体内容如下 react-dnd用法 hooks组件 1.使用DndProvider定义一个可以拖拽的范围 import { HTML5Backend } from 'react-dnd-html5-backend'; import { DndProvider } from 'react-dnd'; class App extends Component{   render () {     return (  

    • JavaScript:Div层拖动效果实例代码

      Div层拖动效果图: 实现:CSS: 复制代码 代码如下: <style>div{position:relative;}</style> JS: 复制代码 代码如下: <script type="text/javascript"> var mouseover=truevar xcoor;var ycoor;function coordinates(){ if (event.srcElement.id.indexOf("wishbroad&q

    • 纯js实现的积木(div层)拖动功能示例

      本文实例讲述了纯js实现的积木(div层)拖动功能.分享给大家供大家参考,具体如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>拖动</title> <style type="text/css"> </style> </head> <bo

    • jQuery实现随意改变div任意属性的名称和值(部分原生js实现)

      一些简单的例子,用原生js和jQuery实现随意改变div属性,和重置.代码如下: 复制代码 代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>函数传参,改变Div任意属性的值</title> <style type="text/css"> body,p{margin:0;padding:0;} bo

    • JavaScript实现DIV层拖动及动态增加新层的方法

      本文实例讲述了JavaScript实现DIV层拖动及动态增加新层的方法.分享给大家供大家参考.具体分析如下: 无刷新添加一个新的DIV层,并可实现的该层的拖动,鼠标拖动层可移动位置,将JS部分另存为一个新文件,用到的时候从外部引入,这个拖动层代码很流行,GG和YAHOO等大网站经常可以看到这种效果. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=

    • js实现div色块拖动录制

      本文实例为大家分享了js实现div色块拖动录制的具体代码,供大家参考,具体内容如下 描述: 实现一个div50*50的色块,拖动它生成一个轨迹,松手后,这个div会重复你刚才拖动的这个路径. 效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style&g

    • JS+DIV实现拖动效果

      本文实例为大家分享了JS+DIV实现拖动效果的具体代码,供大家参考,具体内容如下 效果图 思路 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="main" style=&qu

    • Android 实现可任意拖动的悬浮窗功能(类似悬浮球)

      最近开发项目中,有个在屏幕上任意拖动的悬浮窗功能,其实就是利用 WindowManager的api来完成这个需求,具体的实现的功能如下: 1.自定义view import android.content.Context; import android.content.Intent; import android.os.Handler; import android.os.Message; import android.util.Log; import android.util.TypedValu

    • Android 自定义LineLayout实现满屏任意拖动功能的示例代码

      1.前言 在开发中,会有需求实现控件在屏幕随意拖动,这就需要自定义View,然后在OnTouchEvent事件中,处理MotionEvent.ACTION_MOVE事件,然后通过坐标点传值给onlayout方法,来实现控件的任意拖动,具体代码如下: import android.content.Context; import android.util.AttributeSet; import android.view.Display; import android.view.MotionEven

    随机推荐