The specified area of the picture automatically scaled the JS code (to prevent the page is the picture burst) _javascript Tips

Source: Internet
Author: User

Copy Code code as follows:

<div id=article></div" >
<script type= "Text/javascript" >
Scale pictures to fit size
function Resizeimages ()
{
var myimg,oldwidth,oldheight;
var maxwidth=550;
var maxheight=880
var IMGs = document.getElementById (' article '). getElementsByTagName (' img '); If you have defined an ID that is not article, please modify it here

for (i=0;imyimg = 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 pictures to fit size
Resizeimages ();
</script>



It means to control the size of the picture in the specified area, or some large advertising picture will be distorted.

picture control code for cloud-dwelling communities:

Copy Code code 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&GT;T2?T1:T2));
C[i].height=math.floor (h0/(T1&GT;T2?T1:T2));
if (document.all) {
C[i].outerhtml= ' <a href= ' +c[i].src+ ' target= ' _blank ' title= ' view pictures in new Window ' > ' +c[i].outerhtml+ ' </a> '
}
else{
c[i].title= "Open Picture in new Window";
C[i].onclick=function (e) {window.open (THIS.SRC)}
}
}
}
}

The ele is the specified area, W is the largest width, and larger than this will shrink. H is the maximum height.

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.