---
title: "图片大小自适应"
author: "netxdiy"
date: "2007-06-11T23:41:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2012-09-27T11:21:33Z"
id: 1686735
url: "https://netxdiy.iteye.com/blog/1686735"
---

&lt;body&gt;  
&lt;imgid="achome"src="http://image2.sina.com.cn/ent/y/2006-10-09/U1819P28T3D1276435F326DT20061009152013.jpg"/&gt;  
&lt;/body&gt;

&lt;script&gt;  
//取得img元素  
varimageArr=document.getElementById("achome");

alert("图片原始大小为550x734");

//图形的宽/高比  
imageRate=parseInt(imageArr.style.width)/parseInt(imageArr.style.Height);

//  
if(imageArr.offsetWidth&gt;200)  
{  
alert("现在使图片自适应大小...");  
imageArr.style.width="200px";  
imageArr.style.height=200/imageRate+"px";  
}  
&lt;/script&gt;