人人人妻人人人妻人人人,99精品国产综合久久久久五月天 ,欧美白人最猛性XXXXX,日韩AV无码免费播放

News新聞

業(yè)界新聞動(dòng)態(tài)、技術(shù)前沿
Who are we?

您的位置:首頁(yè)      JS/JQ/AJAX      JS圖片壓縮–圖片后加載后按比例縮放

JS圖片壓縮–圖片后加載后按比例縮放

標(biāo)簽: 發(fā)布日期:2014-05-10 00:00:00 570

原理:圖片加載完后把圖片的尺寸固定在一個(gè)固定的范圍之內(nèi)。。

JS Code:

<script type="text/javascript">

var PRoMaxHeight=100;
var proMaxWidth=100;


function ImgAuto(ImgD)
{
var image=new Image();
image.src=ImgD.src;
image.onload = function(){
if(image.width>0&&image.height>0)
{
var rate=(proMaxWidth/image.width<proMaxHeight/image.height)?proMaxWidth/image.width:proMaxHeight/image.height;
if(rate<=1)
{
ImgD.width=image.width*rate;
ImgD.height=image.height*rate
}
else
{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
};
image.onload();
};

</script>

HTML Code:

<body>
<img src="http://s3.knowsky.com/l/45001-55000/200952916491584421395.jpg" onload="ImgAuto(this)" id="test"/>
<script type="text/Javascript" src="img.js"></script>

</body>