A little bit of JS code for a scaled miniature picture

Source: Internet
Author: User
Tags return
JS wrote a very simple picture of the miniature JS code, of course, there are many similar code on the Internet, this is indeed a shortcoming.
The Main method is written in Setimgsize.js setimgsize.js
1//Smart Miniature Image JS method
2//Parameter: Imgid (ID of the picture)
3//Parameter: MaxWidth (the maximum width of the picture, the value of 0 indicates unrestricted width)
4//Parameter: MaxHeight (the maximum height of the picture, the value of 0 means unlimited height)
5 function setimgsize (imgid,maxwidth,maxheight)
6 {
7 var img = Document.images[imgid];
8 if (MaxWidth < 1)
9 {
Ten if (Img.height > MaxHeight)
11 {
Img.height = MaxHeight;
return true;
if (MaxHeight < 1)
17 {
if (Img.width > MaxWidth)
19 {
Img.width = MaxWidth;
return true;
if (Img.height > MaxHeight | | img.width > MaxWidth)
25 {
if ((img.height/maxheight) > (img.width/maxwidth))
27 {
Img.height = MaxHeight;
Or else
31 {
Img.width = MaxWidth;
return true;
36} Functional implementation principle is in the picture after loading (onload event) with JS to achieve a miniature.
The following is a test file test.htm test.htm
1 <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
2 3 4 <title> Test </title>
5 <script language= "JavaScript" type= "Text/javascript" src= "Setimgsize.js" ></script>
6 7 <body>
8 limits the width of 150px
9 <br/><br/> Limited height of 150px
One <br/><br/> Limits the height of 150px, width 150px
SOURCE Download Http://www.cnblogs.com/Files/aiqingayu/SetImgSize.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.