js功能类库放送(一) 拖动功能库

好久没做正经事情了,今天开始,整理一下以前写的代码。


说明懒得写了,js代码里有很详细的注释,代码很少很精简,喜欢的朋友可以看一下。


唉,外面的代码太乱了,不调整了。轻量级的应用,无须其他任何框架,推荐使用,本人原创,不要移除说明信息。













































div demo,随意拖动
14.jpg随意拖动
2.jpg水平移动
0.jpg垂直移动
完整源码如下:
ExpandedBlockStart.gif ContractedBlock.gif /**/ /*********************************************************
InBlock.gif**
InBlock.gif**  FileName: Drag.js
InBlock.gif**
InBlock.gif**  Author: Truly
InBlock.gif**  Date: 2006.8.15
InBlock.gif**  Function: 实现指定对象的拖动等功能
InBlock.gif**  Compatibility: IE4, IE5+, FireFox, etc.
InBlock.gif**  Description: Perfect!!! powered by truly. 
InBlock.gif**  Web: http://Truly.cnblogs.com
InBlock.gif**
ExpandedBlockEnd.gif*********************************************************
*/

None.gif
None.gif
None.gif
//  事件注册,调用示例:_attachEvent(document, "mousemove", moveHandler);
ExpandedBlockStart.gifContractedBlock.gif
function  _attachEvent(obj, evt, func)  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if(obj.addEventListener) dot.gif{
InBlock.gif        obj.addEventListener(evt,func,
true); 
ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 else if(obj.attachEvent) dot.gif{
InBlock.gif        obj.attachEvent(
"on"+evt,func);
ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 else dot.gif
InBlock.gif        eval(
"var old"+func+"="+obj+".on"+evt+";");
InBlock.gif        eval(obj
+".on"+evt+"="+func+";");
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
//  事件注销,调用示例:_detachEvent(document, "mousemove", moveHandler);
ExpandedBlockStart.gifContractedBlock.gif
function  _detachEvent(obj, evt, func)  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if(obj.removeEventListener) dot.gif//DOM事件模型
InBlock.gif
        obj.removeEventListener(evt,func,true);
ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 else if(obj.detachEvent) dot.gif//IE5+ 事件模型
InBlock.gif
        obj.detachEvent("on"+evt,func);
ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 else  dot.gif//IE事件模型
InBlock.gif
        eval(obj+".on"+evt+"=old"+func+";");
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
//  禁止默认行为,调用示例:_cancelDefault(event);
None.gif
function  _cancelDefault(e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if(e.preventDefault) e.preventDefault(); //2级DOM
InBlock.gif
    else e.returnValue=false//IE
ExpandedBlockEnd.gif
}

None.gif
None.gif
//  阻止事件冒泡, 调用示例:_cancelBubble(event);
None.gif
function  _cancelBubble(e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if(e.stopPropagation) e.stopPropagation(); //2 级DOM
InBlock.gif
    else e.cancelBubble=true//IE
ExpandedBlockEnd.gif
}

None.gif
function  gid(id)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
return document.getElementById(id);
ExpandedBlockEnd.gif}

None.gif
//  激发指定对象的拖动事件,使用空的shadow,速度更快。§ 参数分别为 拖动对象, event, 方向(1水平,2垂直,null任意), 最大X坐标, 最大Y坐标
None.gif
function  DragByShadow(dragObj,event, direction, maxX, maxY)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif        
InBlock.gif    
var objShadow;
InBlock.gif    
var parentOffsetLeft = 0, parentOffsetTop = 0;
InBlock.gif    
var obj = dragObj;
InBlock.gif    
InBlock.gif    
// 先保存原来的尺寸,防止变形
InBlock.gif
    dragObj.style.width = dragObj.offsetWidth;
InBlock.gif    dragObj.style.height 
= dragObj.offsetHeight;
InBlock.gif
//    gid('dbg').innerHTML = dragObj.offsetTop;
InBlock.gif
    // 处理父节点偏移量
InBlock.gif
    while(obj.parentNode && obj.parentNode.tagName != "BODY")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        parentOffsetLeft 
+= obj.parentNode.offsetLeft;
InBlock.gif        parentOffsetTop 
+= obj.parentNode.offsetTop;        
InBlock.gif        obj 
= obj.parentNode;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
if(!document.getElementById('shadow'))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        objShadow
=document.createElement("DIV");
InBlock.gif        objShadow.id 
= 'shadow';
InBlock.gif        objShadow.style.position
='absolute';
InBlock.gif        objShadow.style.filter 
= 'alpha(opacity=30,style=0)';
InBlock.gif        objShadow.style.backgroundColor 
= '#ccc';        
InBlock.gif        document.body.appendChild(objShadow);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
InBlock.gif        objShadow
= document.getElementById('shadow');
InBlock.gif        
InBlock.gif            
InBlock.gif    
// 将阴影层放置在本层下面以防止遮住按钮等东西
InBlock.gif
    objShadow.style.zIndex = dragObj.style.zIndex+1;
InBlock.gif    objShadow.style.left 
= parentOffsetLeft + dragObj.offsetLeft + 'px';
InBlock.gif    objShadow.style.top 
= parentOffsetTop + dragObj.offsetTop + 'px';
InBlock.gif    
InBlock.gif    objShadow.style.width 
= dragObj.offsetWidth + 'px';
InBlock.gif    objShadow.style.height 
= dragObj.offsetHeight + 'px';
InBlock.gif    
InBlock.gif    
InBlock.gif    dragStart(objShadow,event, direction, maxX, maxY);
InBlock.gif    objShadow.style.display
='none';
InBlock.gif    _attachEvent(document, 'mouseup', dragEnd);
InBlock.gif
InBlock.gif    
InBlock.gif    _cancelBubble(event);
InBlock.gif
InBlock.gif    
//下面禁止执行默认动作
InBlock.gif
    _cancelDefault(event);
InBlock.gif    
function dragEnd()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(!direction)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 未指定方向时,随意拖动
InBlock.gif
            dragObj.style.left = objShadow.offsetLeft-parentOffsetLeft + 'px';
InBlock.gif            dragObj.style.top 
= objShadow.offsetTop-parentOffsetTop + 'px';
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 else if( direction == 1dot.gif{
InBlock.gif            
// 水平移动
InBlock.gif
            dragObj.style.left = objShadow.style.left;
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 else if( direction == 2dot.gif{
InBlock.gif            
// 垂直移动
InBlock.gif
            dragObj.style.top = objShadow.style.top;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif
InBlock.gif        
// 删除阴影层,防止缓存
InBlock.gif
        document.body.removeChild(objShadow);
InBlock.gif        
InBlock.gif        
// 注销事件
InBlock.gif
        _detachEvent(document, 'mouseup', dragEnd);
InBlock.gif        
InBlock.gif        
//不要再让事件进一步传播.
InBlock.gif
        _cancelBubble(event);
InBlock.gif        
if(dragObj.layerid)  //有蒙层的模块要改变蒙层的高度、宽度等。
InBlock.gif
            ChangeLayer(dragObj);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
//  激发指定对象的拖动事件,实时拖动。§ 参数分别为 拖动对象, event, 方向(1水平,2垂直,null任意), 最大X坐标, 最大Y坐标
None.gif
function  dragStart(dragObj,event, direction, maxX, maxY)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
// 先保存原来的尺寸,防止变形
InBlock.gif
    dragObj.style.width = dragObj.offsetWidth;
InBlock.gif    dragObj.style.height 
= dragObj.offsetHeight;
InBlock.gif    
InBlock.gif    pointX
=event.clientX-parseInt(dragObj.offsetLeft);
InBlock.gif    pointY
=event.clientY-parseInt(dragObj.offsetTop);
InBlock.gif    
InBlock.gif    _attachEvent(document, 
"mousemove", moveHandler);
InBlock.gif    _attachEvent(document, 'mouseup', dragEnd);
InBlock.gif
InBlock.gif    _cancelBubble(event);
InBlock.gif
InBlock.gif    
//下面禁止执行默认动作
InBlock.gif
    _cancelDefault(event);
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
function moveHandler(e)  dot.gif{
InBlock.gif        dragObj.style.display 
= '';
InBlock.gif        
//把元素移动到鼠标当前的位置,根据初始鼠标点击的偏移量进行调整
InBlock.gif
        if(!direction)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 未指定方向时,随意拖动
InBlock.gif
            dragObj.style.left=(e.clientX-pointX)+"px";
InBlock.gif            dragObj.style.top
=(e.clientY-pointY)+"px";
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 else if( direction == 1dot.gif{
InBlock.gif            
// 水平移动
InBlock.gif
            dragObj.style.left=(e.clientX-pointX)+"px";
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 else if( direction == 2dot.gif{
InBlock.gif            
// 垂直移动
InBlock.gif
            dragObj.style.top=(e.clientY-pointY)+"px";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//不要再让其他元素看到该事件.
InBlock.gif
        _cancelBubble(event);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
function dragEnd()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// 注销事件
InBlock.gif
        _detachEvent(document, 'mousemove', moveHandler);
InBlock.gif        _detachEvent(document, 'mouseup', dragEnd);
InBlock.gif        
InBlock.gif        
//不要再让事件进一步传播.
InBlock.gif
        _cancelBubble(event);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/Truly/archive/2007/04/06/702752.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值