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

来源:互联网 发布:人工智能电影精彩影评 编辑:程序博客网 时间:2024/06/05 01:58
//#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
0 0
原创粉丝点击