您的位置:首页 > 其它

八点改变div大小

2010-02-27 18:06 246 查看
其实还是比较简单的..............
<!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=utf-8" />
<title>Resize</title>
<style type="text/css">
#rRightDown,#rLeftDown,#rLeftUp,#rRightUp,#rRight,#rLeft,#rUp,#rDown{
position:absolute;background:#C00;width:6px;height:6px;z-index:5;font-size:0;}
#rLeftDown,#rRightUp{cursor:ne-resize;}
#rRightDown,#rLeftUp{cursor:nw-resize;}
#rRight,#rLeft{cursor:e-resize;}
#rUp,#rDown{cursor:n-resize;}
#rRightDown{ bottom:-3px; right:-3px;}
#rLeftDown{ bottom:-3px; left:-3px;}
#rRightUp{ top:-3px; right:-3px;}
#rLeftUp{ top:-3px; left:-3px;}
#rRight{ right:-3px; top:50%}
#rLeft{ left:-3px; top:50%}
#rUp{ top:-3px; left:50%}
#rDown{ bottom:-3px; left:50%}
</style>
</head>
<body>
<div id='ss' style="height:100px; width:100px; border:1px solid #000000; position:absolute; left:200px; top:200px;" >
<div id="rRightDown"> </div>
<div id="rLeftDown"> </div>
<div id="rRightUp"> </div>
<div id="rLeftUp"> </div>
<div id="rRight"> </div>
<div id="rLeft"> </div>
<div id="rUp"> </div>
<div id="rDown"></div>
</div>
<script>
var Sys = (function(ua){
var s = {};
s.IE = ua.match(/msie ([\d.]+)/)?true:false;
s.Firefox = ua.match(/firefox\/([\d.]+)/)?true:false;
s.Chrome = ua.match(/chrome\/([\d.]+)/)?true:false;
s.IE6 = (s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6))?true:false;
s.IE7 = (s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 7))?true:false;
s.IE8 = (s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 8))?true:false;
return s;
})(navigator.userAgent.toLowerCase());
var $ = function (id) {
return document.getElementById(id);
};
var Css = function(e,o){
for(var i in o)
e.style[i] = o[i];
};
var Extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
};
var Bind = function(object, fun) {
var args = Array.prototype.slice.call(arguments).slice(2);
return function() {
return fun.apply(object, args);
}
};
var BindAsEventListener = function(object, fun) {
var args = Array.prototype.slice.call(arguments).slice(2);
return function(event) {
return fun.apply(object, [event || window.event].concat(args));
}
};
var CurrentStyle = function(element){
return element.currentStyle || document.defaultView.getComputedStyle(element, null);
};
function addListener(element,e,fn){
element.addEventListener?element.addEventListener(e,fn,false):element.attachEvent("on" + e,fn);
};
function removeListener(element,e,fn){
element.removeEventListener?element.removeEventListener(e,fn,false):element.detachEvent("on" + e,fn)
};
var Class = function(properties){
var _class = function(){return (arguments[0] !== null && this.initialize && typeof(this.initialize) == 'function') ? this.initialize.apply(this, arguments) : this;};
_class.prototype = properties;
return _class;
};
var Resize =new Class({
initialize : function(obj){
this.obj = obj;
this.resizeelm = null;
this.fun = null; //记录触发什么事件的索引
this.original = []; //记录开始状态的数组
this.width = null;
this.height = null;
this.fR = BindAsEventListener(this,this.resize);
this.fS = Bind(this,this.stop);
},
set : function(elm,direction){
if(!elm)return;
this.resizeelm = elm;
addListener(this.resizeelm,'mousedown',BindAsEventListener(this, this.start, this[direction]));
return this;
},
start : function(e,fun){
this.fun = fun;
this.original = [parseInt(CurrentStyle(this.obj).width),parseInt(CurrentStyle(this.obj).height),parseInt(CurrentStyle(this.obj).left),parseInt(CurrentStyle(this.obj).top)];
this.width = (this.original[2]||0) + this.original[0];
this.height = (this.original[3]||0) + this.original[1];
addListener(document,"mousemove",this.fR);
addListener(document,'mouseup',this.fS);
},
resize : function(e){
this.fun(e);
Sys.IE?(this.resizeelm.onlosecapture=function(){this.fS()}):(this.resizeelm.onblur=function(){this.fS()})
},
stop : function(){
removeListener(document, "mousemove", this.fR);
removeListener(document, "mousemove", this.fS);
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
},
up : function(e){
this.height>e.clientY?Css(this.obj,{top:e.clientY + "px",height:this.height-e.clientY + "px"}):this.turnDown(e);
},
down : function(e){
e.clientY>this.original[3]?Css(this.obj,{top:this.original[3]+'px',height:e.clientY-this.original[3]+'px'}):this.turnUp(e);
},
left : function(e){
e.clientX<this.width?Css(this.obj,{left:e.clientX +'px',width:this.width-e.clientX + "px"}):this.turnRight(e);
},
right : function(e){
e.clientX>this.original[2]?Css(this.obj,{left:this.original[2]+'px',width:e.clientX-this.original[2]+"px"}):this.turnLeft(e) ;
},
leftUp:function(e){
this.up(e);this.left(e);
},
leftDown:function(e){
this.left(e);this.down(e);
},
rightUp:function(e){
this.up(e);this.right(e);
},
rightDown:function(e){
this.right(e);this.down(e);
},
turnDown : function(e){
Css(this.obj,{top:this.height+'px',height:e.clientY - this.height + 'px'});
},
turnUp : function(e){
Css(this.obj,{top : e.clientY +'px',height : this.original[3] - e.clientY +'px'});
},
turnRight : function(e){
Css(this.obj,{left:this.width+'px',width:e.clientX- this.width +'px'});
},
turnLeft : function(e){
Css(this.obj,{left:e.clientX +'px',width:this.original[2]-e.clientX+'px'});
}
});
window.onload = function(){
new Resize($('ss')).set($('rUp'),'up').set($('rDown'),'down').set($('rLeft'),'left').set($('rRight'),'right').set($('rLeftUp'),'leftUp').set($('rLeftDown'),'leftDown').set($('rRightDown'),'rightDown').set($('rRightUp'),'rightUp');
}
</script>
</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=utf-8" />
<title>Resize</title>
<style type="text/css">
#rRightDown,#rLeftDown,#rLeftUp,#rRightUp,#rRight,#rLeft,#rUp,#rDown{
position:absolute;background:#C00;width:6px;height:6px;z-index:5;font-size:0;}
#rLeftDown,#rRightUp{cursor:ne-resize;}
#rRightDown,#rLeftUp{cursor:nw-resize;}
#rRight,#rLeft{cursor:e-resize;}
#rUp,#rDown{cursor:n-resize;}
#rRightDown{ bottom:-3px; right:-3px;}
#rLeftDown{ bottom:-3px; left:-3px;}
#rRightUp{ top:-3px; right:-3px;}
#rLeftUp{ top:-3px; left:-3px;}
#rRight{ right:-3px; top:50%}
#rLeft{ left:-3px; top:50%}
#rUp{ top:-3px; left:50%}
#rDown{ bottom:-3px; left:50%}
#container{
height:400px; width:400px; border:1px solid #0000FF; position:absolute; left:80px; top:80px;
}
</style>
</head>
<body>
<div id="container"></div>
<div id='ss' style="height:100px; width:100px; border:1px solid #000000; position:absolute; left:200px; top:200px;" >
<div id="rRightDown"> </div>
<div id="rLeftDown"> </div>
<div id="rRightUp"> </div>
<div id="rLeftUp"> </div>
<div id="rRight"> </div>
<div id="rLeft"> </div>
<div id="rUp"> </div>
<div id="rDown"></div>
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<input value="设置外围容器范围" type="button" onclick="vvresize('a')" />
<input value="取消外围容器范围" type="button" onclick="vvresize('b')" />
<input value="设置最小范围" type="button" onclick="vvresize('c')" />
<input value="取消最小范围" type="button" onclick="vvresize('d')" />
<script>
var Sys = (function(ua){
var s = {};
s.IE = ua.match(/msie ([\d.]+)/)?true:false;
s.Firefox = ua.match(/firefox\/([\d.]+)/)?true:false;
s.Chrome = ua.match(/chrome\/([\d.]+)/)?true:false;
s.IE6 = (s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6))?true:false;
s.IE7 = (s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 7))?true:false;
s.IE8 = (s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 8))?true:false;
return s;
})(navigator.userAgent.toLowerCase());

var $ = function (id) {
return document.getElementById(id);
};

var Css = function(e,o){
for(var i in o)
e.style[i] = o[i];
};

var Extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
};

var Getpos = function(o){
var x = 0, y = 0;
do{x += o.offsetLeft;y += o.offsetTop;}while((o=o.offsetParent));
return {'x':x,'y':y};
}

var Bind = function(object, fun) {
var args = Array.prototype.slice.call(arguments).slice(2);
return function() {
return fun.apply(object, args);
}
};

var BindAsEventListener = function(object, fun) {
var args = Array.prototype.slice.call(arguments).slice(2);
return function(event) {
return fun.apply(object, [event || window.event].concat(args));
}
};

var CurrentStyle = function(element){
return element.currentStyle || document.defaultView.getComputedStyle(element, null);
};

function addListener(element,e,fn){
element.addEventListener?element.addEventListener(e,fn,false):element.attachEvent("on" + e,fn);
};
function removeListener(element,e,fn){
element.removeEventListener?element.removeEventListener(e,fn,false):element.detachEvent("on" + e,fn)
};

var Class = function(properties){
var _class = function(){return (arguments[0] !== null && this.initialize && typeof(this.initialize) == 'function') ? this.initialize.apply(this, arguments) : this;};
_class.prototype = properties;
return _class;
};

var Resize =new Class({
initialize : function(obj,container,max,min){
this.obj = obj;
this.container = container;
var config = Getpos(container);
this.containerconfig ={l:config.x,t:config.y,w:container.offsetWidth,h:container.offsetHeight};
this.max = max==undefined?true:max; //是否限制最大化 默认限制
this.min = min==undefined?false:min; //是否限制最小化 默认不限制
this.resizeelm = null;
this.fun = null; //记录触发什么事件的索引
this.original = []; //记录开始状态的数组
this.width = null;
this.height = null;
this.fR = BindAsEventListener(this,this.resize);
this.fS = Bind(this,this.stop);
},
set : function(elm,direction){
if(!elm)return;
this.resizeelm = elm;
addListener(this.resizeelm,'mousedown',BindAsEventListener(this, this.start, this[direction]));
return this;
},
start : function(e,fun){
this.fun = fun;
this.original = [parseInt(CurrentStyle(this.obj).width),parseInt(CurrentStyle(this.obj).height),parseInt(CurrentStyle(this.obj).left),parseInt(CurrentStyle(this.obj).top)];
this.width = (this.original[2]||0) + this.original[0];
this.height = (this.original[3]||0) + this.original[1];
addListener(document,"mousemove",this.fR);
addListener(document,'mouseup',this.fS);
},
resize : function(e){
this.fun(e);
Sys.IE?(this.resizeelm.onlosecapture=function(){this.fS()}):(this.resizeelm.onblur=function(){this.fS()})
},
stop : function(){
removeListener(document, "mousemove", this.fR);
removeListener(document, "mousemove", this.fS);
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
},
up : function(e){
if(this.height>e.clientY)
{
var top = this.max?Math.max(this.containerconfig.t,e.clientY):e.clientY;
var height = this.max?(this.height-top):this.height-e.clientY;
Css(this.obj,{top:top + "px",height: Math.max(height,0) + "px"});
return;
}
!this.min&&this.turnDown(e);
},
down : function(e){
if(e.clientY>this.original[3])
{
var top = this.original[3];
var height = this.max?Math.min(e.clientY,this.containerconfig.t+this.containerconfig.h)-this.original[3]:e.clientY-this.original[3];
Css(this.obj,{top:this.original[3]+'px',height:Math.max(height-2,0)+'px'});
return;
}
!this.min&&this.turnUp(e);
},
left : function(e){
if(e.clientX<this.width)
{
var left = this.max?Math.max(e.clientX,this.containerconfig.l):e.clientX;
var width = this.max?this.width-left:this.width-e.clientX;
Css(this.obj,{left:left +'px',width:Math.max(width,0)+ "px"});
return
}
!this.min&&this.turnRight(e);
},
right : function(e){
if(e.clientX>this.original[2])
{
var width = this.max?Math.min(e.clientX,this.containerconfig.l+this.containerconfig.w)-this.original[2]:e.clientX-this.original[2];
Css(this.obj,{left:this.original[2]+'px',width:Math.max(width-2,0)+"px"});
return;
}
!this.min&&this.turnLeft(e);
},
leftUp:function(e){
this.up(e);this.left(e);
},
leftDown:function(e){
this.left(e);this.down(e);
},
rightUp:function(e){
this.up(e);this.right(e);
},
rightDown:function(e){
this.right(e);this.down(e);
},
turnDown : function(e){
var top = this.max?Math.min(this.height,this.containerconfig.t+this.containerconfig.h):this.height;
var height = this.max?Math.min(e.clientY,this.containerconfig.t+this.containerconfig.h)-this.height:e.clientY - this.height;
Css(this.obj,{top:top+'px',height:Math.max(height-2,0) + 'px'});
},
turnUp : function(e){
var top = this.max?Math.max(this.containerconfig.t,e.clientY):e.clientY;
var height = this.max?this.original[3]-top:this.original[3] - e.clientY;
Css(this.obj,{top : top +'px',height : Math.max(height,0) +'px'});
},
turnRight : function(e){
var width = this.max?Math.min(e.clientX,this.containerconfig.l+this.containerconfig.w)- this.width:e.clientX- this.width;
Css(this.obj,{left:this.width+'px',width:Math.max(width-2,0) +'px'});
},
turnLeft : function(e){
var left = this.max?Math.max(e.clientX,this.containerconfig.l):e.clientX;
var width = this.max?this.original[2]-left:this.original[2]-e.clientX;
Css(this.obj,{left:left +'px',width:Math.max(width,0)+'px'})
}
});
var resize;
window.onload = function(){
resize = new Resize($('ss'),$('container')).set($('rUp'),'up').set($('rDown'),'down').set($('rLeft'),'left').set($('rRight'),'right').set($('rLeftUp'),'leftUp').set($('rLeftDown'),'leftDown').set($('rRightDown'),'rightDown').set($('rRightUp'),'rightUp');
}
function vvresize(p){
var cc = {
a:function(){resize.max=true},
b:function(){resize.max=false},
c:function(){resize.min=true},
d:function(){resize.min=false}
}
cc[p]();
}
</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: