js+css图片自动等比例缩小且垂直居中
来源:互联网 发布:js 定义json字符串 编辑:程序博客网 时间:2024/05/22 00:41
图片自动等比例缩小,其实如果不考虑ie6的话,用css就可以实现,设定img的max-width和max-height,而标签内不设定widht和height即可。 <br />ie7已经支持max-width和max-height,这是为数不多的好消息之一。 <br />但是对于ie6及以前的版本,就只能用js来设置了。 <br /><br />在 ff 2.0/ ie6 / ie7 中测试通过。 <br />opera 8.5 cn 垂直居中未通过,正在研究[貌似opera下如果只有图片行高会失效……]。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>图片自动等比例缩小且垂直居中</title>
<!--[if lte IE 6]>
<script type="text/javascript" language="javascript">
function imgFix() {
//定义要限制的图片宽高,这个宽高要同style里面定义的相同,小于限定高宽的图片不操作
var widthRestriction = 200;
var heightRestriction = 200;
var allElements = document.getElementsByTagName('*')
for (var i = 0; i < allElements.length; i++)
{
if (allElements[i].className.indexOf('imgBox') >= 0)
{
var imgElements = allElements[i].getElementsByTagName('img');
for (var j=0; j < imgElements.length; j++)
{
if ( imgElements[j].width > widthRestriction || imgElements[j].height > heightRestriction )
{
if ( imgElements[j].width > imgElements[j].height)
{
imgElements[j].height = imgElements[j].height*(widthRestriction/imgElements[j].width);
imgElements[j].width = widthRestriction;
} else
{
imgElements[j].width = imgElements[j].width*(heightRestriction/imgElements[j].height);
imgElements[j].height = heightRestriction;
}
}
if ( imgElements[j].height < heightRestriction )
{
imgElements[j].style.paddingTop = ( heightRestriction -imgElements[j].height ) /2 + "px";
}
} /*for j*/
}
}/*for i*/
}
window.onload = imgFix;
</script>
<![endif]-->
<style type="text/css">
<!--
* {
margin:0;
padding:0;
}
.imgBox li {
list-style:none;
width:200px; /* 宽度 */
height:200px; /* 高度 */
background:#ccc;
border:1px solid #666;
text-align:center;
margin:5px;
line-height:200px;
}
.imgBox img {
max-width:200px; /* 宽度 */
max-height:200px; /* 高度 */
vertical-align:middle;
}
-->
</style>
</head>
<body>
<ul class="imgBox">
<li><img src="http://www.ddcat.net/tebie/coco/img/coco_20070618_03.jpg" alt="img" /></li>
<li><img src="http://www.ddcat.net/tebie/coco/img/coco_20070618_04.jpg" alt="img" /></li>
<li><img src="http://www.ddcat.net/bbs2007/images/style_1/zh-cn/redirect_logo.gif" alt="img" /></li>
<li><img src="http://www.ddcat.net/blog/templates/ddcat2007/img/logo.gif" alt="img" /></li>
</ul>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>图片自动等比例缩小且垂直居中</title>
<!--[if lte IE 6]>
<script type="text/javascript" language="javascript">
function imgFix() {
//定义要限制的图片宽高,这个宽高要同style里面定义的相同,小于限定高宽的图片不操作
var widthRestriction = 200;
var heightRestriction = 200;
var allElements = document.getElementsByTagName('*')
for (var i = 0; i < allElements.length; i++)
{
if (allElements[i].className.indexOf('imgBox') >= 0)
{
var imgElements = allElements[i].getElementsByTagName('img');
for (var j=0; j < imgElements.length; j++)
{
if ( imgElements[j].width > widthRestriction || imgElements[j].height > heightRestriction )
{
if ( imgElements[j].width > imgElements[j].height)
{
imgElements[j].height = imgElements[j].height*(widthRestriction/imgElements[j].width);
imgElements[j].width = widthRestriction;
} else
{
imgElements[j].width = imgElements[j].width*(heightRestriction/imgElements[j].height);
imgElements[j].height = heightRestriction;
}
}
if ( imgElements[j].height < heightRestriction )
{
imgElements[j].style.paddingTop = ( heightRestriction -imgElements[j].height ) /2 + "px";
}
} /*for j*/
}
}/*for i*/
}
window.onload = imgFix;
</script>
<![endif]-->
<style type="text/css">
<!--
* {
margin:0;
padding:0;
}
.imgBox li {
list-style:none;
width:200px; /* 宽度 */
height:200px; /* 高度 */
background:#ccc;
border:1px solid #666;
text-align:center;
margin:5px;
line-height:200px;
}
.imgBox img {
max-width:200px; /* 宽度 */
max-height:200px; /* 高度 */
vertical-align:middle;
}
-->
</style>
</head>
<body>
<ul class="imgBox">
<li><img src="http://www.ddcat.net/tebie/coco/img/coco_20070618_03.jpg" alt="img" /></li>
<li><img src="http://www.ddcat.net/tebie/coco/img/coco_20070618_04.jpg" alt="img" /></li>
<li><img src="http://www.ddcat.net/bbs2007/images/style_1/zh-cn/redirect_logo.gif" alt="img" /></li>
<li><img src="http://www.ddcat.net/blog/templates/ddcat2007/img/logo.gif" alt="img" /></li>
</ul>
</body>
</html>
- js+css图片自动等比例缩小且垂直居中
- js+css图片自动等比例缩小且垂直居中
- js+css图片自动等比例缩小且垂直居中
- js+css图片自动等比例缩小且垂直居中[v20071208]
- js+css图片自动等比例缩小且垂直居中[v20071208]
- CSS强制等比例缩小图片
- CSS强制等比例缩小图片
- Node.js等比例放倒缩小图片
- JS等比例缩放图片并构建图片水平垂直居中解决方案
- div css布局中CSS图片大小自动按比例等比例缩小图片不变形解决技巧
- //等比例缩小图片
- 图片等比例缩小
- 对图片进行等比例缩放,并水平垂直居中
- css实现图片等比例缩小不变形
- 【CSS实现图片等比例缩小不变形】
- css实现文字垂直居中且自动换行
- div css布局中CSS图片大小自动按比例等比例缩小图片不变形解决技巧(转)
- 如何一个td或table里的图片等比例缩小(css问题)图片自动缩放
- 用Visual Studio 2005和MapXtremeNT开发网络地理信息系统
- 三则小故事
- SEO与外链
- Genetic Algorithm for VRP
- 网站推广的18种秘笈
- js+css图片自动等比例缩小且垂直居中
- DataSet的用法
- 有关日期函数
- 中文版Windows XP升级SP3安装方法
- Log4j具体输出信息级别配置方法
- 佩服!原来兔子是这样吃掉狼和野猪的
- WebForm中DataGrid的20篇经典文章
- SWT/JFace一些经典技术与算法
- vs2005常用快捷键