JavaScript 圖片切割效果(放大鏡)
更新時(shí)間:2008年12月10日 17:25:39 作者:
自上一個(gè)版本的圖片切割效果出來后,雖然也經(jīng)??吹健翱蚣荛_發(fā)這個(gè)如何如何容易”之類的評(píng)論,但也受到很多人的肯定,小弟在此感謝大家的支持。
這里貼出切割效果部分的代碼:
復(fù)制代碼 代碼如下:
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 ImgCropper = Class.create();
ImgCropper.prototype = {
//容器對象,控制層,圖片地址
initialize: function(container, handle, url, options) {
this._Container = $(container);//容器對象
this._layHandle = $(handle);//控制層
this.Url = url;//圖片地址
this._layBase = this._Container.appendChild(document.createElement("img"));//底層
this._layCropper = this._Container.appendChild(document.createElement("img"));//切割層
this._layCropper.onload = Bind(this, this.SetPos);
//用來設(shè)置大小
this._tempImg = document.createElement("img");
this._tempImg.onload = Bind(this, this.SetSize);
this.SetOptions(options);
this.Opacity = Math.round(this.options.Opacity);
this.Color = this.options.Color;
this.Scale = !!this.options.Scale;
this.Ratio = Math.max(this.options.Ratio, 0);
this.Width = Math.round(this.options.Width);
this.Height = Math.round(this.options.Height);
//設(shè)置預(yù)覽對象
var oPreview = $(this.options.Preview);//預(yù)覽對象
if(oPreview){
oPreview.style.position = "relative";
oPreview.style.overflow = "hidden";
this.viewWidth = Math.round(this.options.viewWidth);
this.viewHeight = Math.round(this.options.viewHeight);
//預(yù)覽圖片對象
this._view = oPreview.appendChild(document.createElement("img"));
this._view.style.position = "absolute";
this._view.onload = Bind(this, this.SetPreview);
}
//設(shè)置拖放
this._drag = new Drag(this._layHandle, { Limit: true, onMove: Bind(this, this.SetPos), Transparent: true });
//設(shè)置縮放
this.Resize = !!this.options.Resize;
if(this.Resize){
var op = this.options, _resize = new Resize(this._layHandle, { Max: true, onResize: Bind(this, this.SetPos) });
//設(shè)置縮放觸發(fā)對象
op.RightDown && (_resize.Set(op.RightDown, "right-down"));
op.LeftDown && (_resize.Set(op.LeftDown, "left-down"));
op.RightUp && (_resize.Set(op.RightUp, "right-up"));
op.LeftUp && (_resize.Set(op.LeftUp, "left-up"));
op.Right && (_resize.Set(op.Right, "right"));
op.Left && (_resize.Set(op.Left, "left"));
op.Down && (_resize.Set(op.Down, "down"));
op.Up && (_resize.Set(op.Up, "up"));
//最小范圍限制
this.Min = !!this.options.Min;
this.minWidth = Math.round(this.options.minWidth);
this.minHeight = Math.round(this.options.minHeight);
//設(shè)置縮放對象
this._resize = _resize;
}
//設(shè)置樣式
this._Container.style.position = "relative";
this._Container.style.overflow = "hidden";
this._layHandle.style.zIndex = 200;
this._layCropper.style.zIndex = 100;
this._layBase.style.position = this._layCropper.style.position = "absolute";
this._layBase.style.top = this._layBase.style.left = this._layCropper.style.top = this._layCropper.style.left = 0;//對齊
//初始化設(shè)置
this.Init();
},
//設(shè)置默認(rèn)屬性
SetOptions: function(options) {
this.options = {//默認(rèn)值
Opacity: 50,//透明度(0到100)
Color: "",//背景色
Width: 0,//圖片高度
Height: 0,//圖片高度
//縮放觸發(fā)對象
Resize: false,//是否設(shè)置縮放
Right: "",//右邊縮放對象
Left: "",//左邊縮放對象
Up: "",//上邊縮放對象
Down: "",//下邊縮放對象
RightDown: "",//右下縮放對象
LeftDown: "",//左下縮放對象
RightUp: "",//右上縮放對象
LeftUp: "",//左上縮放對象
Min: false,//是否最小寬高限制(為true時(shí)下面min參數(shù)有用)
minWidth: 50,//最小寬度
minHeight: 50,//最小高度
Scale: false,//是否按比例縮放
Ratio: 0,//縮放比例(寬/高)
//預(yù)覽對象設(shè)置
Preview: "",//預(yù)覽對象
viewWidth: 0,//預(yù)覽寬度
viewHeight: 0//預(yù)覽高度
};
Extend(this.options, options || {});
},
//初始化對象
Init: function() {
//設(shè)置背景色
this.Color && (this._Container.style.backgroundColor = this.Color);
//設(shè)置圖片
this._tempImg.src = this._layBase.src = this._layCropper.src = this.Url;
//設(shè)置透明
if(isIE){
this._layBase.style.filter = "alpha(opacity:" + this.Opacity + ")";
} else {
this._layBase.style.opacity = this.Opacity / 100;
}
//設(shè)置預(yù)覽對象
this._view && (this._view.src = this.Url);
//設(shè)置縮放
if(this.Resize){
with(this._resize){
Scale = this.Scale; Ratio = this.Ratio; Min = this.Min; minWidth = this.minWidth; minHeight = this.minHeight;
}
}
},
//設(shè)置切割樣式
SetPos: function() {
var p = this.GetPos();
//按拖放對象的參數(shù)進(jìn)行切割
this._layCropper.style.clip = "rect(" + p.Top + "px " + (p.Left + p.Width) + "px " + (p.Top + p.Height) + "px " + p.Left + "px)";
//設(shè)置預(yù)覽
this.SetPreview();
},
//設(shè)置預(yù)覽效果
SetPreview: function() {
if(this._view){
//預(yù)覽顯示的寬和高
var p = this.GetPos(), s = this.GetSize(p.Width, p.Height, this.viewWidth, this.viewHeight), scale = s.Height / p.Height;
//按比例設(shè)置參數(shù)
var pHeight = this._layBase.height * scale, pWidth = this._layBase.width * scale, pTop = p.Top * scale, pLeft = p.Left * scale;
//設(shè)置預(yù)覽對象
with(this._view.style){
//設(shè)置樣式
width = pWidth + "px"; height = pHeight + "px"; top = - pTop + "px "; left = - pLeft + "px";
//切割預(yù)覽圖
clip = "rect(" + pTop + "px " + (pLeft + s.Width) + "px " + (pTop + s.Height) + "px " + pLeft + "px)";
}
}
},
//設(shè)置圖片大小
SetSize: function() {
var s = this.GetSize(this._tempImg.width, this._tempImg.height, this.Width, this.Height);
//設(shè)置底圖和切割圖
this._layBase.style.width = this._layCropper.style.width = s.Width + "px";
this._layBase.style.height = this._layCropper.style.height = s.Height + "px";
//設(shè)置拖放范圍
this._drag.mxRight = s.Width; this._drag.mxBottom = s.Height;
//設(shè)置縮放范圍
if(this.Resize){ this._resize.mxRight = s.Width; this._resize.mxBottom = s.Height; }
},
//獲取當(dāng)前樣式
GetPos: function() {
with(this._layHandle){
return { Top: offsetTop, Left: offsetLeft, Width: offsetWidth, Height: offsetHeight }
}
},
//獲取尺寸
GetSize: function(nowWidth, nowHeight, fixWidth, fixHeight) {
var iWidth = nowWidth, iHeight = nowHeight, scale = iWidth / iHeight;
//按比例設(shè)置
if(fixHeight){ iWidth = (iHeight = fixHeight) * scale; }
if(fixWidth && (!fixHeight || iWidth > fixWidth)){ iHeight = (iWidth = fixWidth) / scale; }
//返回尺寸對象
return { Width: iWidth, Height: iHeight }
}
}
相關(guān)文章
JavaScript判斷圖片是否能夠加載,失敗則替換默認(rèn)圖片
JavaScript智能判斷圖片是否能夠正確加載,若加載失敗則用默認(rèn)圖片替換,這是個(gè)比較實(shí)用的功能,不少網(wǎng)站都可見到這種功能.2010-10-10JS+FLASH幻燈片播放圖片腳本,整理了代碼,使得調(diào)用更加方便!
JS+FLASH幻燈片播放圖片腳本,整理了代碼,使得調(diào)用更加方便!...2007-01-01javascript圖片自動(dòng)縮放和垂直居中處理函數(shù)
非常不錯(cuò)的應(yīng)用代碼,方便我們處理一些圖片效果2008-10-10符合web標(biāo)準(zhǔn)的連續(xù)滾動(dòng)圖像的js代碼
符合web標(biāo)準(zhǔn)的連續(xù)滾動(dòng)圖像的js代碼...2007-02-02感應(yīng)鼠標(biāo)的圖片遮罩動(dòng)畫效果
感應(yīng)鼠標(biāo)的圖片遮罩動(dòng)畫效果,鼠標(biāo)放上后會(huì)動(dòng)畫顯示文字提示,有意思哦,有興趣的朋友運(yùn)行一下看效果。2010-10-10