效果图例如以下:



功能:使图片自适应居中位于容器内

限制:容器须要给定大小

用法:

1、引入jQuery。然后引入fitimg插件

2、给须要图片自适应的容器固定宽高

3、header .account .img { width: 40px; height: 40px; margin: 5px 5px; float: left; }

4、加入data-src属性

<div class="img" data-src ="/Images/avatar.jpg"></div>

这里并没有写img标签,插件会自己主动生成img,把容器当成你想要呈现的图片就能够了

5、调用

$(".img").fitimg('/Images/捕获.png')

括号内为假设data-src指向的图片载入失败的替补图片,假设该图片也载入失败,则该容器会清空容器内全部内容

源码:

(function ($)
{
$.fn.extend({
fitimg: function (errorimg)
{
$(this).each(function ()
{
if ($(this).data('src'))
{
$(this).empty()
var img = document.createElement('img')
$(this).append($(img))
$(img).load(function ()
{
var parent = $(this).parent()
var pWidth = parent.width()
var pHeight = parent.height()
var oWidth = $(this).width()
var oHeight = $(this).height()
if (oWidth / pWidth > oHeight / pHeight)
{
$(this).height(pHeight)
var nWidth = pHeight * oWidth / oHeight
$(this).width(nWidth)
$(this).css('margin-left', -(nWidth - pWidth) / 2)
}
else
{
$(this).width(pWidth)
var nHeight = pWidth * oHeight / oWidth
$(this).height(nHeight)
$(this).css('margin-top', -(nHeight - pHeight) / 2)
}
parent.css('overflow', 'hidden')
}).error(function ()
{
if (errorimg)
{
$(this).parent().data('src', errorimg).fitimg()
}
else
{
$(this).parent().empty()
}
})
$(img).attr('src', $(this).data('src'))
}
})
return $(this)
}
})
})(jQuery)

近期(20150831)又加了两个新的功能

1、等图片载入完毕才显示出来,以免因网络问题导致图片刚開始非常大,然后再由js缩放到恰当大小。这个过程不应让用户看见,所以做了一点小小的处理

2、加入图片自适应选项。曾经仅仅同意拉伸到和容器一样大,如今添加可选參数能够缩小到被容器包裹

新增的參数名叫iszoomin,默觉得放大,也就是说假设不传这个值表示进行放大操作

两种效果对照图例如以下

下面为插件最新的代码

(function ($)
{
$.fn.extend({
fitimg: function (errorimg, iszoomin)
{
$(this).each(function ()
{
$(this).empty()
var img = document.createElement('img')
$(this).append($(img))
img.style.display = 'none'
$(img).load(function ()
{
var parent = $(this).parent()
var pWidth = parent.width()
var pHeight = parent.height()
var oWidth = $(this).width()
var oHeight = $(this).height()
if (oWidth / pWidth > oHeight / pHeight)
{
if (!iszoomin)
{
$(this).height(pHeight)
var nWidth = pHeight * oWidth / oHeight
$(this).width(nWidth)
$(this).css('margin-left', -(nWidth - pWidth) / 2)
}
else
{
$(this).width(pWidth)
var nHeight = pWidth * oHeight / oWidth
$(this).height(nHeight)
$(this).css('margin-top', (pHeight - nHeight) / 2)
}
}
else
{
if (!iszoomin)
{
$(this).width(pWidth)
var nHeight = pWidth * oHeight / oWidth
$(this).height(nHeight)
$(this).css('margin-top', -(nHeight - pHeight) / 2)
}
else
{
$(this).height(pHeight)
var nWidth = pHeight * oWidth / oHeight
$(this).width(nWidth)
$(this).css('margin-left', (pWidth - nWidth) / 2)
}
}
parent.css('overflow', 'hidden')
img.style.display = ''
}).error(function ()
{
if (errorimg)
{
$(this).parent().data('src', errorimg).fitimg(null, iszoomin)
}
else
{
$(this).parent().empty()
}
})
$(img).attr('src', $(this).data('src'))
})
return $(this)
}
})
})(jQuery)

2016/12/11更新

jQuery3.1已经公布,为了适配jQuery3.1,代码改动例如以下

(function ($) {
$.fn.extend({
fitimg: function (errorimg, iszoomin) {
iszoomin = typeof iszoomin === 'undefined' ? false : iszoomin
$(this).each(function () {
$(this).empty()
var img = document.createElement('img')
$(this).append($(img))
img.style.display = 'none'
$(img).on('load', function () {
var parent = $(this).parent()
var pWidth = parent.width()
var pHeight = parent.height()
var oWidth = $(this).width()
var oHeight = $(this).height()
if (oWidth / pWidth > oHeight / pHeight) {
if (!iszoomin) {
$(this).height(pHeight)
var nWidth = pHeight * oWidth / oHeight
$(this).width(nWidth)
$(this).css('margin-left', -(nWidth - pWidth) / 2)
}
else {
$(this).width(pWidth)
var nHeight = pWidth * oHeight / oWidth
$(this).height(nHeight)
$(this).css('margin-top', (pHeight - nHeight) / 2)
}
}
else {
if (!iszoomin) {
$(this).width(pWidth)
var nHeight = pWidth * oHeight / oWidth
$(this).height(nHeight)
$(this).css('margin-top', -(nHeight - pHeight) / 2)
}
else {
$(this).height(pHeight)
var nWidth = pHeight * oWidth / oHeight
$(this).width(nWidth)
$(this).css('margin-left', (pWidth - nWidth) / 2)
}
}
parent.css('overflow', 'hidden')
img.style.display = ''
}).on('error', function () {
if (errorimg) {
$(this).parent().data('src', errorimg).fitimg(null, iszoomin)
}
else {
$(this).parent().empty()
}
})
$(img).attr('src', $(this).data('src'))
})
return $(this)
}
})
})(jQuery)

原创jQuery插件之图片自适应的更多相关文章

  1. 原创jquery插件treeTable(转)

    由于工作需要,要直观的看到某个业务是由那些子业务引起的异常,所以我需要用树表的方式来展现各个层次的数据. 需求: 1.数据层次分明: 2.数据读取慢.需要动态加载孩子节点: 3.支持默认展开多少层. ...

  2. ASP.NET中使用jQuery插件实现图片幻灯效果

    参照网上的资料及提供的jQuery插件实现图片幻灯效果. 1.页面前台代码: //头部引用 <head runat="server"><title>< ...

  3. JQuery插件让图片旋转任意角度且代码极其简单 - 摘自网友

    JQuery插件让图片旋转任意角度且代码极其简单 2012-04-01 09:57:03     我来说两句      收藏    我要投稿 引入下方的jquery.rotate.js文件,然后通过$ ...

  4. jQuery插件实现图片展开效果,jquery.gallery。仿腾讯QQ空间说说图片展示效果。

    公司的项目http://www.umfun.com/,有个说说的页面(和腾讯QQ空间说说一样),里面有个发表图片功能,上传完图片,需要点击展开的效果. 当时手里面事情比较多(公司就我一个前端),忙不过 ...

  5. 使用jquery插件实现图片延迟加载技术(懒加载)

    有时我们看到一些大型网站,页面如果有很多图片的时候,当你滚动到相应的行时,当前行的图片才即时加载的,这样子的话页面在打开只加可视区域的图片,而其它隐藏的图片则不加载,一定程序上加快了页面加载的速度,对 ...

  6. JQuery插件之图片轮播插件–slideBox

    来源:http://www.ido321.com/852.html 今天偶然发现了一个比较好用的图片轮播插件—slideBox 先看看效果:http://slidebox.sinaapp.com/ 代 ...

  7. JQuery插件:图片上传本地预览插件,改进案例一则。

    /* *名称:图片上传本地预览插件 v1.1 *作者:周祥 *时间:2013年11月26日 *介绍:基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari *插 ...

  8. 使用jquery插件实现图片延迟加载--懒加载技术

    原文链接:http://www.cnblogs.com/lei2007/archive/2013/05/31/3110725.html 感谢作者.以下为原文,备忘仅供自己学习. 第一:lazyLoad ...

  9. JQuery插件让图片旋转任意角度且代码极其简单

    引入下方的jquery.rotate.js文件,然后通过$("选择器").rotate(角度);可以旋转任意角度, 例如$("#rotate-image").r ...

随机推荐

  1. Java 中 方法名或类名 变更 同时 更新 所有引用的 类名或方法名 的解决方案

    选中 类名,或属性名  Ctrl + 1  然后选择 理新当前文件,还是更新整个工作空间,然后修改对应的类名或方法名  回车即可. 如果.有SVN 版本在控制着,则 会提示,然后把对应的文件 锁定 再 ...

  2. html5-video视频播放

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  3. Codeforces Round #250 (Div. 1) D. The Child and Sequence

    D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...

  4. linux free命令详解和使用实例(查看内存使用率)

    转载:http://www.jb51.net/LINUXjishu/152017.html 1.命令格式: free [参数] 2.命令功能: free 命令显示系统使用和空闲的内存情况,包括物理内存 ...

  5. ASP.NET HTTP404错误怎么办

    如图所示,HTTP404错误. 这是由于没有安装ASP.NET导致的.我们在控制面板->打开或关闭windows功能->Internet信息服务->万维网服务->应用程序开发功 ...

  6. linux性能监控命令

    vmstat 可以用来监控虚拟内存.可对操作系统的虚拟内存.IO.CPU等多个指标的整体情况进行监视. Linux系统的内存分为物理内存和虚拟内存两种.物理内存是真实的,也就是物理内存条上的内存.而虚 ...

  7. spring作用域(Spring Bean Scopes Example)

    http://docs.spring.io/spring/docs/2.0.x/reference/beans.html#beans-factory-scopes In Spring, bean sc ...

  8. Unity Web前端研究

    原地址:http://blog.csdn.net/libeifs/article/details/7200630 开发环境 Window7 Unity3D  3.4.1 MB525defy Andro ...

  9. CodeForces 2A - Winner(模拟)

    题目链接:http://codeforces.com/problemset/problem/2/A A. Winner time limit per test 1 second memory limi ...

  10. 【转帖】为什么不要把ZooKeeper用于服务发现

    http://www.infoq.com/cn/news/2014/12/zookeeper-service-finding ZooKeeper是Apache基金会下的一个开源的.高可用的分布式应用协 ...