Script House integration article, welcome to reprint.
function controlImg(ele,w,h){
var c=ele.getElementsByTagName("img");
for(var i=0;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 '< ;/a>'
}
else{
c[i].title="Open image in new window";
c[i].onclick=function(e){window.open (this.src)}
}
}
}
}
window.onload=function(){
controlImg(document.getElementById("content"),670,980);
}
This kind of code was needed before, but because the specific idea was not very clear, I have compiled the article I saw in blueidea today.
Images in the specified area are generally used to control the content part. The content in controlImg(document.getElementById("content"),670,980); is used. The following is the test code.
]
The following is used The method of implementing css expression will increase the load on the client. It is recommended to use js's
. Suppose there is a div with an id of test. How to control whether the pictures inside it will be stretched?
Just define the CSS as follows:
The code is as follows:
#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);
}
<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='<a href="'+c[i].src+'" target="_blank" title="在新窗口打开图片">'+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> , the width of the picture will not exceed 600, the height will not exceed 450, and will be reduced according to the original proportion!