Home > Web Front-end > JS Tutorial > body text

JavaScript scales images proportionally to control images that are out of range_javascript skills

WBOY
Release: 2016-05-16 17:26:29
Original
1199 people have browsed it

js scales images proportionally. This function is very practical. When a web page loads a relatively large image, it will often deform the web page and make the page ugly. So we thought of using JS to control the image beyond a certain range. Picture, Script House stabilizes the page layout. This code snippet completes this function, and the code is very concise and the effect is very good.

Copy code The code is as follows:

Scale the image proportionally<script>function <br>DrawImage(ImgD,iwidth,iheight){ //Parameters (image, allowed width, allowed height) <br>var <br>image=new Image(); image.src=ImgD.src; <br>if(image.width>0 <br>&& image.height>0){ if(image.width/image.height>= <br>iwidth/iheight ){ if(image.width>iwidth){ <br>ImgD.width=iwidth; <br>ImgD.height=(image.height*iwidth)/image.width; <br>}else{ <br>ImgD. width=image.width; <br>ImgD.height=image.height; <br>} <br>}else{ <br>if(image.height>iheight){ <br>ImgD.height=iheight; <br>ImgD.width=(image.width*iheight)/image.height; <br>}else{ <br>ImgD.width=image.width; <br>ImgD.height=image.height; <br>} <br>} <br>} <br>} <br></script>
src=http://www.jb51.net/uploadfile /2013/0803/20130803034531502.jpg"
alt="The effect after automatic scaling"

width="100"

height="100"

onload ="javascript:DrawImage(this,80,80)"

/>
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template