当前位置:编程学习 > JS >>

js图片等比例缩放

js图片等比例缩放

/使用onload=resizeImage(this,长,宽);
function resizeImage(img,w,h)
{
    var image=new Image();
    image.src = img.src;
    if(image.width > 0 && image.height > 0)
    {
        if(image.width / image.height >= w/h)
        {
            if(image.width > w)
            {
                img.width = w;
                img.height = (image.height * w) / image.width;
            }
            else
            {
                img.width = image.width;
                img.height = image.height;
            }
            //img.alt=image.width+"×"+image.height;
        }
        else
        {
            if(image.height > h)
            {
                img.height = h;
                img.width = (image.width * h) / image.height;
            }
            else
            {
                img.width = image.width;
                img.height = image.height;
            }
        }
    }
}

补充:网页制作,js教程 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,