通过JS脚本控制图片等比缩放

假设原始图片的高宽为:248px*636px

Html代码:

<div id="container" style='text-align: center; vertical-align: middle;margin-right: auto;margin-left: auto;'>
        <img width='100%' height='100%' src='resources/images/jisu-header.png' />

</div>

 

JS代码:

function () {
        var maxH = 248, maxW = 636;//原始图片高宽

        //设置图片的高度和宽度,根据图片比例设置高度
        var container=document.getElementById("container");

        if (Ext.os.deviceType == "Phone") {
            var width = document.body.clientWidth;//获取屏幕宽度
            var height = (maxH / maxW) * width;//根据宽度计算高度
            if (width < 640) {
                container.style.height=height+'px';
            } else if (width >= 640) {

                container.style.height=maxH+'px';

                container.style.width=maxW+'px';

            }
        }
        else {
                container.style.height=maxH+'px';

                container.style.width=maxW+'px';

        }

    }

 

转载于:https://my.oschina.net/caimingkai/blog/351391

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值