用js 让图片在 div或dl里 居中,底部对齐


解决图片长\宽大于容器,一边长一边宽,小于容器
在ff,ie里都通过
这里是js代码:

复制代码 代码如下:

<HTML>
<HEAD>
<TITLE> phpStudy www.phpstudy.net New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<style type="text/css">
*{margin:0px; padding:0px; font-size:12px; }
body{ font-size:0.8em; color: #333;}
ul{ list-style:none}
.clear:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;}
img{border:0px;}
.clear1{ clear: both; width: 100%; height: 1px; overflow: hidden;}
a{ text-decoration: none; color:#404040 }
body{ background:#f7f3d0 url(../images/bg.jpg) repeat-x left 210px;}
/**/
#pic{border:1px solid #000; width:800px; height:auto!important;height:300px; min-height:300px;background:#fff}
#pic dl{width:215px;height:200px; overflow: hidden;border:1px solid #000;margin:10px;float:left}
#pic dt{height:161px; width:209px;overflow:hidden;border:1px solid red;text-align:center}

</style>
<script language="javascript" src="tt.js"></script>
<script language="javascript">
<!--
imgsrc={
vh:'161', //高
vw:'209', //宽

addEvent: function(elm, evType, fn, useCapture){
if (elm.addEventListener){
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent) {
var r = elm.attachEvent('on' + evType, fn);
return r;
} else {
elm['on' + evType] = fn;
}
},
init:function()
{
var divn=document.getElementById("pic")
var dln=divn.getElementsByTagName("dl")
for (var i=0;i<dln.length ;i++) {
var dtn=dln[i].getElementsByTagName("dt");
var imgn=dtn[0].getElementsByTagName("img");


if(imgn[0].height>imgsrc.vh||imgn[0].width>imgsrc.vw)
{
if(imgn[0].height/imgsrc.vh > imgn[0].width/imgsrc.vw)
{
imgn[0].heigh=imgsrc.vh
imgn[0].width=imgsrc.vh/imgn[0].height*imgn[0].width
}
else
{
imgn[0].width=imgsrc.vw
imgn[0].heigh=imgsrc.vw/imgn[0].width*imgn[0].heigh
}

}
if(imgsrc.vh-imgn[0].height>0)
{
var hh=(imgsrc.vh-imgn[0].height)/2
imgn[0].style.margin=hh+" 0px 0px 0px"
}



}
}

}
imgsrc.addEvent(window,"load",imgsrc.init,false)
//-->
</script>

</HEAD>
<BODY>
<div id="pic">
<dl>
<dt><a href="images/_1.jpg" title=""><img src="images/_1.jpg" border=0 alt="" title=""></a></dt>
<dd>图片1</dd>
</dl>

<dl>
<dt><a href="images/_2.jpg" title=""><img src="images/_2.jpg" border=0 alt="" title=""></a></dt>
<dd>图片1</dd>
</dl>
<dl>
<dt><a href="images/_3.jpg" title=""><img src="images/_3.jpg" border=0 alt="" title=""></a></dt>
<dd>图片1</dd>
</dl>

<dl>
<dt><a href="images/_4.jpg" title=""><img src="images/_4.jpg" border=0 alt="" title=""></a></dt>
<dd>图片1</dd>
</dl>

<dl>
<dt><a href="images/_5.jpg" title=""><img src="images/_5.jpg" border=0 alt="" title=""></a></dt>
<dd>图片1</dd>
</dl>

<dl>
<dt><a href="images/_6.jpg" title=""><img src="images/_6.jpg" border=0 alt="" title=""></a></dt>
<dd>图片1</dd>
</dl>

<dl>
<dt><a href="images/_1.jpg" title=""><img src="images/_1.jpg" border=0 alt="" title=""></a></dt>
<dd>图片1</dd>
</dl>

<dl>
<dt><a href="images/_2.jpg" title=""><img src="images/_2.jpg" border=0 alt="" title=""></a></dt>
<dd>图片1</dd>
</dl>

<dl>
<dt><a href="images/_3.jpg" title=""><img src="images/_3.jpg" border=0 alt="" title=""></a></dt>
<dd>图片1</dd>
</dl>

<dl>
<dt><a href="images/_4.jpg" title=""><img src="images/_4.jpg" border=0 alt="" title=""></a></dt>
<dd>图片1</dd>
</dl>

<dl>
<dt><a href="images/_5.jpg" title=""><img src="images/_5.jpg" border=0 alt="" title=""></a></dt>
<dd>图片1</dd>
</dl>

<dl>
<dt><a href="images/_6.jpg" title=""><img src="images/_6.jpg" border=0 alt="" title=""></a></dt>
<dd>图片1</dd>
</dl>
<div class="clear1"></div>
</div>



</BODY>
</HTML>


包太大上传不了,我把html文件放上来,自己放图片看看就行了



相关阅读:
加固WindowsServer2003IIS服务器(2)
ie8.0下显示本地图片的js实现代码 img.src
ORACLE SQL 语句分类
VBS教程:属性-Type 属性
js String对象中常用方法小结(字符串操作)
说明你的Javascript技术很烂的五个原因
ASP3.0高级编程(四十二)
JavaScript RegExp方法获取地址栏参数(面向对象)
Linux服务器ADSL利用NAT实现上网共享
使用Delphi创建IIS虚拟目录的方法
dedecms调用当前文档中TAG标签利于内页优化提高收录量
FreeBSD 里面用设置 ADSL宽带上网
linux vi 替换命令
js中将HTMLCollection/NodeList/伪数组转换成数组的代码
快速导航

Copyright © 2016 phpStudy |