Picture Perfect Scaling _javascript tips

Source: Internet
Author: User
In the company's work is often to customers for product display of the page, because the customer upload the picture format size, zoom will lead to deformation, so in Sunday, a bit of time, write a JS code, supporting the perfect zoom picture.
First add a <div></div> label to the picture, the IMG can not define height or width, as follows:
<div class= "Product_img_div" ></div> Write code in CSS:. product_img_div {Width:210px;height:190px;overflow:hidden}
The effect is to control the image load, the overflow part hidden, so that the interface will not be too ugly.
Copy Code code as follows:

Resizeimg ("Product_img", 200,180);
function Resizeimg (cname,w,h) {
var Reimgs = document.getelementsbytagname ("img");
for (j=0;j<reimgs.length;j++) {
if (reimgs[j].classname==cname && (reimgs[j].height>h | | reimgs[j].width>w)) {
if (reimgs[j].height==reimgs[j].width) {
Reimgs[j].height=h;reimgs[j].width=w;
else if (reimgs[j].height>reimgs[j].width) {
Reimgs[j].height=h;
else if (reimgs[j].height<reimgs[j].width) {
Reimgs[j].width=w;
}
}
}
}

After the test, the picture size is scaled perfectly, and it solves the problem that the interface will be hard to see when loading.

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.