⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imgcropper.htm

📁 一些javascript效果 很值得学习
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!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=gb2312" />
<title>JavaScript 图片切割效果(带拖放、缩放效果) </title>
</head>
<body>

<style type="text/css">
#rRightDown,#rLeftDown,#rLeftUp,#rRightUp,#rRight,#rLeft,#rUp,#rDown{position:absolute;background:#F00;width:5px; height:5px; z-index:500; font-size:0;}
#dragDiv{border:1px solid #000000;}
</style>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="50%"><div id="bgDiv" style="width:400px; height:500px;">
        <div id="dragDiv">
          <div id="rRightDown" style="right:0; bottom:0;"> </div>
          <div id="rLeftDown" style="left:0; bottom:0;"> </div>
          <div id="rRightUp" style="right:0; top:0;"> </div>
          <div id="rLeftUp" style="left:0; top:0;"> </div>
          <div id="rRight" style="right:0; top:50%;"> </div>
          <div id="rLeft" style="left:0; top:50%;"> </div>
          <div id="rUp" style="top:0; left:50%;"> </div>
          <div id="rDown" style="bottom:0;left:50%;"></div>
        </div>
      </div></td>
    <td><div id="viewDiv" style="width:200px; height:200px;"> </div></td>
  </tr>
</table>
<script>

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";
	}
	//设置触发对象
	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);
  },

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -