Automatically scale down the js Code of images in the specified area (to prevent the page from being broken by images)

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Div id = article> </div>
<Script type = "text/javascript">
// Scale the image to a proper size
Function ResizeImages ()
{
Var myimg, oldwidth, oldheight;
Var maxwidth = 550;
Var maxheight = 880
Var imgs = document. getElementById ('Article'). getElementsByTagName ('img '); // if your defined id is not article, modify it here

For (I = 0; I Myimg = imgs [I];

If (myimg. width> myimg. height)
{
If (myimg. width> maxwidth)
{
Oldwidth = myimg. width;
Myimg. height = myimg. height * (maxwidth/oldwidth );
Myimg. width = maxwidth;
}
} Else {
If (myimg. height> maxheight)
{
Oldheight = myimg. height;
Myimg. width = myimg. width * (maxheight/oldheight );
Myimg. height = maxheight;
}
}
}
}
// Scale the image to a proper size
ResizeImages ();
</Script>


This means to control the image size in the specified area, otherwise some advertisement images will be distorted.

The picture control code used by the script house:

Copy codeThe Code is as follows:
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 | w0> = 600 ){
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 =" view image in new window ">' + c [I]. outerHTML + '</a>'
}
Else {
C [I]. title = "opening an image in a new window ";
C [I]. onclick = function (e) {window. open (this. src )}
}
}
}
}

Ele is the specified area. w is the maximum width. If it is greater than this, it will be reduced. H is the maximum height.

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.