Original jquery plugin Image adaptive

Source: Internet
Author: User

As follows:


function: make the picture adaptive centered in the container

limit: container needs a given size

How to use:

1, introduce jquery, then introduce fitimg plug-in

2, to the need to adapt the image of the container fixed width high

3. Header. Account. img {width:40px; height:40px; margin:5px 5px; float:left;}

4. Add Data-src Property

<div class= "img" DATA-SRC = "/images/avatar.jpg" ></div>
This does not write an IMG tag, the plugin will automatically generate IMG, the container as you want to render the picture on it

5. Call

$ (". img"). FITIMG ('/images/capture. png ')
In parentheses, if the picture that the data-src points to fails to load, if the picture also fails to load, the container empties everything inside the container

Source:

(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)


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Original jquery plugin Image adaptive

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.