国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js與自動伸縮圖片 自動縮小圖片的多瀏覽器兼容的方法總結 原創

2019-11-21 02:12:46
字體:
來源:轉載
供稿:網友
最近做一個圖片的自動縮小效果,發現一直用的js,竟然在firefox下無法正常啊,導致頁面變形.所以自己寫了個兼容性一般的代碼,大家可以來討論下
原來我用的是從pjblog上的
復制代碼 代碼如下:

//查找網頁內寬度太大的圖片進行縮放以及PNG糾正
 function ReImgSize(){
  for (i=0;i<document.images.length;i++)
   {
   if (document.all){
    if (document.images[i].width>550)
     {
       document.images[i].width="550"  //沒有高,明顯會讓圖片變形
       try{
           document.images[i].outerHTML='<a href="'+document.images[i].src+'" target="_blank" title="在新窗口打開圖片">'+document.images[i].outerHTML+'</a>'
           }catch(e){}
       }
   }
  else{
    if (document.images[i].width>400) {
//寬和高都沒有,更是讓firefox下圖片撐大圖片
      document.images[i].title="在新窗口打開圖片"
      document.images[i].style.cursor="pointer"
      document.images[i].onclick=function(e){window.open(this.src)}
    }
  }
  }
 }

非常好用的代碼可不足的地方就是firefox下大圖會變形,而且無法控制區域的圖片,如果想要的大圖,也被變成小圖了
我自己寫了個,
復制代碼 代碼如下:

function $(objectId) { 
     if(document.getElementById && document.getElementById(objectId)) { 
    // W3C DOM 
       return document.getElementById(objectId); 
     }  
     else if (document.all && document.all(objectId)) { 
    // MSIE 4 DOM 
       return document.all(objectId); 
     }  
     else if (document.layers && document.layers[objectId]) { 
    // NN 4 DOM.. note: this won't find nested layers 
       return document.layers[objectId]; 
     }  
     else { 
       return false; 
    } 
}
function dxy_ReImgSize(){
var box=$("dxy_content");
var imgall=box.getElementsByTagName("img")
  for (i=0;i<imgall.length;i++)
   {
    if (imgall[i].width>500)
     {
    var oWidth=imgall[i].width;
    var oHeight=imgall[i].height;
    imgall[i].width="500";
    imgall[i].height=oHeight*500/oWidth;
       }
    }
}

可又發現,如果低瀏覽器,不支持getElementsByTagName,就沒的玩了,好處是可以控制區域.
最后沒辦法了,就先弄個,暫時的解決辦法
復制代碼 代碼如下:

function ReImgSize(){
  for (i=0;i<document.images.length;i++)
   {
   if (document.all){
    if (document.images[i].width>500)
     {
       var oWidth=document.images[i].width;
       var oHeight=document.images[i].height;
       document.images[i].width="500";
       document.images[i].height=oHeight*500/oWidth;
       document.images[i].outerHTML='<a href="'+document.images[i].src+'" target="_blank" title="在新窗口打開圖片">'+document.images[i].outerHTML+'</a>'
       }
   }
  else{
    if (document.images[i].width>500) {
       var oWidth=document.images[i].width;
       var oHeight=document.images[i].height;
       document.images[i].width="500";
       document.images[i].height=oHeight*500/oWidth;
      document.images[i].title="在新窗口打開圖片";
      document.images[i].style.cursor="pointer"
      document.images[i].onclick=function(e){window.open(this.src)}
    }
  }
  }
 }

注意我增加了
復制代碼 代碼如下:

var oWidth=document.images[i].width; 
       var oHeight=document.images[i].height; 
       document.images[i].width="500"; 
       document.images[i].height=oHeight*500/oWidth; 

如果大家發現了什么更好的方法,貼出來啊
www.survivalescaperooms.com 武林網 原創,轉載請寫明出處
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 循化| 石河子市| 忻州市| 肃宁县| 苏州市| 西昌市| 龙口市| 陵川县| 青川县| 玉龙| 广宁县| 南郑县| 兰考县| 邵东县| 黄石市| 绿春县| 且末县| 信阳市| 大安市| 宁强县| 庐江县| 阜平县| 武平县| 汕头市| 平罗县| 利津县| 迭部县| 乐都县| 拉孜县| 泸定县| 文登市| 四川省| 德格县| 田东县| 资兴市| 根河市| 仙游县| 望城县| 金湖县| 驻马店市| 商水县|