JavaScript 图片切割效果(带拖放、缩放效果)

本文转帖自:http://www.cnblogs.com/cloudgamer/archive/2008/07/21/1247267.html  以备他日所用

 

前言:

这个程序主要分三部分:层的拖放、层的缩放、图片切割(包括预览)。
其中层的拖放是很常见的效果,层的缩放有点难度,图片切割看着炫其实原理也很简单。
不过在实现的过程中也学习到很多以前不知道的东西,下面都会说明,希望大家从中也能学到东西。

效果:




 


 


 

 

原理:

【拖放程序】

基本原理很简单,不知道的看代码就明白,其中参考了越兔BlueDestiny的相关文章。

下面说一下比较有用的地方:

【范围限制】

首先当然是有拖放范围参数,分别是mxLeft(左边的left最小值)、mxRight(右边的left最大值)、mxTop(上边的top最小值)、mxBottom(下边的top最大值)。
然后在拖动程序Move()中看有没有超过,超过的话设回极限值就行:

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->if(this.Limit){
    
//获取超出长度
    var iRight = iLeft + this._obj.offsetWidth - this.mxRight, iBottom = iTop + this._obj.offsetHeight - this.mxBottom;
    
//这里是先设置右边下边再设置左边上边,可能会不准确
    if(iRight > 0) iLeft -= iRight;
    
if(iBottom > 0) iTop -= iBottom;
    
if(this.mxLeft > iLeft) iLeft = this.mxLeft;
    
if(this.mxTop > iTop) iTop = this.mxTop;
}

【释放选择】

我以前就用的方法是设置ie的onselectstart和ff的MozUserSelect,
但BlueDestiny说“用user-select会相当于event.preventDefault。阻止默认动作就会在某些操作的时候导致mouseup丢失。”,
最好的方法是ie用document.selection.empty(),ff用window.getSelection().removeAllRanges()。
所以可以在Move()中加入:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> window.getSelection  &&  window.getSelection().removeAllRanges();

这种写法是从越兔的程序中学到的。
因为ie的鼠标捕获默认(下面会说)带这个,所以ie就不用了。

【鼠标捕获】

以前不知道js有这个东西,使用很简单:
设置捕获:this.Drag.setCapture();
取消捕获:this.Drag.releaseCapture()。
它的作用是:将鼠标捕获设置到指定的对象。这个对象会为当前应用程序或整个系统接收所有鼠标输入。
还不明白的话,试试拖放的时候把鼠标拖放到浏览器外面,会发现拖动还在继续,
如果没有这个鼠标捕获就会失效了。
但在浏览器外是触发不了mouseup的,不过还可以用losecapture事件代替:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> addEventHandler( this .Drag,  " losecapture " this ._fS);
this .Drag.setCapture();

程序中给ff的window添加blur时停止的事件,越兔说是为了可以检测到alt+tab造成的mouseup丢失,完美一点,也加上去了。

这样一个拖放程序就完成了。


【缩放程序】

原理也很简单,根据鼠标的坐标来设置缩放对象样式。
除了设置width和height外,对于上边和左边的缩放还要设置left和top,详细可参考代码。

程序更重要的是结构设计,因为有八个方向又分普通和比例缩放,
要尽量抓出能重用的部分,不然程序的复杂度可想而知。
为了提高程序内函数重用度减低复杂度我做了以下设计:
1.设一个_fun程序存放缩放过程中要执行的程序(有点像委托);
2.计算四个样式初始值,缩放函数修改这些初始值,最后重新设置全部样式(为了减低复杂度不是按需修改);
3.对于普通缩放只需要四个方向的程序就够,像右下方向可以用执行右边和下边程序代替;
4.根据比例缩放程序和普通缩放程序可重用的部分抽出了四个修正程序(用了部分程序效率来换取);

下面是程序中比较有用的部分:

【边宽获取】

由于涉及到高度和宽度的修改,边框宽度的获取必不可少。
因为用offset取得的宽度或高度是包括了边框宽度的,style中的宽度和高度是不包括边框宽度的,
所以设置样式的时候必须在offset取得的宽度或高度的基础上减去边框宽度。

那怎么取得边框宽度呢?
直观的方法是通过parseInt(object.style.borderBottomWidth)来获取,但这是错误的,
因为这样只能获取style中设置的样式,而不能获取class中设置的样式。

要取得最终样式(实际的样式),在ie中可使用currentStyle取得,在ff中使用document.defaultView.getComputedStyle(object, null),
那么用下面的程序就可以获取边框宽度了:

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->var _style = this._obj.currentStyle || document.defaultView.getComputedStyle(this._obj, null);
this._xBorder = parseInt(_style.borderLeftWidth) + parseInt(_style.borderRightWidth);
this._yBorder = parseInt(_style.borderTopWidth) + parseInt(_style.borderBottomWidth);

【比例缩放】

比例缩放原理也很简单,在原有缩放的基础上,再按比例设置一次高和宽。
例如右下的比例缩放是先设置一次右边的普通缩放取得宽度,
根据比例取得高度后执行一次下边的修正程序,
由于此时高度经过修正可能已经改变了,最后需要再执行一次右边的修正程序。

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->this.SetRight(oEvent);
this.repairDown(parseInt(this.style_width / this._scale));
this.repairRight(parseInt(this.style_height * this._scale));


这样的缩放是以宽度优先的,对于上下两个点以高度优先会有更好的体验,
例如对于上面的点可以:

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->this.SetUp(oEvent);
this.repairRight(parseInt(this.style_height * this._scale));
this.repairUp(parseInt(this.style_width / this._scale));


对于上下左右四个点,更好的体验当然是以该点为中心缩放,
各位有兴趣的话可以把这个也做出来,写多四个修正程序对应这几个点就行。

而最小限制,范围限制可参照修正程序中的代码。
程序中也使用了跟拖放差不多的释放选择和鼠标捕获。

【鼠标捕获补充】

setCapture解决了ie中鼠标捕获的问题,但ff下的鼠标捕获还有问题。
当层的内部没有文本内容时,ie捕获正常,但ff在拖放到浏览器外时捕获就会失效,
暂时的解决方法只有插入文本,例如:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> resize.innerHTML  =   " <font size='1px'>&nbsp;</font> " ;

各位如果有更好解决方案的话记得通知我啊。

 

【图片切割】

关于图片切割的设计,有三个方法:
1.把图片设为背景图,通过设置背景图的位置来实现,但这样的缺点是只能按图片的正常比例实现,不够灵活;
2.把图片放到切割对象里面,通过设置图片的top和left实现,这个方法是可行,但下面有更简单的方法实现;
3.通过设置图片的clip来实现。

这个方法是从当年“珍藏”的代码中看到的,虽然以前接触过clip,但都忘了。
clip的作用是“检索或设置对象的可视区域。可视区域外的部分是透明的。”
依据上-右-下-左的顺序提供自对象左上角为(0,0)坐标计算的四个偏移数值来剪切。
例如:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> div { position:absolute; width:60px; height:60px; clip:rect( 0   20   50   10 ); }

注意position:absolute的设置是必须的(详细看手册)。

下面说说具体实现原理:
首先需要一个容器,拖放对象,图片地址,显示宽度和高度,
还要插入三个层:
底图层:那个半透明的图片,
显示层:拖放对象中正常显示的那个部分,
拖动层:就是拖放对象,
其中为了底图层和显示层能完美的重叠,我两个层都用了绝对定位,定在左上角。
zIndex也要设置一下,保证三个层的顺序。

下面是很简单但最重要设置切割函数SetPos(),按拖放对象的参数进行切割:

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->var o = this.Drag;
this._cropper.style.clip = "rect(" + o.offsetTop + "px " + (o.offsetLeft + o.offsetWidth) + "px " + (o.offsetTop + o.offsetHeight) + "px " + o.offsetLeft + "px)";

只要把SetPos()放到Drag的onMove和Resize的onResize中就行。
onMove和onResize分别是拖放和缩放时附加执行的程序。

程序中有一个Init()函数,它的作用是初始化一些设置,
特别分出来的原因是为了在用户修改了属性后执行一次,就可以根据修改过的属性重新初始化一次。
更好的做法是根据不同的属性修改修改需要修改的设置,这里我是偷懒了。

【切割预览】

至于预览效果也不难,根据预览高度宽度和拖放对象(显示层)的参数,计算出图片和预览图的比例scale:

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->var o = this.Drag, h = this.viewWidth, w = h * o.offsetWidth / o.offsetHeight;
if(w > this.viewHeight){ w = this.viewHeight; h = w * o.offsetHeight / o.offsetWidth; }
var scale = h / o.offsetHeight

通过这个比例就可以计算出预览图的width、height、top、left了:

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->var scale = h / o.offsetHeight, ph = this.Height * scale, pw = this.Width * scale, pt = o.offsetTop * scale, pl = o.offsetLeft * scale, styleView = this._view.style;
styleView.width 
= pw + "px"; styleView.height = ph + "px";
styleView.top 
= - pt + "px "; styleView.left = - pl + "px";

最好根据这些参数切割预览图:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> styleView.clip  =   " rect( "   +  pt  +   " px  "   +  (pl  +  w)  +   " px  "   +  (pt  +  h)  +   " px  "   +  pl  +   " px) " ;

预览图效果就做好了。

【拖放补充】

在ie中,如果对象的position为absolute,并且背景是透明的话,会触发不了鼠标事件(真的是透明了)。
我的解决方法是在对象里面加一个透明的width和height都是100%的层,这样就能解决了。
但ie6中还有问题,在js修改对象的高度后,ie6并居然不会自动填充,
还好BlueDestiny告诉我解决的方法,设置对象的overflow为hidden就可以解决,
BlueDestiny说“出现这个问题的原因是因为IE6渲染的问题,通过某些CSS的属性可以让DOM改变之后再次渲染。”
虽然不太明白,但总算解决:

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->this.Drag.style.overflow = "hidden";
(
function(style){
 style.width 
= style.height = "100%"; style.backgroundColor = "#fff"; style.filter = "alpha(opacity:0)";
})(
this.Drag.appendChild(document.createElement("div")).style)

源码:

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->var $ = function (id) {
    
return "string" == typeof id ? document.getElementById(id) : id;
};

var isIE = (document.all) ? true : false;

function addEventHandler(oTarget, sEventType, fnHandler) {
    
if (oTarget.addEventListener) {
        oTarget.addEventListener(sEventType, fnHandler, 
false);
    } 
else if (oTarget.attachEvent) {
        oTarget.attachEvent(
"on" + sEventType, fnHandler);
    } 
else {
        oTarget[
"on" + sEventType] = fnHandler;
    }
};

function removeEventHandler(oTarget, sEventType, fnHandler) {
    
if (oTarget.removeEventListener) {
        oTarget.removeEventListener(sEventType, fnHandler, 
false);
    } 
else if (oTarget.detachEvent) {
        oTarget.detachEvent(
"on" + sEventType, fnHandler);
    } 
else { 
        oTarget[
"on" + sEventType] = null;
    }
};


var Class = {
  create: 
function() {
    
return function() {
      
this.initialize.apply(this, arguments);
    }
  }
}

Object.extend 
= function(destination, source) {
    
for (var property in source) {
        destination[property] 
= source[property];
    }
    
return destination;
}

//拖放程序
var Drag = Class.create();
Drag.prototype 
= {
  
//拖放对象,触发对象
  initialize: function(obj, drag, options) {
    
var oThis = this;
    
    
this._obj = $(obj);//拖放对象
    this.Drag = $(drag);//触发对象
    this._x = this._y = 0;//记录鼠标相对拖放对象的位置
    //事件对象(用于移除事件)
    this._fM = function(e){ oThis.Move(window.event || e); }
    
this._fS = function(){ oThis.Stop(); }
    
    
this.SetOptions(options);
    
    
this.Limit = !!this.options.Limit;
    
this.mxLeft = parseInt(this.options.mxLeft);
    
this.mxRight = parseInt(this.options.mxRight);
    
this.mxTop = parseInt(this.options.mxTop);
    
this.mxBottom = parseInt(this.options.mxBottom);
    
    
this.onMove = this.options.onMove;
    
    
this._obj.style.position = "absolute";
    addEventHandler(
this.Drag, "mousedown"function(e){ oThis.Start(window.event || e); });
  },
  
//设置默认属性
  SetOptions: function(options) {
    
this.options = {//默认值
        Limit:        false,//是否设置限制(为true时下面参数有用,可以是负数)
        mxLeft:        0,//左边限制
        mxRight:    0,//右边限制
        mxTop:        0,//上边限制
        mxBottom:    0,//下边限制
        onMove:        function(){}//移动时执行
    };
    Object.extend(
this.options, options || {});
  },
  
//准备拖动
  Start: function(oEvent) {
    
//记录鼠标相对拖放对象的位置
    this._x = oEvent.clientX - this._obj.offsetLeft;
    
this._y = oEvent.clientY - this._obj.offsetTop;
    
//mousemove时移动 mouseup时停止
    addEventHandler(document, "mousemove"this._fM);
    addEventHandler(document, 
"mouseup"this._fS);
    
//使鼠标移到窗口外也能释放
    if(isIE){
        addEventHandler(
this.Drag, "losecapture"this._fS);
        
this.Drag.setCapture();
    }
else{
        addEventHandler(window, 
"blur"this._fS);
    }
  },
  
//拖动
  Move: function(oEvent) {
    
//清除选择(ie设置捕获后默认带这个)
    window.getSelection && window.getSelection().removeAllRanges();
    
//当前鼠标位置减去相对拖放对象的位置得到offset位置
    var iLeft = oEvent.clientX - this._x, iTop = oEvent.clientY - this._y;
    
//设置范围限制
    if(this.Limit){
        
//获取超出长度
        var iRight = iLeft + this._obj.offsetWidth - this.mxRight, iBottom = iTop + this._obj.offsetHeight - this.mxBottom;
        
//这里是先设置右边下边再设置左边上边,可能会不准确
        if(iRight > 0) iLeft -= iRight;
        
if(iBottom > 0) iTop -= iBottom;
        
if(this.mxLeft > iLeft) iLeft = this.mxLeft;
        
if(this.mxTop > iTop) iTop = this.mxTop;
    }
    
//设置位置
    this._obj.style.left = iLeft + "px";
    
this._obj.style.top = iTop + "px";    
    
//附加程序
    this.onMove();
  },
  
//停止拖动
  Stop: function() {
    
//移除事件
    removeEventHandler(document, "mousemove"this._fM);
    removeEventHandler(document, 
"mouseup"this._fS);
    
if(isIE){
        removeEventHandler(
this.Drag, "losecapture"this._fS);
        
this.Drag.releaseCapture();
    }
else{
        removeEventHandler(window, 
"blur"this._fS);
    }
  }
};

//缩放程序
var Resize = Class.create();
Resize.prototype 
= {
  
//缩放对象
  initialize: function(obj, options) {
    
var oThis = this;
    
    
this._obj = $(obj);//缩放对象
    this._right = this._down = this._left = this._up = 0;//坐标参数
    this._scale = 1;//比例参数
    this._touch = null;//当前触发对象
    
    
//用currentStyle(ff用getComputedStyle)取得最终样式
    var _style = this._obj.currentStyle || document.defaultView.getComputedStyle(this._obj, null);
    
this._xBorder = parseInt(_style.borderLeftWidth) + parseInt(_style.borderRightWidth);
    
this._yBorder = parseInt(_style.borderTopWidth) + parseInt(_style.borderBottomWidth);
    
    
//事件对象(用于移除事件)
    this._fR = function(e){ oThis.Resize(e); }
    
this._fS = function(){ oThis.Stop(); }
    
    
this.SetOptions(options);
    
    
this.Limit = !!this.options.Limit;
    
this.mxLeft = parseInt(this.options.mxLeft);
    
this.mxRight = parseInt(this.options.mxRight);
    
this.mxTop = parseInt(this.options.mxTop);
    
this.mxBottom = parseInt(this.options.mxBottom);
    
    
this.MinWidth = parseInt(this.options.MinWidth);
    
this.MinHeight = parseInt(this.options.MinHeight);
    
this.Scale = !!this.options.Scale;
    
this.onResize = this.options.onResize;
    
    
this._obj.style.position = "absolute";
  },
  
//设置默认属性
  SetOptions: function(options) {
    
this.options = {//默认值
        Limit:        false,//是否设置限制(为true时下面mx参数有用)
        mxLeft:        0,//左边限制
        mxRight:    0,//右边限制
        mxTop:        0,//上边限制
        mxBottom:    0,//下边限制
        MinWidth:    50,//最小宽度
        MinHeight:    50,//最小高度
        Scale:        false,//是否按比例缩放
        onResize:    function(){}//缩放时执行
    };
    Object.extend(
this.options, options || {});
  },
  
//设置触发对象
  Set: function(resize, side) {
    
var oThis = this, resize = $(resize), _fun, _cursor;
    
if(!resize) return;
    
//根据方向设置 _fun是缩放时执行的程序 _cursor是鼠标样式
    switch (side.toLowerCase()) {
    
case "up" :
        _fun 
= function(e){ if(oThis.Scale){ oThis.scaleUpRight(e); }else{ oThis.SetUp(e); } };
        _cursor 
= "n-resize";
        
break;
    
case "down" :
        _fun 
= function(e){ if(oThis.Scale){ oThis.scaleDownRight(e); }else{ oThis.SetDown(e); } };
        _cursor 
= "n-resize";
        
break;
    
case "left" :
        _fun 
= function(e){ if(oThis.Scale){ oThis.scaleLeftUp(e); }else{ oThis.SetLeft(e); } };
        _cursor 
= "e-resize";
        
break;
    
case "right" :
        _fun 
= function(e){ if(oThis.Scale){ oThis.scaleRightDown(e); }else{ oThis.SetRight(e); } };
        _cursor 
= "e-resize";
        
break;
    
case "left-up" :
        _fun 
= function(e){ if(oThis.Scale){ oThis.scaleLeftUp(e); }else{ oThis.SetLeft(e); oThis.SetUp(e); } };
        _cursor 
= "nw-resize";
        
break;
    
case "right-up" :
        _fun 
= function(e){ if(oThis.Scale){ oThis.scaleRightUp(e); }else{ oThis.SetRight(e); oThis.SetUp(e); } };
        _cursor 
= "ne-resize";
        
break;
    
case "left-down" :
        _fun 
= function(e){ if(oThis.Scale){ oThis.scaleLeftDown(e); }else{ oThis.SetLeft(e); oThis.SetDown(e); } };
        _cursor 
= "ne-resize";
        
break;
    
case "right-down" :
    
default :
        _fun 
= function(e){ if(oThis.Scale){ oThis.scaleRightDown(e); }else{ oThis.SetRight(e); oThis.SetDown(e); } };
        _cursor 
= "nw-resize";
    }
    
//插入字符解决ff下捕获效的问题
    if(!isIE){ resize.innerHTML = "<font size='1px'>&nbsp;</font>"; }
    
//设置触发对象
    resize.style.cursor = _cursor;
    addEventHandler(resize, 
"mousedown"function(e){ oThis._fun = _fun; oThis._touch = resize; oThis.Start(window.event || e); });
  },
  
//准备缩放
  Start: function(oEvent, o) {    
    
//防止冒泡
    if(isIE){ oEvent.cancelBubble = true; } else { oEvent.stopPropagation(); }
    
//计算样式初始值
    this.style_width = this._obj.offsetWidth;
    
this.style_height = this._obj.offsetHeight;
    
this.style_left = this._obj.offsetLeft;
    
this.style_top = this._obj.offsetTop;
    
//设置缩放比例
    if(this.Scale){ this._scale = this.style_width / this.style_height; }
    
//计算当前边的对应另一条边的坐标 例如右边缩放时需要左边界坐标
    this._left = oEvent.clientX - this.style_width;
    
this._right = oEvent.clientX + this.style_width;
    
this._up = oEvent.clientY - this.style_height;
    
this._down = oEvent.clientY + this.style_height;
    
//如果有范围 先计算好范围内最大宽度和高度
    if(this.Limit){
        
this._mxRight = this.mxRight - this._obj.offsetLeft;
        
this._mxDown = this.mxBottom - this._obj.offsetTop;
        
this._mxLeft = this.mxLeft + this.style_width + this._obj.offsetLeft;
        
this._mxUp = this.mxTop + this.style_height + this._obj.offsetTop;
    }
    
//mousemove时缩放 mouseup时停止
    addEventHandler(document, "mousemove"this._fR);
    addEventHandler(document, 
"mouseup"this._fS);
    
    
//使鼠标移到窗口外也能释放
    if(isIE){
        addEventHandler(
this._touch, "losecapture"this._fS);
        
this._touch.setCapture();
    }
else{
        addEventHandler(window, 
"blur"this._fS);
    }
  },  
  
//缩放
  Resize: function(e) {
    
//没有触发对象的话返回
    if(!this._touch) return;
    
//清除选择(ie设置捕获后默认带这个)
    window.getSelection && window.getSelection().removeAllRanges();
    
//执行缩放程序
    this._fun(window.event || e);
    
//设置样式
    //因为计算用的offset是把边框算进去的所以要减去
    this._obj.style.width = this.style_width - this._xBorder + "px";
    
this._obj.style.height = this.style_height - this._yBorder + "px";
    
this._obj.style.top = this.style_top + "px";
    
this._obj.style.left = this.style_left + "px";    
    
//附加程序
    this.onResize();
  },
  
//普通缩放
  //右边
  SetRight: function(oEvent) {
    
//当前坐标位置减去左边的坐标等于当前宽度
    this.repairRight(oEvent.clientX - this._left);
  },
  
//下边
  SetDown: function(oEvent) {
    
this.repairDown(oEvent.clientY - this._up);
  },
  
//左边
  SetLeft: function(oEvent) {
    
//右边的坐标减去当前坐标位置等于当前宽度
    this.repairLeft(this._right - oEvent.clientX);
  },
  
//上边
  SetUp: function(oEvent) {
    
this.repairUp(this._down - oEvent.clientY);
  },
  
//比例缩放
  //比例缩放右下
  scaleRightDown: function(oEvent) {
    
//先计算宽度然后按比例计算高度最后根据确定的高度计算宽度(宽度优先)
    this.SetRight(oEvent);
    
this.repairDown(parseInt(this.style_width / this._scale));
    
this.repairRight(parseInt(this.style_height * this._scale));
  },
  
//比例缩放左下
  scaleLeftDown: function(oEvent) {
    
this.SetLeft(oEvent);
    
this.repairDown(parseInt(this.style_width / this._scale));
    
this.repairLeft(parseInt(this.style_height * this._scale));
  },
  
//比例缩放右上
  scaleRightUp: function(oEvent) {
    
this.SetRight(oEvent);
    
this.repairUp(parseInt(this.style_width / this._scale));
    
this.repairRight(parseInt(this.style_height * this._scale));
  },
  
//比例缩放左上
  scaleLeftUp: function(oEvent) {
    
this.SetLeft(oEvent);
    
this.repairUp(parseInt(this.style_width / this._scale));
    
this.repairLeft(parseInt(this.style_height * this._scale));
  },
  
//这里是高度优先用于上下两边(体验更好)
  //比例缩放下右
  scaleDownRight: function(oEvent) {
    
this.SetDown(oEvent);
    
this.repairRight(parseInt(this.style_height * this._scale));
    
this.repairDown(parseInt(this.style_width / this._scale));
  },
  
//比例缩放上右
  scaleUpRight: function(oEvent) {
    
this.SetUp(oEvent);
    
this.repairRight(parseInt(this.style_height * this._scale));
    
this.repairUp(parseInt(this.style_width / this._scale));
  },
  
//修正程序
  //修正右边
  repairRight: function(iWidth) {
    
//右边和下边只要设置宽度和高度就行
    //当少于最少宽度
    if (iWidth < this.MinWidth){ iWidth = this.MinWidth; }
    
//当超过当前设定的最大宽度
    if(this.Limit && iWidth > this._mxRight){ iWidth = this._mxRight; }
    
//修改样式
    this.style_width = iWidth;
  },
  
//修正下边
  repairDown: function(iHeight) {
    
if (iHeight < this.MinHeight){ iHeight = this.MinHeight; }
    
if(this.Limit && iHeight > this._mxDown){ iHeight = this._mxDown; }
    
this.style_height = iHeight;
  },
  
//修正左边
  repairLeft: function(iWidth) {
    
//左边和上边比较麻烦 因为还要计算left和top
    //当少于最少宽度
    if (iWidth < this.MinWidth){ iWidth = this.MinWidth; }
    
//当超过当前设定的最大宽度
    else if(this.Limit && iWidth > this._mxLeft){ iWidth = this._mxLeft; }
    
//修改样式
    this.style_width = iWidth;
    
this.style_left = this._obj.offsetLeft + this._obj.offsetWidth - iWidth;
  },
  
//修正上边
  repairUp: function(iHeight) {
    
if(iHeight < this.MinHeight){ iHeight = this.MinHeight; }
    
else if(this.Limit && iHeight > this._mxUp){ iHeight = this._mxUp; }
    
this.style_height = iHeight;
    
this.style_top = this._obj.offsetTop + this._obj.offsetHeight - iHeight;
  },
  
//停止缩放
  Stop: function() {
    
//移除事件
    removeEventHandler(document, "mousemove"this._fR);
    removeEventHandler(document, 
"mouseup"this._fS);
    
if(isIE){
        removeEventHandler(
this._touch, "losecapture"this._fS);
        
this._touch.releaseCapture();
    }
else{
        removeEventHandler(window, 
"blur"this._fS);
    }
    
this._touch = null;
  }
};


//图片切割
var ImgCropper = Class.create();
ImgCropper.prototype 
= {
  
//容器对象,拖放缩放对象,图片地址,宽度,高度
  initialize: function(container, drag, url, width, height, options) {
    
var oThis = this;
    
    
//容器对象
    this.Container = $(container);
    
this.Container.style.position = "relative";
    
this.Container.style.overflow = "hidden";
    
    
//拖放对象
    this.Drag = $(drag);
    
this.Drag.style.cursor = "move";
    
this.Drag.style.zIndex = 200;
    
if(isIE){
        
//设置overflow解决ie6的渲染问题(缩放时填充对象高度的问题)
        this.Drag.style.overflow = "hidden";
        
//ie下用一个透明的层填充拖放对象 不填充的话onmousedown会失效(未知原因)
        (function(style){
            style.width 
= style.height = "100%"; style.backgroundColor = "#fff"; style.filter = "alpha(opacity:0)";
        })(
this.Drag.appendChild(document.createElement("div")).style)
    }
else{
        
//插入字符解决ff下捕获失效的问题
        this.Drag.innerHTML += "<font size='1px'>&nbsp;</font>";
    }
    
    
this._pic = this.Container.appendChild(document.createElement("img"));//图片对象
    this._cropper = this.Container.appendChild(document.createElement("img"));//切割对象
    this._pic.style.position = this._cropper.style.position = "absolute";
    
this._pic.style.top = this._pic.style.left = this._cropper.style.top = this._cropper.style.left = "0";//对齐
    this._cropper.style.zIndex = 100;
    
this._cropper.onload = function(){ oThis.SetPos(); }
    
    
this.Url = url;//图片地址
    this.Width = parseInt(width);//宽度
    this.Height = parseInt(height);//高度
    
    
this.SetOptions(options);
    
    
this.Opacity = parseInt(this.options.Opacity);
    
this.dragTop = parseInt(this.options.dragTop);
    
this.dragLeft = parseInt(this.options.dragLeft);
    
this.dragWidth = parseInt(this.options.dragWidth);
    
this.dragHeight = parseInt(this.options.dragHeight);
    
    
//设置预览对象
    this.View = $(this.options.View) || null;//预览对象
    this.viewWidth = parseInt(this.options.viewWidth);
    
this.viewHeight = parseInt(this.options.viewHeight);
    
this._view = null;//预览图片对象
    if(this.View){
        
this.View.style.position = "relative";
        
this.View.style.overflow = "hidden";
        
this._view = this.View.appendChild(document.createElement("img"));
        
this._view.style.position = "absolute";
    }
    
    
this.Scale = parseInt(this.options.Scale);
    
    
//设置拖放
    this._drag = new Drag(this.Drag, this.Drag, { Limit: true, onMove: function(){ oThis.SetPos(); } });
    
//设置缩放
    this._resize = this.GetResize();
    
    
this.Init();
  },
  
//设置默认属性
  SetOptions: function(options) {
    
this.options = {//默认值
        Opacity:    50,//透明度(0到100)    
        //拖放位置和宽高
        dragTop:    0,
        dragLeft:    
0,
        dragWidth:    
100,
        dragHeight:    
100,
        
//缩放触发对象
        Right:        "",
        Left:        
"",
        Up:            
"",
        Down:        
"",
        RightDown:    
"",
        LeftDown:    
"",
        RightUp:    
"",
        LeftUp:        
"",
        Scale:        
false,//是否按比例缩放
        //预览对象设置
        View:    "",//预览对象
        viewWidth:    100,//预览宽度
        viewHeight:    100//预览高度
    };
    Object.extend(
this.options, options || {});
  },
  
//初始化对象
  Init: function() {
    
var oThis = this;
    
    
//设置容器
    this.Container.style.width = this.Width + "px";
    
this.Container.style.height = this.Height + "px";
    
    
//设置拖放对象
    this.Drag.style.top = this.dragTop + "px";
    
this.Drag.style.left = this.dragLeft + "px";
    
this.Drag.style.width = this.dragWidth + "px";
    
this.Drag.style.height = this.dragHeight + "px";
    
    
//设置切割对象
    this._pic.src = this._cropper.src = this.Url;
    
this._pic.style.width = this._cropper.style.width = this.Width + "px";
    
this._pic.style.height = this._cropper.style.height = this.Height + "px";
    
if(isIE){
        
this._pic.style.filter = "alpha(opacity:" + this.Opacity + ")";
    } 
else {
        
this._pic.style.opacity = this.Opacity / 100;
    }
    
    
//设置预览对象
    if(this.View){ this._view.src = this.Url; }
    
    
//设置拖放
    this._drag.mxRight = this.Width; this._drag.mxBottom = this.Height;
    
//设置缩放
    if(this._resize){ this._resize.mxRight = this.Width; this._resize.mxBottom = this.Height; this._resize.Scale = this.Scale; }
  },
  
//设置获取缩放对象
  GetResize: function() {
    
var op = this.options;
    
//有触发对象时才设置
    if(op.RightDown || op.LeftDown || op.RightUp || op.LeftUp || op.Right || op.Left || op.Up || op.Down ){
        
var oThis = this, _resize = new Resize(this.Drag, { Limit: true, onResize: function(){ oThis.SetPos(); } });
        
        
//设置缩放触发对象
        if(op.RightDown){ _resize.Set(op.RightDown, "right-down"); }
        
if(op.LeftDown){ _resize.Set(op.LeftDown, "left-down"); }
        
        
if(op.RightUp){ _resize.Set(op.RightUp, "right-up"); }
        
if(op.LeftUp){ _resize.Set(op.LeftUp, "left-up"); }
        
        
if(op.Right){ _resize.Set(op.Right, "right"); }
        
if(op.Left){ _resize.Set(op.Left, "left"); }
        
        
if(op.Up){ _resize.Set(op.Up, "up"); }
        
if(op.Down){ _resize.Set(op.Down, "down"); }
        
        
return _resize;
    } 
else { return null; }
  },  
  
//设置切割
  SetPos: function() {
    
var o = this.Drag;
    
//按拖放对象的参数进行切割
    this._cropper.style.clip = "rect(" + o.offsetTop + "px " + (o.offsetLeft + o.offsetWidth) + "px " + (o.offsetTop + o.offsetHeight) + "px " + o.offsetLeft + "px)";
    
//切割预览
    if(this.View) this.PreView();
  },  
  
//切割预览
  PreView: function() {
    
//按比例设置宽度和高度
    var o = this.Drag, h = this.viewWidth, w = h * o.offsetWidth / o.offsetHeight;
    
if(w > this.viewHeight){ w = this.viewHeight; h = w * o.offsetHeight / o.offsetWidth; }
    
//获取对应比例尺寸
    var scale = h / o.offsetHeight, ph = this.Height * scale, pw = this.Width * scale, pt = o.offsetTop * scale, pl = o.offsetLeft * scale, styleView = this._view.style;
    
//设置样式
    styleView.width = pw + "px"; styleView.height = ph + "px";
    styleView.top 
= - pt + "px "; styleView.left = - pl + "px";
    
//切割预览图
    styleView.clip = "rect(" + pt + "px " + (pl + w) + "px " + (pt + h) + "px " + pl + "px)";
  }
}

 

使用说明:

首先需要5个参数,分别是:容器对象、拖放对象、图片地址、图片宽度、图片高度。
可选设置:
Opacity:透明度(0到100),
dragTop:拖放对象top,
dragLeft:拖放对象left,
dragWidth:拖放对象宽度,
dragHeight:拖放对象高度,
缩放触发对象:
Right,Left,Up,Down,RightDown,LeftDown,RightUp,LeftUp,
Scale:是否按比例缩放,
View:预览对象,
viewWidth:预览宽度,
viewHeight:预览高度,

实例化对象:

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->var ic = new ImgCropper("bgDiv""dragDiv""1.jpg"400500, {
 dragTop: 
50, dragLeft: 50,
 Right: 
"rRight", Left: "rLeft", Up: "rUp", Down: "rDown",
 RightDown: 
"rRightDown", LeftDown: "rLeftDown", RightUp: "rRightUp", LeftUp: "rLeftUp",
 View: 
"viewDiv", viewWidth: 200, viewHeight: 200
})

可以根据需要扩展,例如:

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->//设置图片大小
function Size(w, h){
    ic.Width 
= w;
    ic.Height 
= h;
    ic.Init();
}
//换图片
function Pic(url){
    ic.Url 
= url;
    ic.Init();
}
//设置透明度
function Opacity(i){
    ic.Opacity 
= i;
    ic.Init();
}
//是否使用比例
function Scale(b){
    ic.Scale 
= b;
    ic.Init();
}

补充:

里面的Drag拖放程序和Resize缩放程序是可以独立出来用的,
ImgCropper图片切割程序只是在内部实例化了这两个对象。

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>JavaScript 图片切割效果(带拖放、缩放效果) </title>
  6. </head>
  7. <body>
  8. <style type="text/css">
  9. #rRightDown,#rLeftDown,#rLeftUp,#rRightUp,#rRight,#rLeft,#rUp,#rDown{position:absolute;background:#F00;width:5px; height:5px; z-index:500; font-size:0;}
  10. #dragDiv{border:1px solid #000000;}
  11. </style>
  12. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  13.   <tr>
  14.     <td width="50%"><div id="bgDiv" style="width:400px; height:500px;">
  15.         <div id="dragDiv">
  16.           <div id="rRightDown" style="right:0; bottom:0;"> </div>
  17.           <div id="rLeftDown" style="left:0; bottom:0;"> </div>
  18.           <div id="rRightUp" style="right:0; top:0;"> </div>
  19.           <div id="rLeftUp" style="left:0; top:0;"> </div>
  20.           <div id="rRight" style="right:0; top:50%;"> </div>
  21.           <div id="rLeft" style="left:0; top:50%;"> </div>
  22.           <div id="rUp" style="top:0; left:50%;"> </div>
  23.           <div id="rDown" style="bottom:0;left:50%;"></div>
  24.         </div>
  25.       </div></td>
  26.     <td><div id="viewDiv" style="width:200px; height:200px;"> </div></td>
  27.   </tr>
  28. </table>
  29. <script>
  30. var $ = function (id) {
  31.     return "string" == typeof id ? document.getElementById(id) : id;
  32. };
  33. var isIE = (document.all) ? true : false;
  34. function addEventHandler(oTarget, sEventType, fnHandler) {
  35.     if (oTarget.addEventListener) {
  36.         oTarget.addEventListener(sEventType, fnHandler, false);
  37.     } else if (oTarget.attachEvent) {
  38.         oTarget.attachEvent("on" + sEventType, fnHandler);
  39.     } else {
  40.         oTarget["on" + sEventType] = fnHandler;
  41.     }
  42. };
  43. function removeEventHandler(oTarget, sEventType, fnHandler) {
  44.     if (oTarget.removeEventListener) {
  45.         oTarget.removeEventListener(sEventType, fnHandler, false);
  46.     } else if (oTarget.detachEvent) {
  47.         oTarget.detachEvent("on" + sEventType, fnHandler);
  48.     } else { 
  49.         oTarget["on" + sEventType] = null;
  50.     }
  51. };
  52. var Class = {
  53.   create: function() {
  54.     return function() {
  55.       this.initialize.apply(this, arguments);
  56.     }
  57.   }
  58. }
  59. Object.extend = function(destination, source) {
  60.     for (var property in source) {
  61.         destination[property] = source[property];
  62.     }
  63.     return destination;
  64. }
  65. //拖放程序
  66. var Drag = Class.create();
  67. Drag.prototype = {
  68.   //拖放对象,触发对象
  69.   initialize: function(obj, drag, options) {
  70.     var oThis = this;
  71.     
  72.     this._obj = $(obj);//拖放对象
  73.     this.Drag = $(drag);//触发对象
  74.     thisthis._x = this._y = 0;//记录鼠标相对拖放对象的位置
  75.     //事件对象(用于移除事件)
  76.     this._fM = function(e){ oThis.Move(window.event || e); }
  77.     this._fS = function(){ oThis.Stop(); }
  78.     
  79.     this.SetOptions(options);
  80.     
  81.     this.Limit = !!this.options.Limit;
  82.     this.mxLeft = parseInt(this.options.mxLeft);
  83.     this.mxRight = parseInt(this.options.mxRight);
  84.     this.mxTop = parseInt(this.options.mxTop);
  85.     this.mxBottom = parseInt(this.options.mxBottom);
  86.     
  87.     thisthis.onMove = this.options.onMove;
  88.     
  89.     this._obj.style.position = "absolute";
  90.     addEventHandler(this.Drag, "mousedown", function(e){ oThis.Start(window.event || e); });
  91.   },
  92.   //设置默认属性
  93.   SetOptions: function(options) {
  94.     this.options = {//默认值
  95.         Limit:      false,//是否设置限制(为true时下面参数有用,可以是负数)
  96.         mxLeft:     0,//左边限制
  97.         mxRight:    0,//右边限制
  98.         mxTop:      0,//上边限制
  99.         mxBottom:   0,//下边限制
  100.         onMove:     function(){}//移动时执行
  101.     };
  102.     Object.extend(this.options, options || {});
  103.   },
  104.   //准备拖动
  105.   Start: function(oEvent) {
  106.     //记录鼠标相对拖放对象的位置
  107.     this._x = oEvent.clientX - this._obj.offsetLeft;
  108.     this._y = oEvent.clientY - this._obj.offsetTop;
  109.     //mousemove时移动 mouseup时停止
  110.     addEventHandler(document, "mousemove", this._fM);
  111.     addEventHandler(document, "mouseup", this._fS);
  112.     //使鼠标移到窗口外也能释放
  113.     if(isIE){
  114.         addEventHandler(this.Drag, "losecapture", this._fS);
  115.         this.Drag.setCapture();
  116.     }else{
  117.         addEventHandler(window, "blur", this._fS);
  118.     }
  119.   },
  120.   //拖动
  121.   Move: function(oEvent) {
  122.     //清除选择(ie设置捕获后默认带这个)
  123.     window.getSelection && window.getSelection().removeAllRanges();
  124.     //当前鼠标位置减去相对拖放对象的位置得到offset位置
  125.     var iLeft = oEvent.clientX - this._x, iTop = oEvent.clientY - this._y;
  126.     //设置范围限制
  127.     if(this.Limit){
  128.         //获取超出长度
  129.         var iRight = iLeft + this._obj.offsetWidth - this.mxRight, iBottom = iTop + this._obj.offsetHeight - this.mxBottom;
  130.         //这里是先设置右边下边再设置左边上边,可能会不准确
  131.         if(iRight > 0) iLeft -iRight;
  132.         if(iBottom > 0) iTop -iBottom;
  133.         if(this.mxLeft > iLeft) iLeft = this.mxLeft;
  134.         if(this.mxTop > iTop) iTop = this.mxTop;
  135.     }
  136.     //设置位置
  137.     this._obj.style.left = iLeft + "px";
  138.     this._obj.style.top = iTop + "px";  
  139.     //附加程序
  140.     this.onMove();
  141.   },
  142.   //停止拖动
  143.   Stop: function() {
  144.     //移除事件
  145.     removeEventHandler(document, "mousemove", this._fM);
  146.     removeEventHandler(document, "mouseup", this._fS);
  147.     if(isIE){
  148.         removeEventHandler(this.Drag, "losecapture", this._fS);
  149.         this.Drag.releaseCapture();
  150.     }else{
  151.         removeEventHandler(window, "blur", this._fS);
  152.     }
  153.   }
  154. };
  155. //缩放程序
  156. var Resize = Class.create();
  157. Resize.prototype = {
  158.   //缩放对象
  159.   initialize: function(obj, options) {
  160.     var oThis = this;
  161.     
  162.     this._obj = $(obj);//缩放对象
  163.     thisthis._right = this._down = this._left = this._up = 0;//坐标参数
  164.     this._scale = 1;//比例参数
  165.     this._touch = null;//当前触发对象
  166.     
  167.     //用currentStyle(ff用getComputedStyle)取得最终样式
  168.     var _style = this._obj.currentStyle || document.defaultView.getComputedStyle(this._obj, null);
  169.     this._xBorder = parseInt(_style.borderLeftWidth) + parseInt(_style.borderRightWidth);
  170.     this._yBorder = parseInt(_style.borderTopWidth) + parseInt(_style.borderBottomWidth);
  171.     
  172.     //事件对象(用于移除事件)
  173.     this._fR = function(e){ oThis.Resize(e); }
  174.     this._fS = function(){ oThis.Stop(); }
  175.     
  176.     this.SetOptions(options);
  177.     
  178.     this.Limit = !!this.options.Limit;
  179.     this.mxLeft = parseInt(this.options.mxLeft);
  180.     this.mxRight = parseInt(this.options.mxRight);
  181.     this.mxTop = parseInt(this.options.mxTop);
  182.     this.mxBottom = parseInt(this.options.mxBottom);
  183.     
  184.     this.MinWidth = parseInt(this.options.MinWidth);
  185.     this.MinHeight = parseInt(this.options.MinHeight);
  186.     this.Scale = !!this.options.Scale;
  187.     thisthis.onResize = this.options.onResize;
  188.     
  189.     this._obj.style.position = "absolute";
  190.   },
  191.   //设置默认属性
  192.   SetOptions: function(options) {
  193.     this.options = {//默认值
  194.         Limit:      false,//是否设置限制(为true时下面mx参数有用)
  195.         mxLeft:     0,//左边限制
  196.         mxRight:    0,//右边限制
  197.         mxTop:      0,//上边限制
  198.         mxBottom:   0,//下边限制
  199.         MinWidth:   50,//最小宽度
  200.         MinHeight:  50,//最小高度
  201.         Scale:      false,//是否按比例缩放
  202.         onResize:   function(){}//缩放时执行
  203.     };
  204.     Object.extend(this.options, options || {});
  205.   },
  206.   //设置触发对象
  207.   Set: function(resize, side) {
  208.     var oThis = thisresize = $(resize), _fun, _cursor;
  209.     if(!resize) return;
  210.     //根据方向设置 _fun是缩放时执行的程序 _cursor是鼠标样式
  211.     switch (side.toLowerCase()) {
  212.     case "up" :
  213.         _fun = function(e){ if(oThis.Scale){ oThis.scaleUpRight(e); }else{ oThis.SetUp(e); } };
  214.         _cursor = "n-resize";
  215.         break;
  216.     case "down" :
  217.         _fun = function(e){ if(oThis.Scale){ oThis.scaleDownRight(e); }else{ oThis.SetDown(e); } };
  218.         _cursor = "n-resize";
  219.         break;
  220.     case "left" :
  221.         _fun = function(e){ if(oThis.Scale){ oThis.scaleLeftUp(e); }else{ oThis.SetLeft(e); } };
  222.         _cursor = "e-resize";
  223.         break;
  224.     case "right" :
  225.         _fun = function(e){ if(oThis.Scale){ oThis.scaleRightDown(e); }else{ oThis.SetRight(e); } };
  226.         _cursor = "e-resize";
  227.         break;
  228.     case "left-up" :
  229.         _fun = function(e){ if(oThis.Scale){ oThis.scaleLeftUp(e); }else{ oThis.SetLeft(e); oThis.SetUp(e); } };
  230.         _cursor = "nw-resize";
  231.         break;
  232.     case "right-up" :
  233.         _fun = function(e){ if(oThis.Scale){ oThis.scaleRightUp(e); }else{ oThis.SetRight(e); oThis.SetUp(e); } };
  234.         _cursor = "ne-resize";
  235.         break;
  236.     case "left-down" :
  237.         _fun = function(e){ if(oThis.Scale){ oThis.scaleLeftDown(e); }else{ oThis.SetLeft(e); oThis.SetDown(e); } };
  238.         _cursor = "ne-resize";
  239.         break;
  240.     case "right-down" :
  241.     default :
  242.         _fun = function(e){ if(oThis.Scale){ oThis.scaleRightDown(e); }else{ oThis.SetRight(e); oThis.SetDown(e); } };
  243.         _cursor = "nw-resize";
  244.     }
  245.     //插入字符解决ff下捕获效的问题
  246.     if(!isIE){ resize.innerHTML = "<font size='1px'> </font>"; }
  247.     //设置触发对象
  248.     resize.style.cursor = _cursor;
  249.     addEventHandler(resize, "mousedown", function(e){ oThis._fun = _fun; oThis._touch = resize; oThis.Start(window.event || e); });
  250.   },
  251.   //准备缩放
  252.   Start: function(oEvent, o) {  
  253.     //防止冒泡
  254.     if(isIE){ oEvent.cancelBubble = true; } else { oEvent.stopPropagation(); }
  255.     //计算样式初始值
  256.     thisthis.style_width = this._obj.offsetWidth;
  257.     thisthis.style_height = this._obj.offsetHeight;
  258.     thisthis.style_left = this._obj.offsetLeft;
  259.     thisthis.style_top = this._obj.offsetTop;
  260.     //设置缩放比例
  261.     if(this.Scale){ thisthis._scale = this.style_width / this.style_height; }
  262.     //计算当前边的对应另一条边的坐标 例如右边缩放时需要左边界坐标
  263.     this._left = oEvent.clientX - this.style_width;
  264.     this._right = oEvent.clientX + this.style_width;
  265.     this._up = oEvent.clientY - this.style_height;
  266.     this._down = oEvent.clientY + this.style_height;
  267.     //如果有范围 先计算好范围内最大宽度和高度
  268.     if(this.Limit){
  269.         thisthis._mxRight = this.mxRight - this._obj.offsetLeft;
  270.         thisthis._mxDown = this.mxBottom - this._obj.offsetTop;
  271.         thisthis._mxLeft = this.mxLeft + this.style_width + this._obj.offsetLeft;
  272.         thisthis._mxUp = this.mxTop + this.style_height + this._obj.offsetTop;
  273.     }
  274.     //mousemove时缩放 mouseup时停止
  275.     addEventHandler(document, "mousemove", this._fR);
  276.     addEventHandler(document, "mouseup", this._fS);
  277.     
  278.     //使鼠标移到窗口外也能释放
  279.     if(isIE){
  280.         addEventHandler(this._touch, "losecapture", this._fS);
  281.         this._touch.setCapture();
  282.     }else{
  283.         addEventHandler(window, "blur", this._fS);
  284.     }
  285.   },  
  286.   //缩放
  287.   Resize: function(e) {
  288.     //没有触发对象的话返回
  289.     if(!this._touch) return;
  290.     //清除选择(ie设置捕获后默认带这个)
  291.     window.getSelection && window.getSelection().removeAllRanges();
  292.     //执行缩放程序
  293.     this._fun(window.event || e);
  294.     //设置样式
  295.     //因为计算用的offset是把边框算进去的所以要减去
  296.     thisthis._obj.style.width = this.style_width - this._xBorder + "px";
  297.     thisthis._obj.style.height = this.style_height - this._yBorder + "px";
  298.     thisthis._obj.style.top = this.style_top + "px";
  299.     thisthis._obj.style.left = this.style_left + "px";  
  300.     //附加程序
  301.     this.onResize();
  302.   },
  303.   //普通缩放
  304.   //右边
  305.   SetRight: function(oEvent) {
  306.     //当前坐标位置减去左边的坐标等于当前宽度
  307.     this.repairRight(oEvent.clientX - this._left);
  308.   },
  309.   //下边
  310.   SetDown: function(oEvent) {
  311.     this.repairDown(oEvent.clientY - this._up);
  312.   },
  313.   //左边
  314.   SetLeft: function(oEvent) {
  315.     //右边的坐标减去当前坐标位置等于当前宽度
  316.     this.repairLeft(this._right - oEvent.clientX);
  317.   },
  318.   //上边
  319.   SetUp: function(oEvent) {
  320.     this.repairUp(this._down - oEvent.clientY);
  321.   },
  322.   //比例缩放
  323.   //比例缩放右下
  324.   scaleRightDown: function(oEvent) {
  325.     //先计算宽度然后按比例计算高度最后根据确定的高度计算宽度(宽度优先)
  326.     this.SetRight(oEvent);
  327.     this.repairDown(parseInt(this.style_width / this._scale));
  328.     this.repairRight(parseInt(this.style_height * this._scale));
  329.   },
  330.   //比例缩放左下
  331.   scaleLeftDown: function(oEvent) {
  332.     this.SetLeft(oEvent);
  333.     this.repairDown(parseInt(this.style_width / this._scale));
  334.     this.repairLeft(parseInt(this.style_height * this._scale));
  335.   },
  336.   //比例缩放右上
  337.   scaleRightUp: function(oEvent) {
  338.     this.SetRight(oEvent);
  339.     this.repairUp(parseInt(this.style_width / this._scale));
  340.     this.repairRight(parseInt(this.style_height * this._scale));
  341.   },
  342.   //比例缩放左上
  343.   scaleLeftUp: function(oEvent) {
  344.     this.SetLeft(oEvent);
  345.     this.repairUp(parseInt(this.style_width / this._scale));
  346.     this.repairLeft(parseInt(this.style_height * this._scale));
  347.   },
  348.   //这里是高度优先用于上下两边(体验更好)
  349.   //比例缩放下右
  350.   scaleDownRight: function(oEvent) {
  351.     this.SetDown(oEvent);
  352.     this.repairRight(parseInt(this.style_height * this._scale));
  353.     this.repairDown(parseInt(this.style_width / this._scale));
  354.   },
  355.   //比例缩放上右
  356.   scaleUpRight: function(oEvent) {
  357.     this.SetUp(oEvent);
  358.     this.repairRight(parseInt(this.style_height * this._scale));
  359.     this.repairUp(parseInt(this.style_width / this._scale));
  360.   },
  361.   //修正程序
  362.   //修正右边
  363.   repairRight: function(iWidth) {
  364.     //右边和下边只要设置宽度和高度就行
  365.     //当少于最少宽度
  366.     if (iWidth < this.MinWidth){ iWidth = this.MinWidth; }
  367.     //当超过当前设定的最大宽度
  368.     if(this.Limit && iWidth > this._mxRight){ iWidth = this._mxRight; }
  369.     //修改样式
  370.     this.style_width = iWidth;
  371.   },
  372.   //修正下边
  373.   repairDown: function(iHeight) {
  374.     if (iHeight < this.MinHeight){ iHeight = this.MinHeight; }
  375.     if(this.Limit && iHeight > this._mxDown){ iHeight = this._mxDown; }
  376.     this.style_height = iHeight;
  377.   },
  378.   //修正左边
  379.   repairLeft: function(iWidth) {
  380.     //左边和上边比较麻烦 因为还要计算left和top
  381.     //当少于最少宽度
  382.     if (iWidth < this.MinWidth){ iWidth = this.MinWidth; }
  383.     //当超过当前设定的最大宽度
  384.     else if(this.Limit && iWidth > this._mxLeft){ iWidth = this._mxLeft; }
  385.     //修改样式
  386.     this.style_width = iWidth;
  387.     thisthis.style_left = this._obj.offsetLeft + this._obj.offsetWidth - iWidth;
  388.   },
  389.   //修正上边
  390.   repairUp: function(iHeight) {
  391.     if(iHeight < this.MinHeight){ iHeight = this.MinHeight; }
  392.     else if(this.Limit && iHeight > this._mxUp){ iHeight = this._mxUp; }
  393.     this.style_height = iHeight;
  394.     thisthis.style_top = this._obj.offsetTop + this._obj.offsetHeight - iHeight;
  395.   },
  396.   //停止缩放
  397.   Stop: function() {
  398.     //移除事件
  399.     removeEventHandler(document, "mousemove", this._fR);
  400.     removeEventHandler(document, "mouseup", this._fS);
  401.     if(isIE){
  402.         removeEventHandler(this._touch, "losecapture", this._fS);
  403.         this._touch.releaseCapture();
  404.     }else{
  405.         removeEventHandler(window, "blur", this._fS);
  406.     }
  407.     this._touch = null;
  408.   }
  409. };
  410. //图片切割
  411. var ImgCropper = Class.create();
  412. ImgCropper.prototype = {
  413.   //容器对象,拖放缩放对象,图片地址,宽度,高度
  414.   initialize: function(container, drag, url, width, height, options) {
  415.     var oThis = this;
  416.     
  417.     //容器对象
  418.     this.Container = $(container);
  419.     this.Container.style.position = "relative";
  420.     this.Container.style.overflow = "hidden";
  421.     
  422.     //拖放对象
  423.     this.Drag = $(drag);
  424.     this.Drag.style.cursor = "move";
  425.     this.Drag.style.zIndex = 200;
  426.     if(isIE){
  427.         //设置overflow解决ie6的渲染问题(缩放时填充对象高度的问题)
  428.         this.Drag.style.overflow = "hidden";
  429.         //ie下用一个透明的层填充拖放对象 不填充的话onmousedown会失效(未知原因)
  430.         (function(style){
  431.             stylestyle.width = style.height = "100%"style.backgroundColor = "#fff"style.filter = "alpha(opacity:0)";
  432.         })(this.Drag.appendChild(document.createElement("div")).style)
  433.     }else{
  434.         //插入字符解决ff下捕获失效的问题
  435.         this.Drag.innerHTML += "<font size='1px'> </font>";
  436.     }
  437.     
  438.     thisthis._pic = this.Container.appendChild(document.createElement("img"));//图片对象
  439.     thisthis._cropper = this.Container.appendChild(document.createElement("img"));//切割对象
  440.     thisthis._pic.style.position = this._cropper.style.position = "absolute";
  441.     thisthis._pic.style.top = this._pic.style.left = this._cropper.style.top = this._cropper.style.left = "0";//对齐
  442.     this._cropper.style.zIndex = 100;
  443.     this._cropper.onload = function(){ oThis.SetPos(); }
  444.     
  445.     this.Url = url;//图片地址
  446.     this.Width = parseInt(width);//宽度
  447.     this.Height = parseInt(height);//高度
  448.     
  449.     this.SetOptions(options);
  450.     
  451.     this.Opacity = parseInt(this.options.Opacity);
  452.     this.dragTop = parseInt(this.options.dragTop);
  453.     this.dragLeft = parseInt(this.options.dragLeft);
  454.     this.dragWidth = parseInt(this.options.dragWidth);
  455.     this.dragHeight = parseInt(this.options.dragHeight);
  456.     
  457.     //设置预览对象
  458.     this.View = $(this.options.View) || null;//预览对象
  459.     this.viewWidth = parseInt(this.options.viewWidth);
  460.     this.viewHeight = parseInt(this.options.viewHeight);
  461.     this._view = null;//预览图片对象
  462.     if(this.View){
  463.         this.View.style.position = "relative";
  464.         this.View.style.overflow = "hidden";
  465.         thisthis._view = this.View.appendChild(document.createElement("img"));
  466.         this._view.style.position = "absolute";
  467.     }
  468.     
  469.     this.Scale = parseInt(this.options.Scale);
  470.     
  471.     //设置拖放
  472.     this._drag = new Drag(this.Drag, this.Drag, { Limit: true, onMove: function(){ oThis.SetPos(); } });
  473.     //设置缩放
  474.     thisthis._resize = this.GetResize();
  475.     
  476.     this.Init();
  477.   },
  478.   //设置默认属性
  479.   SetOptions: function(options) {
  480.     this.options = {//默认值
  481.         Opacity:    50,//透明度(0到100) 
  482.         //拖放位置和宽高
  483.         dragTop:    0,
  484.         dragLeft:   0,
  485.         dragWidth:  100,
  486.         dragHeight: 100,
  487.         //缩放触发对象
  488.         Right:      "",
  489.         Left:       "",
  490.         Up:         "",
  491.         Down:       "",
  492.         RightDown:  "",
  493.         LeftDown:   "",
  494.         RightUp:    "",
  495.         LeftUp:     "",
  496.         Scale:      false,//是否按比例缩放
  497.         //预览对象设置
  498.         View:   "",//预览对象
  499.         viewWidth:  100,//预览宽度
  500.         viewHeight: 100//预览高度
  501.     };
  502.     Object.extend(this.options, options || {});
  503.   },
  504.   //初始化对象
  505.   Init: function() {
  506.     var oThis = this;
  507.     
  508.     //设置容器
  509.     thisthis.Container.style.width = this.Width + "px";
  510.     thisthis.Container.style.height = this.Height + "px";
  511.     
  512.     //设置拖放对象
  513.     thisthis.Drag.style.top = this.dragTop + "px";
  514.     thisthis.Drag.style.left = this.dragLeft + "px";
  515.     thisthis.Drag.style.width = this.dragWidth + "px";
  516.     thisthis.Drag.style.height = this.dragHeight + "px";
  517.     
  518.     //设置切割对象
  519.     thisthis._pic.src = this._cropper.src = this.Url;
  520.     thisthis._pic.style.width = this._cropper.style.width = this.Width + "px";
  521.     thisthis._pic.style.height = this._cropper.style.height = this.Height + "px";
  522.     if(isIE){
  523.         this._pic.style.filter = "alpha(opacity:" + this.Opacity + ")";
  524.     } else {
  525.         thisthis._pic.style.opacity = this.Opacity / 100;
  526.     }
  527.     
  528.     //设置预览对象
  529.     if(this.View){ thisthis._view.src = this.Url; }
  530.     
  531.     //设置拖放
  532.     thisthis._drag.mxRight = this.Width; thisthis._drag.mxBottom = this.Height;
  533.     //设置缩放
  534.     if(this._resize){ thisthis._resize.mxRight = this.Width; thisthis._resize.mxBottom = this.Height; thisthis._resize.Scale = this.Scale; }
  535.   },
  536.   //设置获取缩放对象
  537.   GetResize: function() {
  538.     var op = this.options;
  539.     //有触发对象时才设置
  540.     if(op.RightDown || op.LeftDown || op.RightUp || op.LeftUp || op.Right || op.Left || op.Up || op.Down ){
  541.         var oThis = this_resize = new Resize(this.Drag, { Limit: true, onResize: function(){ oThis.SetPos(); } });
  542.         
  543.         //设置缩放触发对象
  544.         if(op.RightDown){ _resize.Set(op.RightDown, "right-down"); }
  545.         if(op.LeftDown){ _resize.Set(op.LeftDown, "left-down"); }
  546.         
  547.         if(op.RightUp){ _resize.Set(op.RightUp, "right-up"); }
  548.         if(op.LeftUp){ _resize.Set(op.LeftUp, "left-up"); }
  549.         
  550.         if(op.Right){ _resize.Set(op.Right, "right"); }
  551.         if(op.Left){ _resize.Set(op.Left, "left"); }
  552.         
  553.         if(op.Up){ _resize.Set(op.Up, "up"); }
  554.         if(op.Down){ _resize.Set(op.Down, "down"); }
  555.         
  556.         return _resize;
  557.     } else { return null; }
  558.   },  
  559.   //设置切割
  560.   SetPos: function() {
  561.     var o = this.Drag;
  562.     //按拖放对象的参数进行切割
  563.     this._cropper.style.clip = "rect(" + o.offsetTop + "px " + (o.offsetLeft + o.offsetWidth) + "px " + (o.offsetTop + o.offsetHeight) + "px " + o.offsetLeft + "px)";
  564.     //切割预览
  565.     if(this.View) this.PreView();
  566.   },  
  567.   //切割预览
  568.   PreView: function() {
  569.     //按比例设置宽度和高度
  570.     var o = this.Drag, h = this.viewWidth, w = h * o.offsetWidth / o.offsetHeight;
  571.     if(w > this.viewHeight){ w = this.viewHeight; h = w * o.offsetHeight / o.offsetWidth; }
  572.     //获取对应比例尺寸
  573.     var scale = h / o.offsetHeight, ph = this.Height * scale, pw = this.Width * scale, pt = o.offsetTop * scale, pl = o.offsetLeft * scale, styleView = this._view.style;
  574.     //设置样式
  575.     styleView.width = pw + "px"; styleView.height = ph + "px";
  576.     styleView.top = - pt + "px "; styleView.left = - pl + "px";
  577.     //切割预览图
  578.     styleView.clip = "rect(" + pt + "px " + (pl + w) + "px " + (pt + h) + "px " + pl + "px)";
  579.   }
  580. }
  581. var ic = new ImgCropper("bgDiv", "dragDiv", "1.jpg", 400, 500, {
  582.     dragTop: 50, dragLeft: 50,
  583.     Right: "rRight", Left: "rLeft", Up: "rUp", Down: "rDown",
  584.     RightDown: "rRightDown", LeftDown: "rLeftDown", RightUp: "rRightUp", LeftUp: "rLeftUp",
  585.     View: "viewDiv", viewWidth: 200, viewHeight: 200
  586. })
  587. </script>
  588. <script>
  589. //设置图片大小
  590. function Size(w, h){
  591.     ic.Width = w;
  592.     ic.Height = h;
  593.     ic.Init();
  594. }
  595. //换图片
  596. function Pic(url){
  597.     ic.Url = url;
  598.     ic.Init();
  599. }
  600. //设置透明度
  601. function Opacity(i){
  602.     iic.Opacity = i;
  603.     ic.Init();
  604. }
  605. //是否使用比例
  606. function Scale(b){
  607.     ic.Scale = b;
  608.     ic.Init();
  609. }
  610. </script>
  611. <br />
  612. <br />
  613. <div>
  614.   <input name="" type="button" value=" 增肥 " onclick="Size(500,400)" />
  615.   <input name="" type="button" value=" 还原 " onclick="Size(300,400)" />
  616. </div>
  617. <br />
  618. <div>
  619.   <input name="" type="button" value=" 换图 " onclick="Pic('http://images.cnblogs.com/cnblogs_com/cloudgamer/143727/r_min.jpg')" />
  620.   <input name="" type="button" value=" 还原 " onclick="Pic('https://i-blog.csdnimg.cn/blog_migrate/357ef0080a1a58473ebdcbc1e783e64c.jpeg')" />
  621. </div>
  622. <br />
  623. <div>
  624.   <input name="" type="button" value=" 透明 " onclick="Opacity(0)" />
  625.   <input name="" type="button" value=" 还原 " onclick="Opacity(50)" />
  626. </div>
  627. <br />
  628. <div>
  629.   <input name="" type="button" value="使用比例" onclick="Scale(true)" />
  630.   <input name="" type="button" value="取消比例" onclick="Scale(false)" />
  631. </div>
  632. </body>
  633. </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值