HTML5网页设计

javaScript

Javascript等比例缩小图片

时间:2015-12-29 16:50来自:未知作者:user点击:
等比例缩小图片 function changeImg(obj,width,height) { var img = new Image(); img.src = document.getElementById(obj.id).src var ys_w = img.width; var ys_h = img.height; if(ys_w width || ys_h height) { var scale; var scale1 = ys_w / width; v

等比例缩小图片

function changeImg(obj,width,height) { 
    var img = new Image();
    img.src = document.getElementById(obj.id).src
    var ys_w = img.width;
    var ys_h = img.height;       
   
    if(ys_w > width || ys_h > height)       
    {
        var scale;
        var scale1 = ys_w / width;
        var scale2 = ys_h / height;  
        //alert(scale1+","+scale2);
        if(scale1 > scale2)
        {
            scale = scale1;
        }
        else
        {
            scale = scale2;
        }
       document.getElementById(obj.id).style.width = ys_w / scale;
       document.getElementById(obj.id).style.height = ys_h / scale;  
    }
}

当前文章地址:http://www.shejicool.com/web/javaScript/636.html

阅完本篇文章?您还可以: 收藏本文 文章挑错 朋友推荐