Javascript Image Auto scaling and vertical center processing functions

Source: Internet
Author: User

CopyCode The Code is as follows: <HTML>
<Head>
<Title> untitled </title>
<Style type = "text/CSS">
. Testcss {width: 200px; Height: 300px; Border: 1px red solid; text-align: center; display: block ;}
. Testcss1 {width: 300px; Height: 300px; Border: 1px red solid; text-align: center; display: block ;}
. Testcss2 {width: 400px; Height: 300px; Border: 1px red solid; text-align: center; display: block ;}
</Style>
<SCRIPT type = "text/JavaScript">
Function autosizeimg (contents, offsetwidth, offsetheight, vlmiddle ){
VaR o = contents. getelementsbytagname ("IMG ");
VaR cwidth = Window. getcomputedstyle? Window. getcomputedstyle (contents, null). Width: contents. currentstyle ["width"];
VaR cheight = Window. getcomputedstyle? Window. getcomputedstyle (contents, null). Height: contents. currentstyle ["height"];
VaR ncwidth = parseint (cwidth );
VaR ncheight = parseint (cheight );
For (VAR I = 0; I <O. length; I ++ ){
VaR IMG = O [I];
VaR IW = IMG. width;
VaR ih = IMG. height;
If (IMG. width> ncwidth ){
VaR nw = ncwidth-offsetwidth;
IMG. width = NW
IMG. Height = (NW * IH)/IW;
} Else if (IMG. width <ncwidth & IMG. Height> ncheight ){
VaR nH = ncheight-offsetheight;
IMG. Height = nH;
IMG. width = (NH * IW)/iH;
}
If (IMG. Height> ncheight & IMG. width <ncwidth ){
VaR HH = ncheight-offsetheight;
IMG. Height = nH;
IMG. width = (NH * IW)/iH;
}
If (vlmiddle) IMG. style. margintop = (ncheight-img.height)/2) + "PX ";
}
}
</SCRIPT>
</Head>

<Body>
<Div class = "testcss">

</Div>
<Div class = "testcss1">

</Div>
<Div> </div>
<Div class = "testcss2">

</Div>
</Body>
</Html>

Note:
1. The following testcss1 and testcss2 styles are the same as testcss. Only the size is set.
2. the height and width of the red part must be defined, which is the height and width of the parent layer container that stores the IMG.
3. Text-algin: horizontally centered
4. display: block, which must be set. Otherwise, enabling auto scaling under FF does not work.

Usage:
1. Define the style at the parent layer of IMG, including the things mentioned in the above annotations;
2. IMG and

Onload = "autosizeimg (this. parentnode, 5, 5, true )"

parameter description:
the contents parameter generally uses this. parentnode, even if the IMG parent is easy
the parameter offsetwidth and offsetheight are the correction values of the center width and height respectively.
If vlmiddle is set to true, the center is vertical. <br/> <style type = "text/CSS">. testcss {width: 200px; Height: 300px; Border: 1px red solid; text-align: center; display: block ;}. testcss1 {width: 300px; Height: 300px; Border: 1px red solid; text-align: center; display: block ;}. testcss2 {width: 400px; Height: 300px; Border: 1px red solid; text-align: center; display: block ;}</style> <p> (this. parentnode, 5, 5, true) "> <p>

Related Article

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.