效果图例如以下:



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

限制:容器须要给定大小

用法:

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. Web开发中的6个坏习惯

    在 Usersnap,我们在能很好的组织网站开发有超过20(总和)年的经验.我们认为这些过去的经验能让我们很好的分辨出什么是好.坏和丑陋的网站开发.如今我们不想把注意力放在消极的部分,但就这一次,我们 ...

  2. 模拟jQuery中的ready方法及实现按需加载css,js实例代码

    这篇文章介绍了模拟jQuery中的ready方法及实现按需加载css,js实例代码,有需要的朋友可以参考一下     一.ready函数的实现经常用jQuery类库或其他类库中的ready方法,有时候 ...

  3. Report Studio中目录结构报表浅析

    一:场景:在一个报表中如果存在多个页面,每个页面显示不同的数据,如何通过目录控件实现对每一个报表的友好访问呢?下面我们就来看一下下面的效果,如下图1,2 图1:-------------------- ...

  4. ejs和swig对比的问题之一

    本来想测试下两者在nodejs中得执行速度,设置了一个测试数据,如下 var testData = { title:'测试标题', description: '<p>这是一个描述,里面用很 ...

  5. tomcat支持中文文件名下载

    http://blog.csdn.net/wnczwl369/article/details/7483806 Tomcat 是Java开发者使用得较多的一个Web服务器,因为它占用资源小,运行速度快等 ...

  6. JDK1.5 AtomicLong实例

    JDK1.5 AtomicLong实例 类 AtomicLong 可以用原子方式更新的 long 值.有关原子变量属性的描述,请参阅 java.util.concurrent.atomic 包规范.A ...

  7. Git 提示fatal: remote origin already exists

    Git 提示fatal: remote origin already exists 错误解决办法 最后找到解决办法如下: 1.先删除远程 Git 仓库 $ git remote rm origin 2 ...

  8. 〖Android〗(how-to) fix k860/k860i buletooth.

    bluedroid.so for k860/k860i 1./media/Enjoy/AndroidCode/cm10.1/device/lenovo/stuttgart/bluetooth/blue ...

  9. js获取事件源及触发该事件的对象

    怎样获取事件源及触发该事件的对象,方法有非常多,js中能够通过event来实现.以下有个不错的演示样例,感兴趣的朋友能够參考下: function myfunction(event) { event ...

  10. php 命令行方式运行时 几种传入参数的方式

    1. url方式 $param = array(); if ($argc > 1) { parse_str ( $argv [1], $param ); foreach ( $param as ...