国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

javascript圖片自動(dòng)縮放和垂直居中處理函數(shù)

2019-11-21 01:36:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
復(fù)制代碼 代碼如下:

<html>
<head>
<title>Untitled</title>
<style type="text/css">
.testCss{width:200px;height:300px;border:1px red solid;text-align:center;display:block;}
.testCss1{width:300px;height:300px;border:1px red solid;text-align:center;display:block;}
.testCss2{width:400px;height:300px;border:1px red solid;text-align:center;display:block;}
</style>
<script type="text/javascript">
function autoSizeImg(Contents,offsetWidth,offsetHeight,vlmiddle){
var o=Contents.getElementsByTagName("IMG");
var cwidth= window.getComputedStyle?window.getComputedStyle(Contents,null).width:Contents.currentStyle["width"];
var cheight=window.getComputedStyle?window.getComputedStyle(Contents,null).height:Contents.currentStyle["height"];
var ncwidth=parseInt(cwidth);
var ncheight=parseInt(cheight);
for(var i=0;i<o.length;i++){
var img=o[i];
var iw=img.width;
var ih=img.height;
if(img.width>ncwidth){
var nw=ncwidth-offsetWidth;
img.width=nw
img.height=(nw*ih)/iw;
}else if(img.width<ncwidth&&img.height>ncheight){
var nh=ncheight-offsetHeight;
img.height=nh;
img.width=(nh*iw)/ih;
}
if(img.height>ncheight&&img.width<ncwidth){
var hh=ncheight-offsetHeight;
img.height=nh;
img.width=(nh*iw)/ih;
}
if(vlmiddle)img.style.marginTop=((ncheight-img.height)/2)+"px";
}
}
</script>
</head>

<body>
<div class="testCss">
<img src="http://files.VeVB.COm/file_images/sucai/111131524.jpg" onload="autoSizeImg(this.parentNode,5,5,true)"/>
</div>
<div class="testCss1">
<img src="http://files.VeVB.COm/upload/20081010215957304.jpg" onload="autoSizeImg(this.parentNode,5,5,true)"/>
</div>
<div> </div>
<div class="testCss2">
<img src="http://files.VeVB.COm/upload/20081010215958874.gif" onload="autoSizeImg(this.parentNode,5,5,true)"/>
</div>
</body>
</html>

注釋:
1、后面的testCss1和testCss2兩個(gè)樣式跟testCss是一樣的。只是設(shè)置了大小。
2、紅色部分的height和width必須定義,是設(shè)置存放img外面的父層容器的高和寬。
3、text-algin:為圖片水平居中
4、display:block,必須設(shè)置。否則FF下啟用自動(dòng)縮放不起作用。

使用方法:
1、是需要在img的父層定義樣式,包含上述注釋里面提到的東西;
2、是img加上 


onload="autoSizeImg(this.parentNode,5,5,true)"

參數(shù)說(shuō)明:
參數(shù)Contents一般采用this.parentNode,即使img的父容易
參數(shù)offsetWidth,offsetHeight分別是寬和高度居中的修正值。
參數(shù)vlmiddle設(shè)置為true則是垂直居中

[Ctrl+A 全選 注:如需引入外部Js需刷新才能執(zhí)行]
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄梅县| 罗甸县| 民勤县| 兴宁市| 瓦房店市| 靖江市| 美姑县| 灵宝市| 灵璧县| 英吉沙县| 贺州市| 香港 | 和静县| 郁南县| 正镶白旗| 辽宁省| 东阿县| 平远县| 德州市| 高台县| 兴山县| 西乌珠穆沁旗| 东兰县| 长治县| 嘉峪关市| 沅江市| 高淳县| 肇庆市| 沙雅县| 灵石县| 襄樊市| 磐安县| 云浮市| 陆丰市| 三门县| 舟山市| 卫辉市| 夏河县| 铜川市| 凤台县| 丹寨县|