您的位置:首页 > 其它

图片自动缩放,前台读富文本编辑器里图片用的

2016-07-06 14:03 260 查看
//#region 图片自动缩放,前台读富文本编辑器里图片用的

/*

使用方法:

一、

<div class="divaaa">

@Html.Raw(ViewBag.Model.NewsContent)

</div>

二、

<script type="text/javascript">

window.onload = function () {

UnLoadImg($(".divaaa"));

}

</script>

*/

function UnLoadImg(obj) {

    $obj = $(obj);

    var $img = $obj.find("img")

    $img.each(function () {

        if ($(this).parents("a").length == 0) {

            $(this).css({ "cursor": "pointer" });

            $(this).click(function () {

                window.open($(this).attr("src"));

            });

        }

    });

    var maxWidth = $obj.width();

    for (var i = 0; i < $img.length; i++) {

        if ($img.eq(i).width() > maxWidth) {

            $img.eq(i).css({ width: maxWidth + "px", height: AutoResizeImage(maxWidth, 0, $img.eq(i)) + "px" });

        }

    }

}

function AutoResizeImage_H(maxWidth, maxHeight, objImg) {

    var img = objImg;

    var hRatio;

    var wRatio;

    var Ratio = 1;

    var w = img.width();

    var h = img.height();

    wRatio = maxWidth / w;

    hRatio = maxHeight / h;

    if (maxWidth == 0 && maxHeight == 0) {

        Ratio = 1;

    } else if (maxWidth == 0) {

        if (hRatio < 1) Ratio = hRatio;

    } else if (maxHeight == 0) {

        if (wRatio < 1) Ratio = wRatio;

    } else if (wRatio < 1 || hRatio < 1) {

        Ratio = (wRatio <= hRatio ? wRatio : hRatio);

    }

    if (Ratio < 1) {

        w = w * Ratio;

        h = h * Ratio;

    }

    return h;

}

//#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: