日期:2014-05-18 浏览次数:20533 次
div下图片自适应解决方法2
<img onload=""if(this.width>700) {this.resized=true; this.width=600;}
div下图片自适应解决方法3
转载aMiLiM'Blog
//图片自适应大小并绝对居中对齐
//函数 fImgageAuto
//能在同一个ID下对单独图片进行自适应外框大小
//Ver 2.1 最后更新07/08/27 by Amilim
function fImageAuto(nID,nMaxWidth,nMaxHeight)
{
var objParentID =document.getElementById(nID);
var objImg =objParentID.getElementsByTagName("img");
var nImgNewRate =0;
var nImgOldRate =nMaxWidth/nMaxHeight;
for (i=0;i<objImg.length;i++) {
nImgNewRate =objImg[i].offsetWidth/objImg[i].offsetHeight;
if (nImgNewRate >=nImgOldRate) {
objImg[i].style.height =nMaxWidth/nImgNewRate +"px";
objImg[i].style.width =nMaxWidth +"px";
objImg[i].style.marginTop =Math.round((nMaxHeight-nMaxWidth/nImgNewRate)/2) +"px";
------解决方案--------------------
。。。 更好的办法
是给 文章内容的上一级 加样式
#上一级div名字 img {
max-height:100px;
max-width:300px;
}