Home > Web Front-end > JS Tutorial > javascript implementation code for proportional scaling of images in a specified area Script House Integrated Version [Original]_Image Special Effects

javascript implementation code for proportional scaling of images in a specified area Script House Integrated Version [Original]_Image Special Effects

WBOY
Release: 2016-05-16 18:43:33
Original
1276 people have browsed it

Script House integration article, welcome to reprint.

Copy code The code is as follows:

function controlImg(ele,w,h){
var c=ele.getElementsByTagName("img");
for(var i=0;ivar 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.

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]


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!
Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template