图片等比缩放

       //图片等比缩放
        function ImgZoom()
        {

            var w = 900;//设置最大宽度,也可根据img的外部容器 而动态获得,比如:$("#demo").width();
            $("img").each(function () {//如果有很多图片,使用each()遍历
                var img_w = $(this).width();//图片宽度
                var img_h = $(this).height();//图片高度
                if (img_w > w) {//如果图片宽度超出指定最大宽度
                    var height = (w * img_h) / img_w; //高度等比缩放
                    $(this).css({
                        "width": w, "height": height
                    });//设置缩放后的宽度和高度
                }
            });
        }

猜你喜欢

转载自www.cnblogs.com/zyx321/p/9145502.html