javascript图片自动缩放和垂直居中处理函数
2008-10-10 00:00
696 查看
<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.jb51.net/file_images/sucai/111131524.jpg" onload="autoSizeImg(this.parentNode,5,5,true)"/> </div> <div class="testCss1"> <img src="http://files.jb51.net/upload/20081010215957304.jpg" onload="autoSizeImg(this.parentNode,5,5,true)"/> </div> <div> </div> <div class="testCss2"> <img src="http://files.jb51.net/upload/20081010215958874.gif" onload="autoSizeImg(this.parentNode,5,5,true)"/> </div> </body> </html>
注释:
1、后面的testCss1和testCss2两个样式跟testCss是一样的。只是设置了大小。
2、红色部分的height和width必须定义,是设置存放img外面的父层容器的高和宽。
3、text-algin:为图片水平居中
4、display:block,必须设置。否则FF下启用自动缩放不起作用。
使用方法:
1、是需要在img的父层定义样式,包含上述注释里面提到的东西;
2、是img加上
onload="autoSizeImg(this.parentNode,5,5,true)"
参数说明:
参数Contents一般采用this.parentNode,即使img的父容易
参数offsetWidth,offsetHeight分别是宽和高度居中的修正值。
参数vlmiddle设置为true则是垂直居中
Untitled
.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;}
/*
函数功用:图片自动缩放和垂直居中处理函数
编写时间:2008-08-06
编 写 人:PaulLeder
联 系 我:paulleder@yeah.net
*/
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;incwidth){
var nw=ncwidth-offsetWidth;
img.width=nw
img.height=(nw*ih)/iw;
}else if(img.widthncheight){
var nh=ncheight-offsetHeight;
img.height=nh;
img.width=(nh*iw)/ih;
}
if(img.height>ncheight&&img.width
相关文章推荐
- javascript图片自动缩放和垂直居中处理函数
- javascript图片等比缩放并自动居中
- JavaScript 图片自动缩放代码!
- JavaScript支持IE和FireFox浏览器自动缩放图片代码
- Javascript + Asp.net 让图片自动成比例缩放
- [cnblogs镜像]页面定制CSS代码初探(一):页面变宽 文本自动换行 图片按比缩放
- JavaScript实现网页图片等比例缩放实现代码及调用方式
- 封装的一些实现图片水印与图片自动结合缩放的类
- 史上最牛X最简洁的Javascript图片缩放代码
- css+javascript 图片自动同比例缩小并且实现垂直居中
- js控制图片自动缩放,实现铺满盒子,不变形,完全局中
- 【JavaScript等比例缩放图片】
- 最新javascript自动按比例显示图片,按比例压缩图片显示
- 图片的浏览、缩放、拖动和自动居中
- JavaScript基础 用定时器做一组图片的自动切换
- Beyond Compare怎么自动缩放对比图片
- JavaScript 图片切割效果(带拖放、缩放效果)[转/改]
- PHP 输出头像到网页,按照输入的参数自动缩放图片
- android 图片的浏览、缩放、拖动和自动居中示例