posts - 0, comments - 77, trackbacks - 0, articles - 356
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          兼容IE8、火狐的本地圖片預覽+等比例縮放

          Posted on 2011-06-16 18:13 semovy 閱讀(691) 評論(0)  編輯  收藏 所屬分類: JavaScriptCSS式樣
          轉自http://it.oyksoft.com/post/974/


          <!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=utf-8" />    
          <title>Firefox3,IE6,IE7,IE8上傳圖片預覽</title>    
          <style type="text/css">    
          #preview_wrapper{     
              display:inline-block;     
              width:300px;     
              height:300px;     
              background-color:#CCC;     
          }     
          #preview_fake{ /* 該對象用戶在IE下顯示預覽圖片 */     
              filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);     
          }     
          #preview_size_fake{ /* 該對象只用來在IE下獲得圖片的原始尺寸,無其它用途 */     
              filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);       
              visibility:hidden;     
          }     
          #preview{ /* 該對象用戶在FF下顯示預覽圖片 */     
              width:300px;     
              height:300px;     
          }     
          </style>    
              
          <script type="text/javascript">    
          function onUploadImgChange(sender){     
              if( !sender.value.match( /.jpg|.gif|.png|.bmp/i ) ){     
                  alert('圖片格式無效!');     
                  return false;     
              }     
                   
              var objPreview = document.getElementById( 'preview' );     
              var objPreviewFake = document.getElementById( 'preview_fake' );     
              var objPreviewSizeFake = document.getElementById( 'preview_size_fake' );     
                   
              if( sender.files &&  sender.files[0] ){     
                  objPreview.style.display = 'block';     
                  objPreview.style.width = 'auto';     
                  objPreview.style.height = 'auto';     
                       
                  // Firefox 因安全性問題已無法直接通過 input[file].value 獲取完整的文件路徑     
                  objPreview.src = sender.files[0].getAsDataURL();         
              }else if( objPreviewFake.filters ){      
                  // IE7,IE8 在設置本地圖片地址為 img.src 時出現莫名其妙的后果     
                  //(相同環境有時能顯示,有時不顯示),因此只能用濾鏡來解決     
                       
                  // IE7, IE8因安全性問題已無法直接通過 input[file].value 獲取完整的文件路徑     
                  sender.select();     
                  var imgSrc = document.selection.createRange().text;     
                       
                  objPreviewFake.filters.item(     
                      'DXImageTransform.Microsoft.AlphaImageLoader').src = imgSrc;     
                  objPreviewSizeFake.filters.item(     
                      'DXImageTransform.Microsoft.AlphaImageLoader').src = imgSrc;     
                       
                  autoSizePreview( objPreviewFake,      
                      objPreviewSizeFake.offsetWidth, objPreviewSizeFake.offsetHeight );     
                  objPreview.style.display = 'none';     
              }     
          }     
              
          function onPreviewLoad(sender){     
              autoSizePreview( sender, sender.offsetWidth, sender.offsetHeight );     
          }     
              
          function autoSizePreview( objPre, originalWidth, originalHeight ){     
              var zoomParam = clacImgZoomParam( 300, 300, originalWidth, originalHeight );     
              objPre.style.width = zoomParam.width + 'px';     
              objPre.style.height = zoomParam.height + 'px';     
              objPre.style.marginTop = zoomParam.top + 'px';     
              objPre.style.marginLeft = zoomParam.left + 'px';     
          }     
              
          function clacImgZoomParam( maxWidth, maxHeight, width, height ){     
              var param = { width:width, height:height, top:0, left:0 };     
                   
              if( width>maxWidth || height>maxHeight ){     
                  rateWidth = width / maxWidth;     
                  rateHeight = height / maxHeight;     
                       
                  if( rateWidth > rateHeight ){     
                      param.width =  maxWidth;     
                      param.height = height / rateWidth;     
                  }else{     
                      param.width = width / rateHeight;     
                      param.height = maxHeight;     
                  }     
              }     
                   
              param.left = (maxWidth - param.width) / 2;     
              param.top = (maxHeight - param.height) / 2;     
                   
              return param;     
          }     
          </script>    
              
          </head>    
              
          <body>    
              <div id="preview_wrapper">    
                  <div id="preview_fake">    
                      <img id="preview" onload="onPreviewLoad(this)"/>    
                  </div>    
              </div>    
              <br/>    
              <input id="upload_img" type="file" onchange="onUploadImgChange(this)"/>    
              <br/>    
              <img id="preview_size_fake"/>    
          </body>    
          </html>
          主站蜘蛛池模板: 垫江县| 丰台区| 绵竹市| 白玉县| 蓬安县| 玉门市| 常州市| 虹口区| 黔西县| 衡东县| 镇坪县| 琼结县| 中山市| 江永县| 两当县| 万源市| 余姚市| 天津市| 彭山县| 望江县| 博客| 南澳县| 天长市| 铜梁县| 卢氏县| 登封市| 廊坊市| 新宁县| 西乌| 彭泽县| 苍溪县| 铜川市| 民丰县| 和顺县| 北票市| 高淳县| 安平县| 古交市| 沙湾县| 临洮县| 珠海市|