Util.re_calcOff( this );
// 记录原先的邻居节点,用来对比是否被移动到新的位置
this .origNextSibling = this .elm.nextSibling;
// 获取移动的时候那个灰色的虚线框
var _ghostElement = getGhostElement();
// 获取正在移动的这个对象的高度
var offH = this .elm.offsetHeight;
if (Util.isGecko) {
// 修正gecko引擎的怪癖吧
offH -= parseInt(_ghostElement.style.borderTopWidth) * 2 ;
}
// 获取正在移动的这个对象的宽度
var offW = this .elm.offsetWidth;
// 获取left和top的坐标
var offLeft = Util.getOffset( this .elm, true );
var offTop = Util.getOffset( this .elm, false );
// 防止闪烁,现隐藏
Util.hide();
// 将自己的宽度记录在style属性里面
this .elm.style.width = offW " px " ;
// 将那个灰框设定得与正在拖动的对象一样高,比较形象
_ghostElement.style.height = offH " px " ;
// 把灰框放到这个对象原先的位置上
字串1
this .elm.parentNode.insertBefore(_ghostElement, this .elm.nextSibling);
// 由于要拖动必须将被拖动的对象从原先的盒子模型里面抽出来,所以设定position为absolute,这个可以参考一下css布局方面的知识
this .elm.style.position = " absolute " ;
// 设置zIndex,让它处在最前面一层,当然其实zIndex=100是让它很靠前,如果页面里有zIndex>100的,那……
this .elm.style.zIndex = 100 ;
// 由于position=absolute了,所以left和top实现绝对坐标定位,这就是先前计算坐标的作用,不让这个模型乱跑,要从开始拖动的地方开始移动
this .elm.style.left = offLeft " px " ;
this .elm.style.top = offTop " px " ;
// 坐标设定完毕,可以显示了,这样就不会闪烁了
Util.show();
// 这里本来有个ig_d.G,没搞明白干什么用的,不过没有也可以用,谁知道麻烦告诉我一声,不好意思
// 还没有开始拖拽,这里做个记号
this .isDragging = false ;
return false ;
};
// 在拖拽时的相应函数,由于绑定到鼠标的move这个event上,所以会传入鼠标的坐标clientX, clientY
字串1
function when_Drag(clientX, clientY) {
// 刚开始拖拽的时候将图层变透明,并标记为正在被拖拽
if ( ! this .isDragging) {
this .elm.style.filter = " alpha(opacity=70) " ;
this .elm.style.opacity = 0.7 ;
this .isDragging = true ;
}
// 被拖拽到的新的column(当然也可以是原来那个)
var found = null ;
// 最大的距离,可能是防止溢出或者什么bug
var max_distance = 100000000 ;
// 遍历所有的可拖拽的element,寻找离当前鼠标坐标最近的那个可拖拽元素,以便后面插入
for ( var i = 0 ; i < Util.dragArray.length; i ) {
var ele = Util.dragArray[i];
// 利用勾股定理计算鼠标到遍历到的这个元素的距离
var distance = Math.sqrt(Math.pow(clientX - ele.elm.pagePosLeft, 2 ) Math.pow(clientY - ele.elm.pagePosTop, 2 ));
// 自己已经浮动了,所以不计算自己的
if (ele == this ) {
continue ;
}
// 如果计算失败继续循环 字串5
if (isNaN(distance)) {
continue ;
}
// 如果更小,记录下这个距离,并将它作为found
if (distance < max_distance) {
max_distance = distance;
found = ele;
}
}
// 准备让灰框落脚
var _ghostElement = getGhostElement();
// 如果找到了另外的落脚点
if (found != null && _ghostElement.nextSibling != found.elm) {
// 找到落脚点就先把灰框插进去,这就是我们看到的那个灰框停靠的特效,有点像吸附的感觉,哈哈
found.elm.parentNode.insertBefore(_ghostElement, found.elm);
if (Util.isOpera) {
// Opera的现实问题,要隐藏/显示后才能刷新出变化
document.body.style.display = " none " ;
document.body.style.display = "" ;
}
}
};
// 拖拽完毕
function end_Drag() {
// 拖拽完毕后执行后面的钩子,执行after_Drag(),如果布局发生了变动了就记录到远程服务器,保存你拖拽后新的布局顺序
字串5
if ( this ._afterDrag()) {
// remote call to save the change
}
return true ;
};
// 拖拽后的执行钩子
function after_Drag() {
var returnValue = false ;
// 防止闪烁
Util.hide();
// 把拖拽时的position=absolute和相关的那些style都消除
this .elm.style.position = "" ;
this .elm.style.width = "" ;
this .elm.style.zIndex = "" ;
this .elm.style.filter = "" ;
this .elm.style.opacity = "" ;
// 获取灰框
var ele = getGhostElement();
// 如果现在的邻居不是原来的邻居了
if (ele.nextSibling != this .origNextSibling) {
// 把被拖拽的这个节点插到灰框的前面
ele.parentNode.insertBefore( this .elm, ele.nextSibling);
// 标明被拖拽了新的地方
returnValue = true ;
}
// 移除灰框,这是这个灰框的生命周期应该就结束了

