Home >Web Front-end >JS Tutorial >js code that automatically scales down images in a specified area (to prevent the page from being broken by images)_javascript skills

js code that automatically scales down images in a specified area (to prevent the page from being broken by images)_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:58:521182browse

Copy code The code is as follows:





It means to control the size of the image in the designated area, or some larger advertisements The picture will also be distorted.

Picture control code used by Script House:

Copy code Code As follows:

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||w0>=600){
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="Open image in new window";
                                                                                                                                                                   }



ele is the specified area, w is the maximum width, if it is larger than this, it will be reduced. h is the maximum height.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn