javascript 指定地區內圖片等比例縮放實現代碼 指令碼之家整合版[原創]_圖象特效

來源:互聯網
上載者:User
雲棲社區整合篇,歡迎轉載。
複製代碼 代碼如下:

function controlImg(ele,w,h){
var c=ele.getElementsByTagName("img");
for(var i=0;i<c.length;i++){
var w0=c[i].clientWidth,h0=c[i].clientHeight;
var t1=w0/w,t2=h0/h;
if(t1>1||t2>1){
c[i].width=Math.floor(w0/(t1>t2?t1:t2));
c[i].height=Math.floor(h0/(t1>t2?t1:t2));
if(document.all){
c[i].outerHTML='<a href="'+c[i].src+'" target="_blank" title="在新視窗開啟圖片">'+c[i].outerHTML+'</a>'
}
else{
c[i].title="在新視窗開啟圖片";
c[i].onclick=function(e){window.open(this.src)}
}
}
}
}
window.onload=function(){
controlImg(document.getElementById("content"),670,980);
}

以前就需要這樣的代碼,但因為具體的思路不是和很清楚,今天在blueidea看到的文章,特整理下。
指定地區內的,一般用於控制內容部分的圖片,可通過controlImg(document.getElementById("content"),670,980); 中的content,下面是測試代碼。
<script> //============================== function controlImg(ele,w,h){ var c=ele.getElementsByTagName("img"); for(var i=0;i<c.length;i++){ var w0=c[i].clientWidth,h0=c[i].clientHeight; var t1=w0/w,t2=h0/h; if(t1>1||t2>1){ c[i].width=Math.floor(w0/(t1>t2?t1:t2)); c[i].height=Math.floor(h0/(t1>t2?t1:t2)); if(document.all){ c[i].outerHTML=''+c[i].outerHTML+'' } else{ c[i].title="在新視窗開啟圖片"; c[i].onclick=function(e){window.open(this.src)} } } } } window.onload=function(){ controlImg(document.getElementById("dxy"),300,300); } </script> <body> 下面是要控制的地區。 <div id="dxy"> 原圖 <center></center>  教程圖層:   一 開啟原圖 複製一層   二 執行圖層<建立調整圖層<色相/飽和度 參數不講解   三 執行圖層<建立調整圖層<曲線   四 去色 然後降低不透明度   五 執行圖層<建立調整圖層<亮度/對比 <center></center> </div> 這下面是不需要控制的地區,大家可以參考下。 原圖 <center></center>  教程圖層:   一 開啟原圖 複製一層   二 執行圖層<建立調整圖層<色相/飽和度 參數不講解   三 執行圖層<建立調整圖層<曲線   四 去色 然後降低不透明度   五 執行圖層<建立調整圖層<亮度/對比 <center></center> </body>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]


下面是用css expression實現的方法會增加用戶端的負荷,建議用js的
假設有一個id為test的div,如何控制其內的圖片不會撐呢?
  如下定義CSS即可:
複製代碼 代碼如下:

  #test IMG{
  border:0;
  margin:0;
  padding:0;
  max-width:600px;
  width:expression(this.width>600?"600px":this.width);
  max-height:450px;
  height:expression(this.height>450?"450px":this.height);
  }

  如此定義後,其中的圖片寬就不會超過600,高不超過450,並按原比例值縮小!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.