Heim > Web-Frontend > js-Tutorial > js通过八个点 拖动改变div大小的实现方法_javascript技巧

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

WBOY
Freigeben: 2016-05-16 16:57:03
Original
1515 Leute haben es durchsucht

复制代码 代码如下:




Resize













<script> <BR>var Sys = (function(ua){ <BR> var s = {}; <BR> s.IE = ua.match(/msie ([\d.]+)/)?true:false; <BR> s.Firefox = ua.match(/firefox\/([\d.]+)/)?true:false; <BR> s.Chrome = ua.match(/chrome\/([\d.]+)/)?true:false; <BR> s.IE6 = (s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6))?true:false; <BR> s.IE7 = (s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 7))?true:false; <BR> s.IE8 = (s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 8))?true:false; <BR> return s; <BR>})(navigator.userAgent.toLowerCase()); <P>var $ = function (id) { <BR> return document.getElementById(id); <BR>}; <P>var Css = function(e,o){ <BR> for(var i in o) <BR> e.style[i] = o[i]; <BR>}; <P>var Extend = function(destination, source) { <BR> for (var property in source) { <BR> destination[property] = source[property]; <BR> } <BR>}; <P>var Bind = function(object, fun) { <BR> var args = Array.prototype.slice.call(arguments).slice(2); <BR> return function() { <BR> return fun.apply(object, args); <BR> } <BR>}; <P>var BindAsEventListener = function(object, fun) { <BR> var args = Array.prototype.slice.call(arguments).slice(2); <BR> return function(event) { <BR> return fun.apply(object, [event || window.event].concat(args)); <BR> } <BR>}; <P>var CurrentStyle = function(element){ <BR> return element.currentStyle || document.defaultView.getComputedStyle(element, null); <BR>}; <P>function addListener(element,e,fn){ <BR> element.addEventListener?element.addEventListener(e,fn,false):element.attachEvent("on" + e,fn); <BR>}; <BR>function removeListener(element,e,fn){ <BR> element.removeEventListener?element.removeEventListener(e,fn,false):element.detachEvent("on" + e,fn) <BR>}; <P>var Class = function(properties){ <BR> var _class = function(){return (arguments[0] !== null && this.initialize && typeof(this.initialize) == 'function') ? this.initialize.apply(this, arguments) : this;}; <BR> _class.prototype = properties; <BR> return _class; <BR>}; <P>var Resize =new Class({ <BR> initialize : function(obj){ <BR> this.obj = obj; <BR> this.resizeelm = null; <BR> this.fun = null; //记录触发什么事件的索引 <BR> this.original = []; //记录开始状态的数组 <BR> this.width = null; <BR> this.height = null; <BR> this.fR = BindAsEventListener(this,this.resize); <BR> this.fS = Bind(this,this.stop); <BR> }, <BR> set : function(elm,direction){ <BR> if(!elm)return; <BR> this.resizeelm = elm; <BR> addListener(this.resizeelm,'mousedown',BindAsEventListener(this, this.start, this[direction])); <BR> return this; <BR> }, <BR> start : function(e,fun){ <BR> this.fun = fun; <BR> this.original = [parseInt(CurrentStyle(this.obj).width),parseInt(CurrentStyle(this.obj).height),parseInt(CurrentStyle(this.obj).left),parseInt(CurrentStyle(this.obj).top)];<BR> this.width = (this.original[2]||0) + this.original[0]; <BR> this.height = (this.original[3]||0) + this.original[1]; <BR> addListener(document,"mousemove",this.fR); <BR> addListener(document,'mouseup',this.fS); <BR> }, <BR> resize : function(e){ <BR> this.fun(e); <BR> Sys.IE?(this.resizeelm.onlosecapture=function(){this.fS()}):(this.resizeelm.onblur=function(){this.fS()}) <BR> }, <BR> stop : function(){ <BR> removeListener(document, "mousemove", this.fR); <BR> removeListener(document, "mousemove", this.fS); <BR> window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); <BR> }, <BR> up : function(e){ <BR> this.height>e.clientY?Css(this.obj,{top:e.clientY + "px",height:this.height-e.clientY + "px"}):this.turnDown(e); <BR> }, <BR> down : function(e){ <BR> e.clientY>this.original[3]?Css(this.obj,{top:this.original[3]+'px',height:e.clientY-this.original[3]+'px'}):this.turnUp(e); <BR> }, <BR> left : function(e){ <BR> e.clientX<this.width?Css(this.obj,{left:e.clientX +'px',width:this.width-e.clientX + "px"}):this.turnRight(e); <BR> }, <BR> right : function(e){ <BR> e.clientX>this.original[2]?Css(this.obj,{left:this.original[2]+'px',width:e.clientX-this.original[2]+"px"}):this.turnLeft(e) ; <BR> }, <BR> leftUp:function(e){ <BR> this.up(e);this.left(e); <BR> }, <BR> leftDown:function(e){ <BR> this.left(e);this.down(e); <BR> }, <BR> rightUp:function(e){ <BR> this.up(e);this.right(e); <BR> }, <BR> rightDown:function(e){ <BR> this.right(e);this.down(e); <BR> }, <BR> turnDown : function(e){ <BR> Css(this.obj,{top:this.height+'px',height:e.clientY - this.height + 'px'}); <BR> }, <BR> turnUp : function(e){ <BR> Css(this.obj,{top : e.clientY +'px',height : this.original[3] - e.clientY +'px'}); <BR> }, <BR> turnRight : function(e){ <BR> Css(this.obj,{left:this.width+'px',width:e.clientX- this.width +'px'}); <BR> }, <BR> turnLeft : function(e){ <BR> Css(this.obj,{left:e.clientX +'px',width:this.original[2]-e.clientX+'px'}); <BR> } <BR>}); <BR>window.onload = function(){ <BR> 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');<BR>} <BR></script>


Verwandte Etiketten:
js
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage