原创jQuery插件之图片自适应

效果图如下:


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

限制:容器需要给定大小

使用方法:

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)


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值