js功能类库放送(二) 调整大小库

稍微有点乱,不打算整理了,因为容器位置的问题,本文演示可能有点问题,可以点击 这里下载演示和源码。

轻量级的应用,无须其他任何框架,推荐使用
注意本文代码和源码均为原创,请不要移去版权说明信息

a
slider_thumb.gifslider_thumb.gifslider_thumb.gif
slider_thumb.gif
content body, only here can be drag!!!

dkj
slider_thumb.gif
slider_thumb.gifslider_thumb.gifslider_thumb.gif
slider_thumb.gifslider_thumb.gifslider_thumb.gif
slider_thumb.gifThis table resize itself just using a shadow.slider_thumb.gif
slider_thumb.gifslider_thumb.gifslider_thumb.gif
slider_thumb.gifslider_thumb.gifslider_thumb.gif
slider_thumb.gifThis table resize itself just using a shadow.
dskjflsddddddddddddddddddddddddddddd


slider_thumb.gif
slider_thumb.gifslider_thumb.gifslider_thumb.gif





































完整源码如下:
ExpandedBlockStart.gif ContractedBlock.gif /**/ /*********************************************************
InBlock.gif**
InBlock.gif**  FileName: Resize.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
//  说明1:当对象在拖动时,其resizeObj.ResizeState状态为true,可以根据此属性判断是否处于resize状态
None.gif

None.gif
//  8.18 修改:增加ResizeChanged事件激发,注意在页面上给Resize对象增加onResizeChanged="callback();",
None.gif//
 当size改变时就会自动回调callback函数
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
None.gif
//  激发指定对象的拖动事件,使用空的shadow,速度更快。
None.gif//
 参数分别为: 
None.gif//
 $1: 拖动对象
None.gif//
 $2: event
None.gif//
 $3: Resize方向,一共八个,复合型以s和n开头(e 右 | n 上 | ne 右上 | nw 左上 | s 下 | se 右下 | sw 左下 | w 左)
None.gif//
 $4: 最小宽度, 最小高度
None.gif//
 $5: 最大宽度, 最大高度
None.gif
function  ResizeWithShadow(resizeObj, event, direction, minWidth, minHeight, maxX, maxY)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if(typeof(resizeObj.CanResize) != 'undefined' && !resizeObj.CanResize)
InBlock.gif        
return;
InBlock.gif    
var objShadow;    
InBlock.gif    
var parentOffsetLeft = 0, parentOffsetTop = 0;
InBlock.gif    
var obj = resizeObj;
InBlock.gif    
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
//    // 附加SizeChanged事件,注意全部小写
InBlock.gif//
    if(resizeObj.onsizechanged)
InBlock.gif//
        objShadow.onsizechanged = resizeObj.onsizechanged;
InBlock.gif//
    else if(resizeObj.onSizeChanged)
InBlock.gif//
        objShadow.onsizechanged = resizeObj.onSizeChanged;
InBlock.gif//
        
InBlock.gif//
    if(resizeObj.onResizeCompleted)
InBlock.gif//
        objShadow.onResizeCompleted = resizeObj.onResizeCompleted;
InBlock.gif
    // 将阴影层放置在本层上面以便观察
InBlock.gif
    objShadow.style.zIndex = resizeObj.style.zIndex+1;
InBlock.gif    objShadow.style.left 
= parentOffsetLeft + resizeObj.offsetLeft + 'px';
InBlock.gif    objShadow.style.top 
= parentOffsetTop + resizeObj.offsetTop + 'px';
InBlock.gif    objShadow.style.width 
= resizeObj.offsetWidth + 'px';
InBlock.gif    objShadow.style.height 
= resizeObj.offsetHeight + 'px';
InBlock.gif    
InBlock.gif    objShadow.style.display
='';
InBlock.gif    
InBlock.gif    Resize(objShadow, event, direction, minWidth, minHeight, maxX, maxY)
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        
var chgX = 1;
InBlock.gif        
var chgY = 1;
InBlock.gif        
switch(direction)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//(e 右 | n 上 | ne 右上 | nw 左上 | s 下 | se 右下 | sw 左下 | w 左)
InBlock.gif
            case 'e':   // 右
InBlock.gif
                chgX = 1;
InBlock.gif                chgY 
= 0;
InBlock.gif                
break;
InBlock.gif            
case 'n':   // 上
InBlock.gif
                chgX = 0;
InBlock.gif                chgY 
= -1;
InBlock.gif                
break;
InBlock.gif            
case 'ne':   // 右上
InBlock.gif
                chgX = 1;
InBlock.gif                chgY 
= -1;
InBlock.gif                
break;
InBlock.gif            
case 'nw':   // 左上
InBlock.gif
                chgX = -1;
InBlock.gif                chgY 
= -1;
InBlock.gif                
break;
InBlock.gif            
case 's':   // 下
InBlock.gif
                chgX = 0;
InBlock.gif                chgY 
= 1;
InBlock.gif                
break;
InBlock.gif            
case 'se':   // 右下
InBlock.gif
                chgX = 1;
InBlock.gif                chgY 
= 1;
InBlock.gif                
break;
InBlock.gif            
case 'sw':   // 左下
InBlock.gif
                chgX = -1;
InBlock.gif                chgY 
= 1;
InBlock.gif                
break;
InBlock.gif            
case 'w':   // 左
InBlock.gif
                chgX = -1;
InBlock.gif                chgY 
= 0;
InBlock.gif                
break;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
if(chgX < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            resizeObj.style.left 
= objShadow.offsetLeft-parentOffsetLeft + 'px';
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if(chgY < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            resizeObj.style.top 
= objShadow.offsetTop-parentOffsetTop + 'px';
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        resizeObj.style.width 
= objShadow.style.width;
InBlock.gif        resizeObj.style.height 
= objShadow.style.height;
InBlock.gif                            
InBlock.gif        
if(resizeObj.onsizechanged)
InBlock.gif            eval(resizeObj.onsizechanged);
InBlock.gif        
else if(resizeObj.onSizeChanged)
InBlock.gif            eval(resizeObj.onSizeChanged);
InBlock.gif        
InBlock.gif        
if(resizeObj.onResizeCompleted)
InBlock.gif            eval(resizeObj.onResizeCompleted);
InBlock.gif        
InBlock.gif        
// 更新对象的状态
InBlock.gif
        resizeObj.ResizeState = false;
InBlock.gif        
// 注销事件
InBlock.gif
        _detachEvent(document, 'mouseup', dragEnd);
InBlock.gif        
InBlock.gif        
// 删除阴影层,防止缓存
InBlock.gif
        document.body.removeChild(objShadow);
InBlock.gif        
InBlock.gif        
//不要再让事件进一步传播.
InBlock.gif
        _cancelBubble(event);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
//  $1: 拖动对象
None.gif//
 $2: event
None.gif//
 $3: Resize方向,一共八个,复合型以s和n开头(e 右 | n 上 | ne 右上 | nw 左上 | s 下 | se 右下 | sw 左下 | w 左)
None.gif//
 $4: 最大X坐标, 最大Y坐标
None.gif//
 $5: 最小
None.gif

None.gif
//  激发指定对象的拖动事件, 参数分别为 拖动对象, event, 方向(1水平,2垂直,null任意), 最大X坐标, 最大Y坐标
None.gif
function  Resize(resizeObj, event, direction, minWidth, minHeight, maxX, maxY)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if(typeof(resizeObj.CanResize) != 'undefined' && !resizeObj.CanResize)
InBlock.gif        
return;
InBlock.gif        resizeObj.style.width 
= resizeObj.offsetWidth;
InBlock.gif        resizeObj.style.height 
= resizeObj.offsetHeight;
InBlock.gif    
// 将对象的拖动状态设置为true
InBlock.gif
    resizeObj.ResizeState = true;
InBlock.gif    
// 取得对应坐标位置
InBlock.gif
    var oldX = event.clientX;
InBlock.gif    
var oldY = event.clientY;
InBlock.gif    
var oldWidth = resizeObj.offsetWidth;
InBlock.gif    
var oldHeight= resizeObj.offsetHeight;
InBlock.gif    oldOffsetRight 
= screen.width -resizeObj.offsetLeft - resizeObj.offsetWidth;
InBlock.gif    oldOffsetBottom 
= screen.height -resizeObj.offsetTop - resizeObj.offsetHeight;
InBlock.gif    
InBlock.gif    
var isChanged = false;  // 对象的Size是否发生改变
InBlock.gif
    var newHeight = 0, newWidth=0;
InBlock.gif    
InBlock.gif    
//document.getElementById('dbg').innerHTML = oldX;
InBlock.gif
    _attachEvent(document, "mousemove", moveHandler);
InBlock.gif    _attachEvent(document, 'mouseup', resizeEnd);
InBlock.gif
InBlock.gif    
//我们处理了该事件,不要再让其他元素看见.
InBlock.gif
    _cancelBubble(event);
InBlock.gif
InBlock.gif    
//下面禁止执行默认动作
InBlock.gif
    _cancelDefault(event);
InBlock.gif    
InBlock.gif    
//把元素移动到鼠标当前的位置,根据初始鼠标点击的偏移量进行调整
ExpandedSubBlockStart.gifContractedSubBlock.gif
    function moveHandler(e)  dot.gif{
InBlock.gif        
var chgX = 1;
InBlock.gif        
var chgY = 1;
InBlock.gif        
switch(direction)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//(e 右 | n 上 | ne 右上 | nw 左上 | s 下 | se 右下 | sw 左下 | w 左)
InBlock.gif
            case 'e':   // 右
InBlock.gif
                chgX = 1;
InBlock.gif                chgY 
= 0;
InBlock.gif                
break;
InBlock.gif            
case 'n':   // 上
InBlock.gif
                chgX = 0;
InBlock.gif                chgY 
= -1;
InBlock.gif                
break;
InBlock.gif            
case 'ne':   // 右上
InBlock.gif
                chgX = 1;
InBlock.gif                chgY 
= -1;
InBlock.gif                
break;
InBlock.gif            
case 'nw':   // 左上
InBlock.gif
                chgX = -1;
InBlock.gif                chgY 
= -1;
InBlock.gif                
break;
InBlock.gif            
case 's':   // 下
InBlock.gif
                chgX = 0;
InBlock.gif                chgY 
= 1;
InBlock.gif                
break;
InBlock.gif            
case 'se':   // 右下
InBlock.gif
                chgX = 1;
InBlock.gif                chgY 
= 1;
InBlock.gif                
break;
InBlock.gif            
case 'sw':   // 左下
InBlock.gif
                chgX = -1;
InBlock.gif                chgY 
= 1;
InBlock.gif                
break;
InBlock.gif            
case 'w':   // 左
InBlock.gif
                chgX = -1;
InBlock.gif                chgY 
= 0;
InBlock.gif                
break;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// 根据所在位置进行向左或向右拖动大小
InBlock.gif
        if(chgX < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            newWidth 
= (oldWidth + oldX - e.clientX );
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else if(chgX > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            newWidth 
= (oldWidth + e.clientX - oldX);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if(chgY < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            newHeight 
= (oldHeight + oldY - e.clientY );
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else if(chgY > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            newHeight 
= (oldHeight + e.clientY - oldY);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
// 最小值判断
InBlock.gif
        if(chgX != 0 && minWidth && newWidth < minWidth)
InBlock.gif            newWidth 
= minWidth;
InBlock.gif
InBlock.gif        
if(chgY != 0 && minHeight && newHeight < minHeight)
InBlock.gif            newHeight 
= minHeight;
InBlock.gif        
InBlock.gif        
// 反相移动时以右上角为原始点
InBlock.gif
        if(chgX < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            resizeObj.style.left 
= screen.width - oldOffsetRight - newWidth +'px';
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
if(chgY < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            resizeObj.style.top 
= screen.height - oldOffsetBottom - newHeight +'px';
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
if(newWidth > 0)
InBlock.gif            resizeObj.style.width
= newWidth + "px";
InBlock.gif        
if(newHeight > 0)
InBlock.gif            resizeObj.style.height
= newHeight + "px";
InBlock.gif            
InBlock.gif                
InBlock.gif        _cancelBubble(event);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
function resizeEnd()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{        
InBlock.gif        
// 大小发生改变
InBlock.gif
        if(resizeObj.offsetWidth != oldWidth || resizeObj.offsetHeight != oldHeight)
InBlock.gif            isChanged 
= true;
InBlock.gif                    
InBlock.gif        
if(isChanged && resizeObj.onsizechanged)
InBlock.gif            eval(resizeObj.onsizechanged);
InBlock.gif        
else if(isChanged && resizeObj.onSizeChanged)
InBlock.gif            eval(resizeObj.onSizeChanged);
InBlock.gif        
InBlock.gif        
if(resizeObj.onResizeCompleted)
InBlock.gif            eval(resizeObj.onResizeCompleted);
InBlock.gif        
InBlock.gif        
// 更新对象的状态
InBlock.gif
        resizeObj.ResizeState = false;
InBlock.gif        
// 注销事件
InBlock.gif
        _detachEvent(document, 'mousemove', moveHandler);
InBlock.gif        _detachEvent(document, 'mouseup', resizeEnd);
InBlock.gif        
InBlock.gif        
//不要再让事件进一步传播.
InBlock.gif
        _cancelBubble(event);
InBlock.gif        
if(resizeObj.layerid)  //有蒙层的模块要改变蒙层的高度、宽度等。
InBlock.gif
            ChangeLayer(resizeObj);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/Truly/archive/2007/04/27/729669.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值