javascript 放大鏡 v1.0 基於Yui2 實現的放大鏡效果

來源:互聯網
上載者:User


v1.0實現功能
1 放大倍數設定
2 透明度設定
3 反轉特效
4 放大圖片層的大小自訂
5 滑鼠層的大小自訂
6 ie6下select遮蓋問題
7 游標樣式自訂
8 zIndex設定
簡單初始化方法舉例 複製代碼 代碼如下:new flower.init("Demo","mag");
new flower.init("Demo1","mag1",{
max:3,zoomType:false,zoomWidth:200,zoomHeight:200,iframe:true,zIndex:666,cursor:"row-resize"
});

代碼講解 複製代碼 代碼如下:defaultConfig={
/**
* 放大鏡的倍數
* @type Number
*/
max:3,
/**
* *放大鏡滑鼠移動層的透明度
* @type Number
*/
opacity:0.5,
/**顯示效果 false為預設,true為反色效果
* @type Boolean
*/
zoomType:false,
/**顯示動畫
* @type String
*/
showEffect:'fadein',
/**放大層的寬度
* @type Number
*/
zoomWidth:'auto',
/**放大層的高度
* @type Number
*/
zoomHeight:'auto',
/**滑鼠層的寬度
* @type Number
*/
tipsWidth:'auto',
/**滑鼠層的高度
* @type Number
*/
tipsHeight:'auto',
/**iframe遮蓋select
* @type Boolean
*/
iframe:false,
/**iframe zIndex
* @type Number
*/
zIndex:999,
/**游標樣式
* @type String
*/
cursor:"auto"
};

組件預設的參數配置,包括放大倍數,寬度,高度,透明度等的設定
2 定義屬性 複製代碼 代碼如下:namespace.init=function(content,mag,config){
/**
* 原始圖片容器
* @type HTMLElement
*/
this.content=D.get(content);
/**
* 放大圖片容器
* @type HTMLElement
*/
this.mag=D.get(mag);
/**
* 原始圖片
* @type HTMLElement
*/
this.imgsource=this.content.getElementsByTagName("img")[0];
/**
* 放大圖片
* @type HTMLElement
*/
this.img=this.mag.getElementsByTagName("img")[0];
/**
* 滑鼠layer
* @type HTMLElement
*/
this.tips=this.content.getElementsByTagName("div")[0];
/**
* 配置參數
* @type this.tipsect
*/
this.config=L.merge(defaultConfig,config||{});
/*初始化*/
this._init();
};

init函數接受三個實參 原圖的容器id,和放大後的圖片容器id和配置參數 (裝firebug的同學可以看下代碼結構)
this.config=L.merge(defaultConfig,config||{});
這句話是後面的對象的屬性覆蓋前面的對象的屬性,並返回
如 this.config=L.merge({"a":"aa"},{"a":"bb"});
此時的this.config.a == "bb"
config||{}
如果config不存在,則返回空的對象自變數
原型初始化方法
代碼 複製代碼 代碼如下:_init:function(){
var self=this;
/*賦值src給大圖*/
this.img.src=this.imgsource.src;
/*get邊框長度*/
this.borderwidth=this.imgsource.offsetWidth - this.imgsource.clientWidth;
/**
* 設定大圖片的寬度和高度 (X倍數)
* 設定大圖容器的寬高和位置
* 設定滑鼠跟隨層的寬高和透明度
*/
this.pi=(this.config.zoomWidth!='auto'?this.config.zoomWidth/this.imgsource.offsetWidth:1)
this.pi2=(this.config.zoomHeight!='auto'?this.config.zoomHeight/this.imgsource.offsetHeight:1)
this._css(this.img,{
'position':'absolute',
'width':(this.config.zoomWidth!='auto' ?this.imgsource.offsetWidth*this.config.max*this.pi:this.imgsource.offsetWidth*this.config.max)+"px",
'height':(this.config.zoomHeight!='auto' ?this.imgsource.offsetHeight*this.config.max*this.pi2:this.imgsource.offsetHeight*this.config.max)+"px"
})._css(this.mag,{
'width':(this.config.zoomWidth!='auto' ? this.config.zoomWidth:this.imgsource.offsetWidth)+"px",
'height':(this.config.zoomHeight!='auto'?this.config.zoomHeight:this.imgsource.offsetHeight)+"px",
'left':D.getX(this.content)+this.imgsource.offsetWidth+10+"px",
'top':this.content.offsetTop+"px",
'position' : 'absolute',
"zIndex":this.config.zIndex
})._css(this.tips,{
'display':'',
'width':(this.config.tipsWidth!='auto' ? this.config.tipsWidth: parseInt(this.imgsource.offsetWidth / this.config.max)- this.borderwidth)+"px",
'height' : (this.config.tipsHeight!='auto' ? this.config.tipsHeight: parseInt(this.imgsource.offsetHeight / this.config.max) - this.borderwidth )+ 'px',
'opacity' : this.config.opacity
})
E.on(this.content,'mousemove',function(e){
self._css(self.mag,{"display":"block"})._css(self.tips,{"display":"block"})._move(e,self.tips)
})
E.on(this.content,'mouseout',function(e){
self._css(self.tips,{"display":"none"})._css(self.mag,{"display":"none"});
})
!!this.config.zoomType && E.on(self.tips,'mouseout',function(e){
self._css(self.imgsource,{"opacity":1});
self.tips.getElementsByTagName("img")[0] && self.tips.removeChild(self.tips.getElementsByTagName("img")[0]);
})
if(ie6 && !!this.config.iframe){
this._createIframe(this.mag);
}
D.setStyle(this.content,"cursor",this.config.cursor);
},

組件的初始化原代碼
預設滑鼠跟隨的層和大圖是隱藏的
1.把圖片的連結賦值給將要放大顯示的圖片。
2. 如有自訂zoomWidth或zoomHeight大小的時候,設定 this.pi 寬比 和this.pi2 高比 (為與實際圖片大小間的比值)
3.設定大圖的寬度和高度
4. 設定大圖容器的寬高和位置
5.設定滑鼠層的位置和寬高和透明度
6 給原圖容器增加mousemove事件
7. 給原圖容器增加mouseout事件
8 反色特效後,還原透明度,並刪除用來實現效果的 Dom (在滑鼠層結構內用appendChild一個img元素)
9 ie6 建立iframe 用來遮擋的select。(預設情況下在無iframe的時候,ie6會被select擋住,無法用zIndex來修正 )
10 設定游標樣式
style設定的方法 複製代碼 代碼如下:_css:function(el,json){
for(var s in json){
D.setStyle(el,s,json[s]);
}
return this;
},

Yui有提供自己的 設定Dom樣式的方法 D.setStyle(dom,style屬性名稱,屬性的值);
用 for (var s in json) 來遍曆 json對象的所有屬性
return this; 常用的鏈式調用寫法 // this._css(/**/)._css(/**/) ._css(/**/) ……
核心mousemove事件代碼 複製代碼 代碼如下:_move:function(e,tips){
var point=E.getXY(e);
/**
* 提示層位置
* 大圖顯示位置
*/
this._css(tips,{
'top' : Math.min(Math.max(point[1] - this.content.offsetTop-parseInt(tips.offsetHeight)/2 ,0),this.content.offsetHeight - tips.offsetHeight) + 'px',
'left' : Math.min(Math.max(point[0] - this.content.offsetLeft-parseInt(tips.offsetWidth)/2 ,0),this.content.offsetWidth - tips.offsetWidth) + 'px'
})._css(this.img,{
'top':-(parseInt(tips.style.top) * this.config.max *this.pi2) + 'px',
'left' : - (parseInt(tips.style.left) * this.config.max *this.pi) + 'px'
});
/**
* 反色效果
*/
if(!!this.config.zoomType){
if(!tips.getElementsByTagName("img").length){
var imgs=document.createElement("img");
imgs.id='temp';
imgs.src=this.imgsource.src;
this._css(imgs,{
'width':this.imgsource.offsetWidth+"px",
'height':this.imgsource.offsetHeight+"px",
'position':'absolute'
});
tips.appendChild(imgs);
this.imgs=imgs;
}
this._css(this.imgsource,{
"opacity":0.2
})._css(this.tips,{
"opacity":1,
"visibility":"visible"
})._css(D.get("temp"),{
'top':-(parseInt(tips.style.top))+"px",
'left':-(parseInt(tips.style.left))+"px"
})
}
},

提示層位置的移動 滑鼠位置X軸 - this.offsetLeft - 滑鼠框寬度/2
並用Math.max和Math.min,不讓滑鼠框超出tuxiang
大圖位置的移動=小圖的位置 X 放大倍數 X 寬比(預設為1)
反色效果是在jquery的一個外掛程式上看到的 沒有看他的代碼 看了下他dom結構 應該和我這種實現方式是一樣的
設定原圖的透明度為0.2 這樣就變灰色了 然後設定滑鼠層透明為1,也就是不透明.層內是一個圖片 和 imgsource的地址是一樣的
這圖片的父元素position也是absolute,所以我們要即時設定top和left值來定位滑鼠層的圖片
建立iframe 複製代碼 代碼如下:_createIframe:function(el){
var layer = document.createElement('iframe');
layer.tabIndex = '-1';
layer.src = 'javascript:false;';
el.appendChild(layer);
this._css(layer,{
"width":(this.config.zoomWidth!='auto' ? this.config.zoomWidth:this.imgsource.offsetWidth)+"px",
"height":(this.config.zoomHeight!='auto'?this.config.zoomHeight:this.imgsource.offsetHeight)+"px",
"zIndex":this.config.zIndex
})
}

iframe元素的寬高和zIndex的設定,配置參數設定iframe:true並在ie6下 才會建立,在其他瀏覽器下設定true也不會建立,因為沒有必要
代碼改進中
1 增加特效的外掛程式機制
2 最佳化設定寬高值運算式的代碼 感覺太長太臃腫
3 增加圖片預載
4 增加回呼函數介面
5 增加className,讓使用者可自訂
6 等等(...)
地址打包下載 :放大鏡

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.