继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Jquery中压缩图像

html5零基础入门学习
关注TA
已关注
手记 246
粉丝 81
获赞 517

Jquery中压缩图像


<!DOCTYPE html>

<html>

<head>

<script src="/jquery/jquery-1.11.1.min.js">

</script>

<script>

function fixImage(img, w, h) {

    var newImg = new Image(); //获得图片的原始尺寸

    newImg.src = img.src;

    var lh;  //用于保存img.height,IE下隐藏的图片读取不到,需currentStyle解决

    if (newImg.width/newImg.height >= w/h) {

        if (newImg.width > w) {

            img.width = w;

            img.height = w * newImg.height / newImg.width;

            lh = window.ActiveXObject ? parseInt(img.currentStyle['height']) : img.height;

            img.style.marginTop = (h - lh)/2 + 'px';  //顺手垂直居中

        } else {

            img.width = newImg.width;

            img.height = newImg.height;

            lh = window.ActiveXObject ? parseInt(img.currentStyle['height']) : img.height;

            img.style.marginTop = (h - lh)/2 + 'px';  //顺手垂直居中

        }

    } else {

        if (newImg.height > h) {

            img.height = h;

            img.width = newImg.width * h / newImg.height;

        } else {

            img.width = newImg.width;

            img.height = newImg.height;

        }

    }

}

</script>

<body>


<p>

一幅图像:

<img src="/i/eg_mouse.jpg"  40, 40)" ;/>

</body>

</html>


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP