js通过八个点 拖动改变div大小的实现方法


Posted in Javascript onMarch 05, 2014
<html> 
<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>
Javascript 相关文章推荐
基于jquery的页面划词搜索JS
Sep 14 Javascript
JS将数字转换成三位逗号分隔的样式(示例代码)
Feb 19 Javascript
简单介绍JavaScript中字符串创建的基本方法
Jul 07 Javascript
Seajs 简易文档 提供简单、极致的模块化开发体验
Apr 13 Javascript
jQuery使用serialize()表单序列化时出现中文乱码问题的解决办法
Jul 27 Javascript
用AngularJS的指令实现tabs切换效果
Aug 31 Javascript
AngularJS指令中的绑定策略实例分析
Dec 14 Javascript
jQuery Easyui datagrid editor为combobox时指定数据源实例
Dec 19 Javascript
JavaScript数据结构之广义表的定义与表示方法详解
Apr 12 Javascript
vue下history模式刷新后404错误解决方法
Aug 18 Javascript
基于vue-cli npm run build之后vendor.js文件过大的解决方法
Sep 27 Javascript
JS正则表达式验证端口范围(0-65535)
Jan 06 Javascript
javascript日期格式化示例分享
Mar 05 #Javascript
javascript避免数字计算精度误差的方法详解
Mar 05 #Javascript
javascript/jquery获取地址栏url参数的方法
Mar 05 #Javascript
js离开或刷新页面检测(且兼容FF,IE,Chrome)
Mar 05 #Javascript
js特殊字符过滤的示例代码
Mar 05 #Javascript
jquerymobile局部渲染的各种刷新方法小结
Mar 05 #Javascript
JqueryMobile动态生成listView并实现刷新的两种方法
Mar 05 #Javascript
You might like
先进的自动咖啡技术,真的可以取代咖啡师吗?
2021/03/06 冲泡冲煮
10个实用的PHP代码片段
2011/09/02 PHP
php设置允许大文件上传示例代码
2014/03/10 PHP
变量在 PHP7 内部的实现(一)
2015/12/21 PHP
PHP封装的MSSql操作类完整实例
2016/05/26 PHP
Mootools 1.2教程 设置和获取样式表属性
2009/09/15 Javascript
jquery easyui combox一些实用的小方法
2013/12/25 Javascript
JavaScript网页定位详解
2014/01/13 Javascript
常用的几段javascript代码分享
2014/03/25 Javascript
node.js中的fs.writeFile方法使用说明
2014/12/14 Javascript
基于BootStrap Metronic开发框架经验小结【八】框架功能总体界面介绍
2016/05/12 Javascript
jQuery+CSS3实现点赞功能
2017/03/13 Javascript
详解微信小程序设置底部导航栏目方法
2017/06/29 Javascript
Node.js创建HTTP文件服务器的使用示例
2018/05/11 Javascript
jQuery实现的页面详情展开收起功能示例
2018/06/11 jQuery
NodeJs之word文件生成与解析的实现代码
2019/04/01 NodeJs
详解如何提升JSON.stringify()的性能
2019/06/12 Javascript
vue使用微信扫一扫功能的实现代码
2020/04/11 Javascript
JavaScript enum枚举类型定义及使用方法
2020/05/15 Javascript
openlayers实现地图弹窗
2020/09/25 Javascript
python爬虫实战之爬取京东商城实例教程
2017/04/24 Python
Python中Threading用法详解
2017/12/27 Python
python selenium自动上传有赞单号的操作方法
2018/07/05 Python
python datetime中strptime用法详解
2019/08/29 Python
Python打包工具PyInstaller的安装与pycharm配置支持PyInstaller详细方法
2020/02/27 Python
Python爬虫JSON及JSONPath运行原理详解
2020/06/04 Python
opencv 形态学变换(开运算,闭运算,梯度运算)
2020/07/07 Python
纯CSS3实现滚动的齿轮动画效果
2014/06/05 HTML / CSS
草莓网美国官网:Strawberrynet USA
2016/12/11 全球购物
新奥尔良珠宝:Mignon Faget
2020/11/23 全球购物
借款协议书范本
2014/04/22 职场文书
捐款倡议书格式范文
2014/05/14 职场文书
经销商会议开幕词
2016/03/04 职场文书
2016年党员读书月活动总结
2016/04/06 职场文书
python munch库的使用解析
2021/05/25 Python
Windows Server 版本 20H2 于 8 月 9 日停止支持,Win10 版本 21H1 将于 12 月结束支
2022/07/23 数码科技