效果图例如以下:



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

限制:容器须要给定大小

用法:

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

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

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

4、加入data-src属性

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

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

5、调用

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

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

源码:

  1. (function ($)
  2. {
  3. $.fn.extend({
  4. fitimg: function (errorimg)
  5. {
  6. $(this).each(function ()
  7. {
  8. if ($(this).data('src'))
  9. {
  10. $(this).empty()
  11. var img = document.createElement('img')
  12. $(this).append($(img))
  13. $(img).load(function ()
  14. {
  15. var parent = $(this).parent()
  16. var pWidth = parent.width()
  17. var pHeight = parent.height()
  18. var oWidth = $(this).width()
  19. var oHeight = $(this).height()
  20. if (oWidth / pWidth > oHeight / pHeight)
  21. {
  22. $(this).height(pHeight)
  23. var nWidth = pHeight * oWidth / oHeight
  24. $(this).width(nWidth)
  25. $(this).css('margin-left', -(nWidth - pWidth) / 2)
  26. }
  27. else
  28. {
  29. $(this).width(pWidth)
  30. var nHeight = pWidth * oHeight / oWidth
  31. $(this).height(nHeight)
  32. $(this).css('margin-top', -(nHeight - pHeight) / 2)
  33. }
  34. parent.css('overflow', 'hidden')
  35. }).error(function ()
  36. {
  37. if (errorimg)
  38. {
  39. $(this).parent().data('src', errorimg).fitimg()
  40. }
  41. else
  42. {
  43. $(this).parent().empty()
  44. }
  45. })
  46. $(img).attr('src', $(this).data('src'))
  47. }
  48. })
  49. return $(this)
  50. }
  51. })
  52. })(jQuery)

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

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

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

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

两种效果对照图例如以下

下面为插件最新的代码

  1. (function ($)
  2. {
  3. $.fn.extend({
  4. fitimg: function (errorimg, iszoomin)
  5. {
  6. $(this).each(function ()
  7. {
  8. $(this).empty()
  9. var img = document.createElement('img')
  10. $(this).append($(img))
  11. img.style.display = 'none'
  12. $(img).load(function ()
  13. {
  14. var parent = $(this).parent()
  15. var pWidth = parent.width()
  16. var pHeight = parent.height()
  17. var oWidth = $(this).width()
  18. var oHeight = $(this).height()
  19. if (oWidth / pWidth > oHeight / pHeight)
  20. {
  21. if (!iszoomin)
  22. {
  23. $(this).height(pHeight)
  24. var nWidth = pHeight * oWidth / oHeight
  25. $(this).width(nWidth)
  26. $(this).css('margin-left', -(nWidth - pWidth) / 2)
  27. }
  28. else
  29. {
  30. $(this).width(pWidth)
  31. var nHeight = pWidth * oHeight / oWidth
  32. $(this).height(nHeight)
  33. $(this).css('margin-top', (pHeight - nHeight) / 2)
  34. }
  35. }
  36. else
  37. {
  38. if (!iszoomin)
  39. {
  40. $(this).width(pWidth)
  41. var nHeight = pWidth * oHeight / oWidth
  42. $(this).height(nHeight)
  43. $(this).css('margin-top', -(nHeight - pHeight) / 2)
  44. }
  45. else
  46. {
  47. $(this).height(pHeight)
  48. var nWidth = pHeight * oWidth / oHeight
  49. $(this).width(nWidth)
  50. $(this).css('margin-left', (pWidth - nWidth) / 2)
  51. }
  52. }
  53. parent.css('overflow', 'hidden')
  54. img.style.display = ''
  55. }).error(function ()
  56. {
  57. if (errorimg)
  58. {
  59. $(this).parent().data('src', errorimg).fitimg(null, iszoomin)
  60. }
  61. else
  62. {
  63. $(this).parent().empty()
  64. }
  65. })
  66. $(img).attr('src', $(this).data('src'))
  67. })
  68. return $(this)
  69. }
  70. })
  71. })(jQuery)

2016/12/11更新

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

  1. (function ($) {
  2. $.fn.extend({
  3. fitimg: function (errorimg, iszoomin) {
  4. iszoomin = typeof iszoomin === 'undefined' ? false : iszoomin
  5. $(this).each(function () {
  6. $(this).empty()
  7. var img = document.createElement('img')
  8. $(this).append($(img))
  9. img.style.display = 'none'
  10. $(img).on('load', function () {
  11. var parent = $(this).parent()
  12. var pWidth = parent.width()
  13. var pHeight = parent.height()
  14. var oWidth = $(this).width()
  15. var oHeight = $(this).height()
  16. if (oWidth / pWidth > oHeight / pHeight) {
  17. if (!iszoomin) {
  18. $(this).height(pHeight)
  19. var nWidth = pHeight * oWidth / oHeight
  20. $(this).width(nWidth)
  21. $(this).css('margin-left', -(nWidth - pWidth) / 2)
  22. }
  23. else {
  24. $(this).width(pWidth)
  25. var nHeight = pWidth * oHeight / oWidth
  26. $(this).height(nHeight)
  27. $(this).css('margin-top', (pHeight - nHeight) / 2)
  28. }
  29. }
  30. else {
  31. if (!iszoomin) {
  32. $(this).width(pWidth)
  33. var nHeight = pWidth * oHeight / oWidth
  34. $(this).height(nHeight)
  35. $(this).css('margin-top', -(nHeight - pHeight) / 2)
  36. }
  37. else {
  38. $(this).height(pHeight)
  39. var nWidth = pHeight * oWidth / oHeight
  40. $(this).width(nWidth)
  41. $(this).css('margin-left', (pWidth - nWidth) / 2)
  42. }
  43. }
  44. parent.css('overflow', 'hidden')
  45. img.style.display = ''
  46. }).on('error', function () {
  47. if (errorimg) {
  48. $(this).parent().data('src', errorimg).fitimg(null, iszoomin)
  49. }
  50. else {
  51. $(this).parent().empty()
  52. }
  53. })
  54. $(img).attr('src', $(this).data('src'))
  55. })
  56. return $(this)
  57. }
  58. })
  59. })(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. [Python爬虫] 之三十一:Selenium +phantomjs 利用 pyquery抓取消费主张信息

    一.介绍 本例子用Selenium +phantomjs爬取央视栏目(http://search.cctv.com/search.php?qtext=消费主张&type=video)的信息(标 ...

  2. 恢复计算机崩溃数据的五款最佳Linux发行版

    嗨,Linux 新手们!你们在尝试运行命令时有没有搞坏过计算机系统?我相信你们有过这种经历.这一幕经常发生:你想尝试运行命令,或者安装测试更新版,结果下一次重启时计算机就崩溃了.我在本文将逐一介绍五款 ...

  3. 再谈Cognos利用FM模型来做同比环比

    很早之前已经讲过 <Cognos利用DMR模型开发同比环比>这篇文章里说的是不利用过滤器,而是采用 except (lastPeriods (-9000,[订单数据分析].[日期维度].[ ...

  4. Ubuntu简单搭建git私有服务

    gitserver搭建过程 搭建gitserver过程记录 例如以下: 环境: serverUbuntu虚拟机(Boss),能通过网络訪问到(server地址:192.168.9.103). clie ...

  5. 【cocos2d-x 3.7 飞机大战】 决战南海I (七) 控制器的实现

    控制器中的功能并不多,主要是以下这些 //对玩家分数的操作 CC_SYNTHESIZE_READONLY(SaveData *, m_saveData, SaveData); void update( ...

  6. .NET破解之爱奇迪(三)

    本教程只能用于学习研究,不可进行任何商业用途.如有使用,请购买正版,尊重他人劳动成果和知识产权! .NET破解之爱奇迪(一) .NET破解之爱奇迪(二) 一打开软件,就看到各种注册和未注册提示信息,就 ...

  7. 对象 get和set方法

    1.用途 用户定义的对象定义 getter 和 setter 以支持新增的属性. 示例:obj创建一个伪属性latest,它会返回log数组的最后一个元素. var obj = { log: ['ex ...

  8. LInux下inode空间报警-CROND出错导致/var/spool/postfix/maildrop/堆积

    Linux下显示磁盘空间不足,,通过 df -ih 查询发现/dev/mapper/*****var 下的inode用满.inode介绍 通过 du -sh * 查询/目录下的问题,最终查到/var/ ...

  9. Hadoop-1.2.1学习之Job创建和提交源码分析

    在Hadoop中,MapReduce的Java作业通常由编写Mapper和Reducer開始.接着创建Job对象.然后使用该对象的set方法设置Mapper和Reducer以及诸如输入输出等參数,最后 ...

  10. 聊聊iClient for Leaflet坐标转换问题

    作者:非法小恋 背景 SuperMap iClient for JavaScript 9D产品想必大伙都用了一段时间了,针对新推出的三款客户端产品,Leaflet,OpenLayaers,Mapbox ...