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 相关文章推荐
List the Codec Files on a Computer
Jun 11 Javascript
Javascript合并表格中具有相同内容单元格示例
Aug 11 Javascript
JavaScript prototype 使用介绍
Aug 29 Javascript
javascript匀速运动实现方法分析
Jan 08 Javascript
jQuery通过ajax请求php遍历json数组到table中的代码(推荐)
Jun 12 Javascript
AngularJS 防止页面闪烁的方法
Mar 09 Javascript
js 去掉字符串前后空格实现代码集合
Mar 25 Javascript
Javascript中this关键字指向问题的测试与详解
Aug 11 Javascript
jQuery实现表格冻结顶栏效果
Aug 20 jQuery
vue实现自定义日期组件功能的实例代码
Nov 06 Javascript
JS加载解析Markdown文档过程详解
May 19 Javascript
Vue.Draggable实现交换位置
Apr 07 Vue.js
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
Zend Framework教程之Loader以及PluginLoader用法详解
2016/03/09 PHP
JavaScript基础语法让人疑惑的地方小结
2012/05/23 Javascript
JavaScript高级程序设计(第3版)学习笔记2 js基础语法
2012/10/11 Javascript
JS中判断null、undefined与NaN的方法
2014/03/26 Javascript
JavaScript中的ArrayBuffer详细介绍
2014/12/08 Javascript
JS使用ajax从xml文件动态获取数据显示的方法
2015/03/24 Javascript
对JavaScript客户端应用编程的一些建议
2015/06/24 Javascript
Node.js操作Firebird数据库教程
2016/03/04 Javascript
require.js配合插件text.js实现最简单的单页应用程序
2016/07/12 Javascript
js实现的页面加载完毕之前loading提示效果完整示例【附demo源码下载】
2016/08/02 Javascript
javascript入门之数组[新手必看]
2016/11/21 Javascript
深入理解jquery中extend的实现
2016/12/22 Javascript
jQuery获取Table某列的值(推荐)
2017/03/03 Javascript
AngularJS读取JSON及XML文件的方法示例
2017/05/25 Javascript
原生JS实现的多个彩色小球跟随鼠标移动动画效果示例
2018/02/01 Javascript
详解Immutable及 React 中实践
2018/03/01 Javascript
Angular学习教程之RouterLink花式跳转
2018/05/03 Javascript
vue+express+jwt持久化登录的方法
2019/06/14 Javascript
超轻量级的js时间库miment使用解析
2019/08/02 Javascript
js+springMVC 提交数组数据到后台的实例
2019/09/21 Javascript
vue 导航内容设置选中状态样式的例子
2019/11/01 Javascript
ES6学习笔记之字符串、数组、对象、函数新增知识点实例分析
2020/01/22 Javascript
javascript实现京东快递单号的查询效果
2020/11/30 Javascript
Python yield与实现方法代码分析
2018/02/06 Python
Python Matplotlib 基于networkx画关系网络图
2019/07/10 Python
linux 下selenium chrome使用详解
2020/04/02 Python
pycharm开发一个简单界面和通用mvc模板(操作方法图解)
2020/05/27 Python
python sleep和wait对比总结
2021/02/03 Python
CSS3 制作绽放的莲花采用效果叠加实现
2013/01/31 HTML / CSS
基于CSS3实现的漂亮Menu菜单效果代码
2015/09/10 HTML / CSS
css3利用transform变形结合事件完成扇形导航
2020/10/26 HTML / CSS
Chain Reaction Cycles芬兰:世界上最大的在线自行车商店
2017/12/06 全球购物
六一儿童节活动总结
2014/08/27 职场文书
公司员工离职证明书
2014/10/04 职场文书
营销与策划实训报告
2014/11/05 职场文书
首席执行官观后感
2015/06/03 职场文书