http://www.aygfsteel.com/ebecket 返還網
          隨筆-140  評論-11  文章-131  trackbacks-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 等等(...)


          地址打包下載 :放大鏡

          posted on 2010-03-31 09:23 becket_zheng 閱讀(324) 評論(0)  編輯  收藏 所屬分類: C#
          主站蜘蛛池模板: 南木林县| 西贡区| 洪湖市| 横峰县| 三明市| 什邡市| 隆昌县| 汕头市| 闵行区| 富平县| 三明市| 杭州市| 巧家县| 随州市| 靖远县| 晋城| 铁岭县| 连城县| 奉化市| 阿坝| 都江堰市| 温宿县| 马尔康县| 内乡县| 电白县| 古田县| 昌都县| 大足县| 锡林浩特市| 宁强县| 申扎县| 罗源县| 江门市| 沙雅县| 晋州市| 晋中市| 崇文区| 利辛县| 河南省| 普定县| 五华县|