JS enables proportional scaling of webpage image thumbnails

Source: Internet
Author: User

When processing the thumbnail of a webpage image, it is difficult to ensure the uniform size of the image. Setting the image size directly causes the image to be stretched and blurred, the code described in this article can automatically adjust the image size proportionally after the image is loaded.

 

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "imageautozoom. aspx. cs" inherits = "ui_imageautozoom" %>

 

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<HTML xmlns = "http://www.w3.org/1999/xhtml">

<Head runat = "server">

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> JS scales webpage images proportionally-by wangtao </title>

<SCRIPT type = "text/JavaScript" src = "../MISC/JS/CORE/jquery. js"> </SCRIPT>

</Head>

<Body>

<SCRIPT type = "text/JavaScript">

Function imageautozoom (IMG, fitwidth, fitheight)

{

VaR image = new image ();

Image. src = IMG. SRC;

If (image. width> 0 & image. Height> 0)

{

If (image. width/image. Height> = fitwidth/fitheight)

{

If (image. width> fitwidth)

{

IMG. width = fitwidth;

IMG. Height = (image. Height * fitwidth)/image. width;

}

Else if (image. Hight> fitheight)

{

IMG. Height = fitheight;

IMG. width = (image. Width * fitheight)/image. height;

}

Else

{

IMG. width = fitwidth;

IMG. Height = fitheight;

}

}

}

}

</SCRIPT>

 

<Form ID = "imageautozoomform" runat = "server">

<Div>

</Div>

</Form>

</Body>

</Html>

When the image is too large, the page will be pulled, so it is best to set the height and width to reduce the pull range. Although this is not very technical, it is very practical. Most websites use images more or less.

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.