鼠标拖拉按比例缩放的JavaScript

  • 利用JS写的一个拖拉缩放框,可以设置按比例缩入、像PS软件一样的裁切区域,代码可要费的劲看哦,要不然看不懂的。

<!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>接触角测定仪</title>

</head>

<body>

<script>

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

var $ = function (id) {

return "string" == typeof id ? document.getElementById(id) : id;

};

var Class = {

create: function() {

return function() { this.initialize.apply(this, arguments); }

}

}

var Extend = function(destination, source) {

for (var property in source) {

destination[property] = source[property];

}

}

var Bind = function(object, fun) {

return function() {

return fun.apply(object, arguments);

}

}

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 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 Resize = Class.create();

Resize.prototype = {

initialize: function(obj, options) {

this._obj = $(obj);//缩放对象

this._styleWidth = this._styleHeight = this._styleLeft = this._styleTop = 0;//样式参数

this._sideRight = this._sideDown = this._sideLeft = this._sideUp = 0;//坐标参数

this._fixLeft = this._fixTop = 0;//定位参数

this._scaleLeft = this._scaleTop = 0;//定位坐标

this._mxSet = function(){};//范围设置

this._mxRightWidth = this._mxDownHeight = this._mxUpHeight = this._mxLeftWidth = 0;//范围参数

this._mxScaleWidth = this._mxScaleHeight = 0;//比例范围参数

this._fun = function(){};//缩放执行程序

//获取边框宽度

var _style = CurrentStyle(this._obj);

this._borderX = (parseInt(_style.borderLeftWidth) || 0) + (parseInt(_style.borderRightWidth) || 0);

this._borderY = (parseInt(_style.borderTopWidth) || 0) + (parseInt(_style.borderBottomWidth) || 0);

//事件对象(用于绑定移除事件)

this._fR = BindAsEventListener(this, this.Resize);

this._fS = Bind(this, this.Stop);

this.SetOptions(options);

//范围限制

this.Max = !!this.options.Max;

this._mxContainer = $(this.options.mxContainer) || null;

this.mxLeft = Math.round(this.options.mxLeft);

this.mxRight = Math.round(this.options.mxRight);

this.mxTop = Math.round(this.options.mxTop);

this.mxBottom = Math.round(this.options.mxBottom);

//宽高限制

this.Min = !!this.options.Min;

this.minWidth = Math.round(this.options.minWidth);

this.minHeight = Math.round(this.options.minHeight);

//按比例缩放

this.Scale = !!this.options.Scale;

this.Ratio = Math.max(this.options.Ratio, 0);

this.onResize = this.options.onResize;

this._obj.style.position = "absolute";

!this._mxContainer || CurrentStyle(this._mxContainer).position == "relative" || (this._mxContainer.style.position = "relative");

},

//设置默认属性

SetOptions: function(options) {

this.options = {//默认值

Max:  false,//是否设置范围限制(为true时下面mx参数有用)

mxContainer:"",//指定限制在容器内

mxLeft:  0,//左边限制

mxRight: 9999,//右边限制

mxTop:  0,//上边限制

mxBottom: 9999,//下边限制

Min:  false,//是否最小宽高限制(为true时下面min参数有用)

minWidth: 50,//最小宽度

minHeight: 50,//最小高度

Scale:  false,//是否按比例缩放

Ratio:  0,//缩放比例(宽/高)

onResize: function(){}//缩放时执行

};

Extend(this.options, options || {});

},

//设置触发对象

Set: function(resize, side) {

var resize = $(resize), fun;

if(!resize) return;

//根据方向设置

switch (side.toLowerCase()) {

case "up" :

fun = this.Up;

break;

case "down" :

fun = this.Down;

break;

case "left" :

fun = this.Left;

break;

case "right" :

fun = this.Right;

break;

case "left-up" :

fun = this.LeftUp;

break;

case "right-up" :

fun = this.RightUp;

break;

case "left-down" :

fun = this.LeftDown;

break;

case "right-down" :

default :

fun = this.RightDown;

};

//设置触发对象

addEventHandler(resize, "mousedown", BindAsEventListener(this, this.Start, fun));

},

//准备缩放

Start: function(e, fun, touch) {

//防止冒泡(跟拖放配合时设置)

e.stopPropagation ? e.stopPropagation() : (e.cancelBubble = true);

//设置执行程序

this._fun = fun;

//样式参数值

this._styleWidth = this._obj.clientWidth;

this._styleHeight = this._obj.clientHeight;

this._styleLeft = this._obj.offsetLeft;

this._styleTop = this._obj.offsetTop;

//四条边定位坐标

this._sideLeft = e.clientX - this._styleWidth;

this._sideRight = e.clientX + this._styleWidth;

this._sideUp = e.clientY - this._styleHeight;

this._sideDown = e.clientY + this._styleHeight;

//top和left定位参数

this._fixLeft = this._styleLeft + this._styleWidth;

this._fixTop = this._styleTop + this._styleHeight;

//缩放比例

if(this.Scale){

//设置比例

this.Ratio = Math.max(this.Ratio, 0) || this._styleWidth / this._styleHeight;

//left和top的定位坐标

this._scaleLeft = this._styleLeft + this._styleWidth / 2;

this._scaleTop = this._styleTop + this._styleHeight / 2;

};

//范围限制

if(this.Max){

//设置范围参数

var mxLeft = this.mxLeft, mxRight = this.mxRight, mxTop = this.mxTop, mxBottom = this.mxBottom;

//如果设置了容器,再修正范围参数

if(!!this._mxContainer){

mxLeft = Math.max(mxLeft, 0);

mxTop = Math.max(mxTop, 0);

mxRight = Math.min(mxRight, this._mxContainer.clientWidth);

mxBottom = Math.min(mxBottom, this._mxContainer.clientHeight);

};

//根据最小值再修正

mxRight = Math.max(mxRight, mxLeft + (this.Min ? this.minWidth : 0) + this._borderX);

mxBottom = Math.max(mxBottom, mxTop + (this.Min ? this.minHeight : 0) + this._borderY);

//由于转向时要重新设置所以写成function形式

this._mxSet = function(){

this._mxRightWidth = mxRight - this._styleLeft - this._borderX;

this._mxDownHeight = mxBottom - this._styleTop - this._borderY;

this._mxUpHeight = Math.max(this._fixTop - mxTop, this.Min ? this.minHeight : 0);

this._mxLeftWidth = Math.max(this._fixLeft - mxLeft, this.Min ? this.minWidth : 0);

};

this._mxSet();

//有缩放比例下的范围限制

if(this.Scale){

this._mxScaleWidth = Math.min(this._scaleLeft - mxLeft, mxRight - this._scaleLeft - this._borderX) * 2;

this._mxScaleHeight = Math.min(this._scaleTop - mxTop, mxBottom - this._scaleTop - this._borderY) * 2;

};

};

//mousemove时缩放 mouseup时停止

addEventHandler(document, "mousemove", this._fR);

addEventHandler(document, "mouseup", this._fS);

if(isIE){

addEventHandler(this._obj, "losecapture", this._fS);

this._obj.setCapture();

}else{

addEventHandler(window, "blur", this._fS);

e.preventDefault();

};

},

//缩放

Resize: function(e) {

//清除选择

window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();

//执行缩放程序

this._fun(e);

//设置样式,变量必须大于等于0否则ie出错

with(this._obj.style){

width = this._styleWidth + "px"; height = this._styleHeight + "px";

top = this._styleTop + "px"; left = this._styleLeft + "px";

}

//附加程序

this.onResize();

},

//缩放程序

//上

Up: function(e) {

this.RepairY(this._sideDown - e.clientY, this._mxUpHeight);

this.RepairTop();

this.TurnDown(this.Down);

},

//下

Down: function(e) {

this.RepairY(e.clientY - this._sideUp, this._mxDownHeight);

this.TurnUp(this.Up);

},

//右

Right: function(e) {

this.RepairX(e.clientX - this._sideLeft, this._mxRightWidth);

this.TurnLeft(this.Left);

},

//左

Left: function(e) {

this.RepairX(this._sideRight - e.clientX, this._mxLeftWidth);

this.RepairLeft();

this.TurnRight(this.Right);

},

//右下

RightDown: function(e) {

this.RepairAngle(

e.clientX - this._sideLeft, this._mxRightWidth,

e.clientY - this._sideUp, this._mxDownHeight

);

this.TurnLeft(this.LeftDown) || this.TurnUp(this.RightUp);

},

//右上

RightUp: function(e) {

this.RepairAngle(

e.clientX - this._sideLeft, this._mxRightWidth,

this._sideDown - e.clientY, this._mxUpHeight

);

this.RepairTop();

this.TurnLeft(this.LeftUp) || this.TurnDown(this.RightDown);

},

//左下

LeftDown: function(e) {

this.RepairAngle(

this._sideRight - e.clientX, this._mxLeftWidth,

e.clientY - this._sideUp, this._mxDownHeight

);

this.RepairLeft();

this.TurnRight(this.RightDown) || this.TurnUp(this.LeftUp);

},

//左上

LeftUp: function(e) {

this.RepairAngle(

this._sideRight - e.clientX, this._mxLeftWidth,

this._sideDown - e.clientY, this._mxUpHeight

);

this.RepairTop(); this.RepairLeft();

this.TurnRight(this.RightUp) || this.TurnDown(this.LeftDown);

},

//修正程序

//水平方向

RepairX: function(iWidth, mxWidth) {

iWidth = this.RepairWidth(iWidth, mxWidth);

if(this.Scale){

var iHeight = Math.round(iWidth / this.Ratio);

if(this.Max  &&  iHeight > this._mxScaleHeight){

iWidth = Math.round((iHeight = this._mxScaleHeight) * this.Ratio);

}else if(this.Min  &&  iHeight < this.minHeight){

var tWidth = Math.round(this.minHeight * this.Ratio);

if(tWidth < mxWidth){ iHeight = this.minHeight; iWidth = tWidth; }

}

this._styleHeight = iHeight;

this._styleTop = this._scaleTop - iHeight / 2;

}

this._styleWidth = iWidth;

},

//垂直方向

RepairY: function(iHeight, mxHeight) {

iHeight = this.RepairHeight(iHeight, mxHeight);

if(this.Scale){

var iWidth = Math.round(iHeight * this.Ratio);

if(this.Max  &&  iWidth > this._mxScaleWidth){

iHeight = Math.round((iWidth = this._mxScaleWidth) / this.Ratio);

}else if(this.Min  &&  iWidth < this.minWidth){

var tHeight = Math.round(this.minWidth / this.Ratio);

if(tHeight < mxHeight){ iWidth = this.minWidth; iHeight = tHeight; }

}

this._styleWidth = iWidth;

this._styleLeft = this._scaleLeft - iWidth / 2;

}

this._styleHeight = iHeight;

},

//对角方向

RepairAngle: function(iWidth, mxWidth, iHeight, mxHeight) {

iWidth = this.RepairWidth(iWidth, mxWidth);

if(this.Scale){

iHeight = Math.round(iWidth / this.Ratio);

if(this.Max  &&  iHeight > mxHeight){

iWidth = Math.round((iHeight = mxHeight) * this.Ratio);

}else if(this.Min  &&  iHeight < this.minHeight){

var tWidth = Math.round(this.minHeight * this.Ratio);

if(tWidth < mxWidth){ iHeight = this.minHeight; iWidth = tWidth; }

}

}else{

iHeight = this.RepairHeight(iHeight, mxHeight);

}

this._styleWidth = iWidth;

this._styleHeight = iHeight;

},

//top

RepairTop: function() {

this._styleTop = this._fixTop - this._styleHeight;

},

//left

RepairLeft: function() {

this._styleLeft = this._fixLeft - this._styleWidth;

},

//height

RepairHeight: function(iHeight, mxHeight) {

iHeight = Math.min(this.Max ? mxHeight : iHeight, iHeight);

iHeight = Math.max(this.Min ? this.minHeight : iHeight, iHeight, 0);

return iHeight;

},

//width

RepairWidth: function(iWidth, mxWidth) {

iWidth = Math.min(this.Max ? mxWidth : iWidth, iWidth);

iWidth = Math.max(this.Min ? this.minWidth : iWidth, iWidth, 0);

return iWidth;

},

//转向程序

//转右

TurnRight: function(fun) {

if(!(this.Min || this._styleWidth)){

this._fun = fun;

this._sideLeft = this._sideRight;

this.Max  &&  this._mxSet();

return true;

}

},

//转左

TurnLeft: function(fun) {

if(!(this.Min || this._styleWidth)){

this._fun = fun;

this._sideRight = this._sideLeft;

this._fixLeft = this._styleLeft;

this.Max  &&  this._mxSet();

return true;

}

},

//转上

TurnUp: function(fun) {

if(!(this.Min || this._styleHeight)){

this._fun = fun;

this._sideDown = this._sideUp;

this._fixTop = this._styleTop;

this.Max  &&  this._mxSet();

return true;

}

},

//转下

TurnDown: function(fun) {

if(!(this.Min || this._styleHeight)){

this._fun = fun;

this._sideUp = this._sideDown;

this.Max  &&  this._mxSet();

return true;

}

},

//停止缩放

Stop: function() {

removeEventHandler(document, "mousemove", this._fR);

removeEventHandler(document, "mouseup", this._fS);

if(isIE){

removeEventHandler(this._obj, "losecapture", this._fS);

this._obj.releaseCapture();

}else{

removeEventHandler(window, "blur", this._fS);

}

}

};

</script>

<script type="text/javascript" src="http://files.cnblogs.com/cloudgamer/Drag.js"></script>

<style type="text/css">

#rRightDown,#rLeftDown,#rLeftUp,#rRightUp,#rRight,#rLeft,#rUp,#rDown{

position:absolute;

background:#C00;

width:7px;

height:7px;

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;}

#rLeftDown{left:-4px;bottom:-4px;}

#rRightUp{right:-4px;top:-4px;}

#rRightDown{right:-4px;bottom:-4px;background-color:#00F;}

#rLeftUp{left:-4px;top:-4px;}

#rRight{right:-4px;top:50%;margin-top:-4px;}

#rLeft{left:-4px;top:50%;margin-top:-4px;}

#rUp{top:-4px;left:50%;margin-left:-4px;}

#rDown{bottom:-4px;left:50%;margin-left:-4px;}

#bgDiv{width:600px; height:300px; border:10px solid #666666; position:relative;}

#dragDiv{border:1px solid #000000; width:100px; height:60px; top:50px; left:50px; background:#fff;}

</style>

<div id="bgDiv">

<div id="dragDiv">

<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>

</div>

<div>

<input id="idScale" type="button" value="设置比例" />

<input id="idMin" type="button" value="设置最小范围" />

</div>

<script>

var rs = new Resize("dragDiv", { Max: true, mxContainer: "bgDiv" });

rs.Set("rRightDown", "right-down");

rs.Set("rLeftDown", "left-down");

rs.Set("rRightUp", "right-up");

rs.Set("rLeftUp", "left-up");

rs.Set("rRight", "right");

rs.Set("rLeft", "left");

rs.Set("rUp", "up");

rs.Set("rDown", "down");

$("idScale").onclick = function(){

if(rs.Scale){

this.value = "设置比例";

rs.Scale = false;

}else{

this.value = "取消比例";

rs.Scale = true;

}

}

$("idMin").onclick = function(){

if(rs.Min){

this.value = "设置最小范围";

rs.Min = false;

}else{

this.value = "取消最小范围";

rs.Min = true;

}

}

new Drag("dragDiv", { Limit: true, mxContainer: "bgDiv" });

</script>

</body>

</html>

鼠标拖拉按比例缩放的JavaScript

时间: 2024-11-15 23:33:15

鼠标拖拉按比例缩放的JavaScript的相关文章

javascript图片等比例缩放代码

javascript图片等比例缩放代码: 图片的尺寸在初始的状态下往往不能够完美的适应网页的布局,这个时候就需要对图片进行缩放处理,当然不能够是无规则的进行缩放,否则可能出现图片变形现象,下面是一段能够对图片进行等比例缩放的实例代码. 代码如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content=&q

前端学习代码实例-JavaScript 图片等比例缩放裁切详解

本文将通过代码实例详细介绍一下如何实现图片等比例缩放裁切效果. 图片有两种应用方式,一种作为子元素存在,一种是作为背景图片. 在每一种应用方式中,图片的等比例缩放又可以大致分为如下几种情况: (1).确保图片能够填充满元素,超出的部分被裁切或者隐藏. (2).确保图片的长或者宽填充满元素,超出的部分被裁切或者隐藏. 下面通过代码实例分别介绍一下上述列举的中可能,需要的朋友可以做一下参考. 一.作为背景图片: 通过CSS的background-image属性可以设置元素的背景图片效果. 下面就以如

jQuery实现等比例缩放大图片

在页面布局时,有时会遇到大图片将页面容器撑大,超出规定区域, 这时我们就需要将图片按比例缩放,让大图片自适应页面布局. 查看演示http://itmyhome.com/jquery_image_scaling/ 1.页面中有如下图片 <img alt="leaf" src="img/leaf.jpg"> 2.使用jQuery将图片缩放 <script type="text/javascript"> window.onloa

S实现控制图片显示大小的方法【图片等比例缩放功能】

S实现控制图片显示大小的方法[图片等比例缩放功能] [需求]:读取磁盘中的图片,展示在弹出框中,等比例缩放图片,使图片显示完全. (读取磁盘中的图片展示在前台,请参照我的另一篇文章:) [开发]: 调用说明: 直接调用js函数即可. 我测试是一个image 标签中直接调用,如下: <div> <img id="showImageimg"  src="/sirdifoa/applycorrection/getImage.do?imgName=2017001.j

JS等比例缩放图片,限定最大宽度和最大高度

JavaScript //图片按比例缩放 var flag=false; function DrawImage(ImgD,iwidth,iheight){ //参数(图片,允许的宽度,允许的高度) var image=new Image(); image.src=ImgD.src; if(image.width>0 && image.height>0){ flag=true; if(image.width/image.height>= iwidth/iheight){ i

css 布局之定位 相对/绝对/成比例缩放

给body添加 overflow: hidden; 可以将页面所有的 滚动条隐藏,但必须要给body 设置一个高度 overflow: hidden; height:864px; 父元素必须要设置 position:relative 必须设置 width 与 height 且不能用百分比 父层如果是图片,要使用  background: url(bg.jpg) no-repeat; 子元素必须设置 position: absolute; 必须设置 width 与 height 定位使用 top

理解CSS3中的background-size(对响应性图片等比例缩放)

2016-03-10 01:40 by 空智, 7463 阅读, 8 评论, 收藏, 编辑 阅读目录 background-size的基本属性 给图片设置固定的宽度和高度的 固定宽度400px和高度200px-使用background-size:400px 200px缩放设置 固定宽度400px和高度200px-使用background-size:400px;的缩放设置,那么第二个参数会自动转换为auto 固定宽度400px和高度200px-使用background-size:100% 100%

jQuery实现的图片等比例缩放效果

jQuery实现的图片等比例缩放效果:如果一个容器中放一个比容器还要大的图,那就可能就造成布局出现问题,就算是不容器大,有时候也看起来不够美观,这时候就要限制图片的尺寸,当然不能变形,否则就难看了,下面就介绍一下如何使用jQuery实现等比例缩放效果.代码如下: <div id="demo"> <img src="a.jpg" width="800" height="300" alt="图片&quo

用UISliader和仿射变换-实现等比例缩放

今天在网上搜了一下没找到用slider实现等比例缩放的例子 今天自己做了一个大家看下.图片放大缩小,slider往左滑动缩小,slider往右滑动放大 #import "ViewController.h" @interface ViewController () { UIImageView * _umgView; UISlider * _sl; } @end @implementation ViewController - (void)viewDidLoad { [super view