http://topic.csdn.net/u/20080702/17/a9c334f0-e983-4981-9dc2-b4cea201e0d1.html?seed=1906517763
参考下wc的拖拽处理
参考下wc的拖拽处理
解决方案 »
- jquery 求解释下面代码中的 cls : "btn_blue_s" 作用
- asp文本框限制
- jquary ajax方法 操作asp.net,json输出类型为object
- javascript实现只能输入数字和移动光标
- div如何拖出有滚动条的容器
- js 能否实现对 一个变量的监听 事件!大家帮帮忙,谢谢 !
- ◎◎◎急阿!我的第一个js程序:跑不动:(((
- 语言
- ajax动态生成一个div,在div里有一个详细点击事件,为什么每次一加载的时候就默认执行,点击的时候就没反应
- Jquery中的Dialog 窗口上的富文本编辑器(CKEditor 或者freetextbox)产生冲突,文本框无法编辑输入文本问题
- Firefox下javascript 出错提示“ (¹Õ Location.toString öCP ”是怎么回事??
- 在ie的地址栏里输入网址后,当页面打开的时候,如何自动隐藏ie地址址,状态栏,全屏等...
<HTML>
<HEAD>
<TITLE> Drag </TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
var moveobj;
var left;
var top;
function Movedown(obj){
moveobj = obj;
moveobj.setCapture();
left = event.x - moveobj.style.pixelLeft;
top = event.y - moveobj.style.pixelTop;
}window.document.attachEvent("onmouseup",function(){
moveobj.releaseCapture();
moveobj = null;
});function Move(obj){
if(moveobj!=null){
moveobj.style.pixelLeft = event.x - left;
moveobj.style.pixelTop = event.y - top;
}
}
//-->
</SCRIPT>
</HEAD><BODY>
<table width="100" border="1" onselectstart="return false" style="position:absolute;left:50;top:50" onmousedown="Movedown(this)" onmousemove="Move(this)">
<tr>
<td bgcolor="#CCCCCC" align="center" style="cursor:move">托拽1</td>
</tr>
<td align="center" height="60">程序天下</td>
<tr></tr>
</table> <table width="100" border="1" onselectstart="return false" style="position:absolute;left:350;top:250" onmousedown="Movedown(this)" onmousemove="Move(this)">
<tr>
<td bgcolor="#CCCCCC" align="center" style="cursor:move">托拽2</td>
</tr>
<td align="center" height="60">程序天下</td>
<tr></tr>
</table>
</BODY>
</HTML>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Freedom</title><style type="text/css">
html, body {
width:100%;
max-height:100%;
padding:0px;
margin:0px;
}body {
text-align:center;
font-size:14px;
}.cell {
float:left;
clear:right;
}.row {
clear:both;
}.r_nbsp {
width:20px;
}.root {
width:868px;
margin: 0 auto;
}.root * {
/*次属性FF的说*/
-moz-user-select:none;
}.line {
width:202px;
height:0px;
overflow:hidden;
}.move {
border:#CCCCCC 1px solid;
width:200px;
height:aotu;
margin-top:5px;
margin-bottom:5px;
}.title {
height:24px;
cursor:move;
background:#0080C0;
font-size:12px;
font-weight:bold;
color:#FFFFFF;
line-height:24px;
text-align:center;
}.content {
border-top:#CCCCCC 1px solid;
height:100px;
background-color:#F7F7F7;
}.CDrag_temp_div {
border:#CCCCCC 1px dashed;
margin-top:5px;
margin-bottom:5px;
}a#DEL_CDrag {
color:#6699CC;
text-decoration:none;
}a#DEL_CDrag:hover {
color:#FF0000;
}
</style><script type="text/javascript">
var Class = {
//创建类
create: function () {
return function () {
this.initialize.apply(this, arguments);
};
}
};var $A = function (a) {
//转换数组
return a ? Array.apply(null, a) : new Array;
};var $ = function (id) {
//获取对象
return document.getElementById(id);
};Object.extend = function (a, b) {
//追加方法
for (var i in b) a[i] = b[i];
return a;
};Object.extend(Object, { addEvent : function (a, b, c, d) {
//添加函数
if (a.attachEvent) a.attachEvent(b[0], c);
else a.addEventListener(b[1] || b[0].replace(/^on/, ""), c, d || false);
return c;
},
delEvent : function (a, b, c, d) {
if (a.detachEvent) a.detachEvent(b[0], c);
else a.removeEventListener(b[1] || b[0].replace(/^on/, ""), c, d || false);
return c;
},
reEvent : function () {
//获取Event
return window.event ? window.event : (function (o) {
do {
o = o.caller;
} while (o && !/^\[object[ A-Za-z]*Event\]$/.test(o.arguments[0]));
return o.arguments[0];
})(this.reEvent);
}
});Function.prototype.bind = function () {
//绑定事件
var wc = this, a = $A(arguments), o = a.shift();
return function () {
wc.apply(o, a.concat($A(arguments)));
};
};
var CDrag = Class.create();CDrag.IE = /MSIE/.test(window.navigator.userAgent);CDrag.Table = Class.create();CDrag.Table.prototype = {
//列的拖拽暂时不考虑 initialize : function () {
//初始化
var wc = this;
wc.cols = new Array; //创建列
},
addCols : function (o) {
//添加列
var wc = this, cols = wc.cols, i = cols.length;
return cols[i] = {
id : i, div : o, rows : new Array, //创建行
addRow : wc.addRow, chRow : wc.chRow, inRow : wc.inRow, delRow : wc.delRow
};
},
addRow : function (o) {
//添加行
var wc = this, rows = wc.rows, i = rows.length;
return rows[i] = {
id : i, div : o, cols : wc
};
},
inRow : function (a, b) {
//插入行
var cols = b.cols, wc = b.cols = this, rows = wc.rows, i;
if (cols == wc && b.id < a) a --;
for (i = a ; i < rows.length ; i ++) rows[i].id ++;
rows.splice(a, 0, b);
b.id = a;
return b;
},
delRow : function (a) {
//删除列
var wc = this, rows = wc.rows, i, r;
if (a >= rows.length) return;
r = rows[a];
rows.splice(a, 1);
for (i = a ; i < rows.length ; i ++) rows[i].id = i;
return r;
}
};CDrag.prototype = {
initialize : function () {
//初始化成员
var wc = this;
wc.table = new CDrag.Table; //建立表格对象
wc.iFunc = wc.eFunc = null;
wc.obj = { on : { a : null, b : "" }, row : null, left : 0, top : 0 };
wc.temp = { row : null, div : document.createElement("div") };
wc.temp.div.setAttribute(CDrag.IE ? "className" : "class", "CDrag_temp_div");
wc.temp.div.innerHTML = " ";
},
reMouse : function (a) {
//获取鼠标位置
var e = Object.reEvent();
return {
x : document.documentElement.scrollLeft + e.clientX,
y : document.documentElement.scrollTop + e.clientY
};
},
rePosition : function (o) {
//获取元素绝对位置
var $x = $y = 0;
do {
$x += o.offsetLeft;
$y += o.offsetTop;
} while ((o = o.offsetParent) && o.tagName != "BODY");
return { x : $x, y : $y };
},
sMove : function (o) {
//当拖动开始时设置参数
var wc = this;
if (wc.iFunc || wc.eFinc) return;
var mouse = wc.reMouse(), obj = wc.obj, temp = wc.temp, div = o.div, position = wc.rePosition(div);
obj.row = o;
obj.on.b = "me";
obj.left = mouse.x - position.x;
obj.top = mouse.y - position.y;
temp.row = document.body.appendChild(div.cloneNode(true)); //复制预拖拽对象
with (temp.row.style) {
//设置复制对象
position = "absolute";
left = mouse.x - obj.left + "px";
top = mouse.y - obj.top + "px";
zIndex = 100;
opacity = "0.3";
filter = "alpha(opacity:30)";
}
with (temp.div.style) {
//设置站位对象
height = div.clientHeight + "px";
width = div.clientWidth + "px";
}
div.parentNode.replaceChild(temp.div, div);
wc.iFunc = Object.addEvent(document, ["onmousemove"], wc.iMove.bind(wc));
wc.eFunc = Object.addEvent(document, ["onmouseup"], wc.eMove.bind(wc));
document.onselectstart = new Function("return false");
},
iMove : function () {
//当鼠标移动时设置参数
var wc = this, cols = wc.table.cols, mouse = wc.reMouse(), obj = wc.obj, temp = wc.temp, row = obj.row, div = temp.row,
t_position, t_cols, t_rows, i, j;
with (div.style) {
left = mouse.x - obj.left + "px";
top = mouse.y - obj.top + "px";
}
for (i = 0 ; i < cols.length ; i ++) {
t_cols = cols[i];
t_position = wc.rePosition(t_cols.div);
if (t_position.x < mouse.x && t_position.x + t_cols.div.offsetWidth > mouse.x) {
if (t_cols.rows.length > 0) { //如果此列行数大于0
if (wc.rePosition(t_cols.rows[0].div).y + 20 > mouse.y) {
//如果鼠标位置大于第一行的位置即是最上。。
//向上
obj.on.a = t_cols.rows[0];
obj.on.b = "up";
obj.on.a.div.parentNode.insertBefore(temp.div, obj.on.a.div);
} else if (t_cols.rows.length > 1 && t_cols.rows[0] == row &&
wc.rePosition(t_cols.rows[1].div).y + 20 > mouse.y) {
//如果第一行是拖拽对象而第鼠标大于第二行位置则,没有动。。
//向上
obj.on.b = "me";
t_cols.rows[1].div.parentNode.insertBefore(temp.div, t_cols.rows[1].div);
} else {
for (j = t_cols.rows.length - 1 ; j > -1 ; j --) {
//重最下行向上查询
t_rows = t_cols.rows[j];
if (t_rows == obj.row) {
if (t_cols.rows.length == 1) {
t_cols.div.appendChild(temp.div);
obj.on.b = "me";
}
continue;
}
if (wc.rePosition(t_rows.div).y < mouse.y) {
//如果鼠标大于这行则在这行下面
if (t_rows.id + 1 < t_cols.rows.length && t_cols.rows[t_rows.id + 1] != obj.row) {
//如果这行有下一行则重这行下一行的上面插入
t_cols.rows[t_rows.id + 1].div.parentNode.
insertBefore(temp.div, t_cols.rows[t_rows.id + 1].div);
obj.on.a = t_rows;
obj.on.b = "down";
} else if (t_rows.id + 2 < t_cols.rows.length) {
//如果这行下一行是拖拽对象则插入到下两行,即拖拽对象返回原位
t_cols.rows[t_rows.id + 2].div.parentNode.
insertBefore(temp.div, t_cols.rows[t_rows.id + 2].div);
obj.on.b = "me";
} else {
//前面都没有满足则放在最低行
t_rows.div.parentNode.appendChild(temp.div);
obj.on.a = t_rows;
obj.on.b = "down";
}
return;
}
}
}
} else {
//此列无内容添加新行
t_cols.div.appendChild(temp.div);
obj.on.a = t_cols;
obj.on.b = "new";
}
}
}
},
eMove : function () {
//当鼠标释放时设置参数
var wc = this, obj = wc.obj, temp = wc.temp, row = obj.row, div = row.div, o_cols, n_cols;
if (obj.on.b == "up") {
//向最上添加
o_cols = obj.row.cols;
n_cols = obj.on.a.cols;
n_cols.inRow(0, o_cols.delRow(obj.row.id));
} else if (obj.on.b == "down") {
//相对向下添加
o_cols = obj.row.cols;
n_cols = obj.on.a.cols;
n_cols.inRow(obj.on.a.id + 1, o_cols.delRow(obj.row.id));
/*for (i = 0 ; i < n_cols.rows.length ; i ++) {
alert(n_cols.rows[i].div.childNodes[0].innerHTML);
}*/
} else if (obj.on.b == "new") {
//向无内容列添加
o_cols = obj.row.cols;
n_cols = obj.on.a;
n_cols.inRow(0, o_cols.delRow(obj.row.id));
}
if (obj.on.b != "me") wc.set_cookie();
temp.div.parentNode.replaceChild(div, temp.div);
temp.row.parentNode.removeChild(temp.row);
delete temp.row;
Object.delEvent(document, ["onmousemove"], wc.iFunc);
Object.delEvent(document, ["onmouseup"], wc.eFunc);
document.onselectstart = wc.iFunc = wc.eFunc = null;
},
add : function (o) {
//添加对象
var wc = this;
Object.addEvent(o.div.childNodes[CDrag.IE ? 0 : 1], ["onmousedown"], wc.sMove.bind(wc, o));
},
set_cookie : function () {
//设置COOKIE
var wc = this, cols = wc.table.cols, date = new Date, a = [], b = [], i, j, r;
date.setDate(date.getDate() + 1);
for (i = 0 ; i < cols.length ; i ++) {
for (r = cols[i].rows, j = 0 ; j < r.length ; j ++) b[b.length] = "'" + r[j].div.id + "'";
a[a.length] = "cols:'" + cols[i].div.id + "',rows:[" + b.splice(0, b.length).join(",") + "]";
}
document.cookie = "CDrag=" + escape("[{" + a.join("},{") + "}]") + ";expires=" + date.toGMTString();
},
del_cookie : function () {
//删除COOKIE
var date;
if (/(CDrag=[^;]+(?:;|$))/.test(document.cookie)) {
date = new Date;
date.setTime(date.getTime() - 1);
document.cookie = "CDrag=" + RegExp.$1 + ";expires=" + date.toGMTString();
}
},
parse : function (o) {
//初始化成员
try {
var wc = this, table = wc.table, cols, c_div, r_div, i, j;
for (i = 0 ; i < o.length ; i ++) {
c_div = $(o[i].cols), cols = table.addCols(c_div);
for (j = 0 ; j < o[i].rows.length ; j ++) {
r_div = $(o[i].rows[j]), wc.add(cols.addRow(r_div));
c_div.appendChild(r_div);
}
}
} catch (exp) {
wc.del_cookie();
}
}
};Object.addEvent(window, ["onload"], function () {
var wc = new CDrag,
json = /CDrag=([^;]+)(?:;|$)/.test(document.cookie) ? eval('(' + unescape(RegExp.$1) + ')') : [
{
cols : "m_1", rows : ["m_1_1", "m_1_2", "m_1_3", "m_1_4"]
},
{
cols : "m_2", rows : ["m_2_1", "m_2_2", "m_2_3", "m_2_4"]
},
{
cols : "m_3", rows : ["m_3_1", "m_3_2", "m_3_3", "m_3_4"]
},
{
cols : "m_4", rows : ["m_4_1", "m_4_2", "m_4_3", "m_4_4"]
}
];
wc.parse(json);
$("DEL_CDrag").onclick = (function (wc) {
return function () {
wc.del_cookie();
};
})(wc);
wc = null;
});</script>
</head>
<body>
<br />
<div><a id="DEL_CDrag" href="javascript:void(0);">[清除记录]</a></div>
<div class="root">
<div class="cell" id="m_1">
<div class="line"> </div>
<div class="move" id="m_1_1">
<div class="title">第一列的第一个的说</div>
<div class="content"></div>
</div>
<div class="move" id="m_1_2">
<div class="title">第一列的第二个的说</div>
<div class="content"></div>
</div>
<div class="move" id="m_1_3">
<div class="title">第一列的第三个的说</div>
<div class="content"></div>
</div>
<div class="move" id="m_1_4">
<div class="title">第一列的第四个的说</div>
<div class="content"></div>
</div>
</div>
<div class="cell r_nbsp"> </div>
<div class="cell" id="m_2">
<div class="line"> </div>
<div class="move" id="m_2_1">
<div class="title">第二列的第一个的说</div>
<div class="content"></div>
</div>
<div class="move" id="m_2_2">
<div class="title">第二列的第二个的说</div>
<div class="content"></div>
</div>
<div class="move" id="m_2_3">
<div class="title">第二列的第三个的说</div>
<div class="content"></div>
</div>
<div class="move" id="m_2_4">
<div class="title">第二列的第四个的说</div>
<div class="content"></div>
</div>
</div>
<div class="cell r_nbsp"> </div>
<div class="cell" id="m_3">
<div class="line"> </div>
<div class="move" id="m_3_1">
<div class="title">第三列的第一个的说</div>
<div class="content"></div>
</div>
<div class="move" id="m_3_2">
<div class="title">第三列的第二个的说</div>
<div class="content"></div>
</div>
<div class="move" id="m_3_3">
<div class="title">第三列的第三个的说</div>
<div class="content"></div>
</div>
<div class="move" id="m_3_4">
<div class="title">第三列的第四个的说</div>
<div class="content"></div>
</div>
</div>
<div class="cell r_nbsp"> </div>
<div class="cell" id="m_4">
<div class="line"> </div>
<div class="move" id="m_4_1">
<div class="title">第四列的第一个的说</div>
<div class="content"></div>
</div>
<div class="move" id="m_4_2">
<div class="title">第四列的第二个的说</div>
<div class="content"></div>
</div>
<div class="move" id="m_4_3">
<div class="title">第四列的第三个的说</div>
<div class="content"></div>
</div>
<div class="move" id="m_4_4">
<div class="title">第四列的第四个的说</div>
<div class="content"></div>
</div>
</div>
</div>
</body>
</html>
然后在页面引用一下<script src="../js/jquery.js" type="text/javascript"></script>
<script src="../js/ui/jquery.ui.all.js" type="text/javascript"></script>
<script src="../js/ui/ui.draggable.js" type="text/javascript"></script>
<script type="text/javascript"> $(document).ready(function() {
$("#a").draggable();
})
</script>
<div id="a">qweqqweq</div>现在你拖拖看,按不紧也不会掉
<div style="border:1px solid #a1b8e2;font-weight:bold;height:28px; background:#a1b8e2">
</div><div style="background:#EBF3FD;border1px solid #a1b8e2;height:470px;">
<div style="border:1px solid #a1b8e2;position:relative;float:left;top:10px;left:8px;height:350px;">
<iframe src="http://www.baidu.com" scrolling="no" frameborder="0" width="680" height="450"></iframe>
</div>
</div>
</div>
冒似用这个当div来实现拖动效果的时候
效果都不是很理想。