图像的等比例缩放

<script type="text/javascript">
        $(function() {
            $("#originalWidth").val($("#img1").width());
            $("#originalHeight").val($("#img1").height());
            var originalWidth = $("#originalWidth").val();
            var originalHeight = $("#originalHeight").val();
            var desireWidth = $("#desireWidth").val();
            var desireHeight = $("#desireHeight").val();
            var scaledWidth = 0, scaledHeight = 0;
            var wRate = 1, hRate = 1, setRate = 1;
            $("#btn").click(function() {

                if (originalHeight > desireHeight) {
                    hRate = desireHeight / originalHeight;
                }
                if (originalWidth > desireWidth) {
                    wRate = desireWidth / originalWidth;
                }

                if (wRate != 1 || hRate != 1) {
                    if (wRate > hRate) {
                        setRate = hRate;
                    }
                    else {
                        setRate = wRate;
                    }
                }
                scaledWidth = originalWidth * setRate;
                scaledHeight = originalHeight * setRate;
                $("#scaledWidth").val(scaledWidth);
                $("#scaledHeight").val(scaledHeight);
                $("#img1").width(scaledWidth);
                $("#img1").height(scaledHeight);
            });

        });
    </script>

posted @ 2011-05-18 14:21  张占岭  阅读(542)  评论(0编辑  收藏  举报