相關(guān)關(guān)鍵詞
關(guān)于我們
最新文章
- 拉動懸浮于頂部的JS控制代碼
- 在JavaScript中構(gòu)建ArrayList示例代碼
- js使用for循環(huán)及if語句判斷多個一樣的name
- JavaScript中判斷原生函數(shù)檢查function是否是原生代碼
- jQuery CSS()方法改變現(xiàn)有的CSS樣式表
- JavaScript中判斷原生函數(shù)檢查function是否是原生代碼
- jQuery動畫高級用法(上)——詳解animation中的.queue()函數(shù)
- python小技巧之批量抓取美女圖片
- JS中offsetTop、clientTop、scrollTop、offsetTop各屬性介紹
- JS獲取瀏覽器窗口大小 獲取屏幕,瀏覽器,網(wǎng)頁高度寬度
JS圖片壓縮–圖片后加載后按比例縮放
原理:圖片加載完后把圖片的尺寸固定在一個固定的范圍之內(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>