html页面中图片自适应容器大小且上下左右居中插件
2012-11-15 15:24
561 查看
概述
做了一个通用的图片自适应插件,方便大家使用,下面直接上代码。
插件源码
使用
页面代码:
<div class="mytest">
<ul>
<li>
<img src="Images/green.png" />
</li>
<li>
<img src="Images/login.gif" />
</li>
</ul>
</div>
函数调用:
注意事项:
1、".mytest ul li“是图片的容器
2、li要定义大小,且要加属性overflow:hidden;
做了一个通用的图片自适应插件,方便大家使用,下面直接上代码。
插件源码
// jQuery.imgAutoSize.js // Wangbaifeng - http://donglisoft.com/ - MIT Licensed (function ($) { $.fn.imgAutoSize = function () { this.each(function () { var pwidth = $(this).width(); var pheight = $(this).height(); var _img = $(this).find("img"); var iwidth = _img.width(); var iheight = _img.height(); //alert(pwidth + "-" + pheight + "-" + iwidth + "-" + iheight) if (pwidth <= iwidth || pheight <= iheight) { if (iwidth / pwidth > iheight / pheight) { alert(pwidth); _img.css("width", pwidth).css("height", pwidth / iwidth * iheight); _img.css("margin-top", (pheight - pwidth / iwidth * iheight) / 2 + "px"); } else { _img.css("width", pheight / iheight * iwidth).css("height", pheight); _img.css("margin-left", (pwidth - pheight / iheight * iwidth) / 2 + "px"); } } else { _img.css("margin-left", (pwidth - iwidth) / 2 + "px"); _img.css("margin-top", (pheight - iheight) / 2 + "px"); } }); }; })(jQuery);
使用
页面代码:
<div class="mytest">
<ul>
<li>
<img src="Images/green.png" />
</li>
<li>
<img src="Images/login.gif" />
</li>
</ul>
</div>
函数调用:
<script type="text/javascript"> $(function () { $('.mytest ul li').imgAutoSize(); }); </script>
注意事项:
1、".mytest ul li“是图片的容器
2、li要定义大小,且要加属性overflow:hidden;
相关文章推荐
- 图片自适应父元素大小,并左右上下居中的css方法
- 图片自适应父元素大小,并左右上下居中的css方法
- 图片自适应父元素的大小,并左右上下居中
- css3 来实现图片在指定容器内等比例缩放,并控制中上下左右居中
- 用纯CSS实现容器内图片上下左右居中
- 只用CSS实现容器内图片上下左右居中
- html图片上下左右居中
- 基于jquery实现的图片在各种分辨率下未知的容器内上下左右居中
- css图片自动绽放大小,左右,上下居中
- 基于jquery实现的图片在各种分辨率下未知的容器内上下左右居中
- html div自适应动态上下左右居中
- css3 来实现图片在指定容器内等比例缩放,并控制中上下左右居中
- 图片在容器内上下左右居中
- html自适应页面上下左右分栏的处理技巧
- jQuery-图片预加载+等比例缩放+图片在容器中上下左右居中!
- jquery图片尺寸调整插件图片垂直居中自适应容器
- 只用CSS实现容器内图片上下左右居中
- webview加载html图片过大左右滑动的解决/webview加载图片自适应大小
- jquery图片尺寸调整插件图片垂直居中自适应容器
- JS实现图片大小自适应且位置居中,防止图片变形