雲棲社區整合篇,歡迎轉載。
複製代碼 代碼如下:
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,並按原比例值縮小!