JavaScript dynamically changes page picture size

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags ajax browsers change function image javascript problems set

One of the more common problems when updating articles on your Web site is that the illustrations are too wide to make the entire page distorted. It would be too much trouble if you scaled and inserted each illustration first.

I wrote a period of time before the article encountered this kind of thing, and later with CSS overflow and max-width properties temporarily solve the problem of page deformation. The benefits of this approach are simple, but the downside is the effect of destroying certain details.

such as Overflow:hidden, meaning that when the inner element is wider than the parent frame, the part that is outside the width is hidden. Doing so may be a sudden truncation of some content, being hidden, and very sorry for the audience.

Using the Max-width property to limit the maximum width of an article illustration, you need to consider the compatibility of each browser. IE6 is not supported by this attribute, in my impression, some browsers support this attribute, but the picture is not equal to zoom (like Safari and opera, not remember), this is not fair to the users of these browsers.

So I finally chose to dynamically change the size of the picture through JavaScript. This approach is good for a variety of browsers (it should be very rare for people to disable JavaScript right now?) , if you change the theme, you can also flexibly change the maximum size of the article illustrations.

There are two scenarios, because the theme I'm using is that the jquery library is loaded, so you can do this with the following code:

the following references:


$ (document). Ready (function () {


$ ('. Post img '). each (function () {


var maxwidth = 100; Picture Max width


var maxheight = 100; Picture Max height


var ratio = 0; Scaling


var width = $ (this). width (); Picture actual width


var height = $ (this). Height (); Picture actual height


 


//Check whether the picture is very wide


if (Width > maxwidth) {


ratio = Maxwidth/width; Calculate Scaling


$ (this). CSS ("width", maxwidth); Set the actual display width


height = height * ratio; Calculate the height of proportional scaling


$ (this). CSS ("height", height * ratio); Set the height
after proportional scaling

}


 


//Check whether the picture is very high


if (height > maxheight) {


ratio = Maxheight/height; Calculate Scaling


$ (this). CSS ("height", maxheight); Set the actual display height


width = width * ratio; Calculate the height of proportional scaling


$ (this). CSS ("width", width * ratio); Set the height
after proportional scaling

}


});


});

If you do not want to load the jquery library, you can use the following code:

the following references:


function Resizeimage (image, maximum width of illustration, maximum height of illustration)


{


if (image.classname = = "Thumbnail")


{


w = image.width;


h = image.height;





if (w = 0 | | h = = 0)


{


image.width = maxwidth;


image.height = maxheight;


}


else if (w > H)


{


if (W > maxwidth) image.width = maxwidth;


}


Else


{


if (H > MaxHeight) image.height = MaxHeight;


}


 


image.classname = "Scaledthumbnail";


}


}

Using pure JavaScript, trouble points, need to manually add class= to the picture "Thumbnail", but the final effect is the same.

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.