Coder Social home page Coder Social logo

farinspace / jquery.imgpreload Goto Github PK

View Code? Open in Web Editor NEW
301.0 13.0 112.0 299 KB

The jQuery.imgpreload plugin lets you preload images before and after the DOM has loaded.

Home Page: http://farinspace.com/jquery-image-preload-plugin/

License: Other

JavaScript 100.00%

jquery.imgpreload's Introduction

jQuery Image Preload Plugin

The jQuery.imgpreload plugin lets you preload images before and after the DOM has loaded.

Tested: IE6, IE7, IE8, IE9+, FF, Chrome, Safari

Usage

Callbacks

The following are available callbacks, you may change them globally or override the defaults by passing the settings object to the imgpreload method.

$.fn.imgpreload.defaults =
{
    each: null // callback invoked when each image is loaded
    , all: null // callback invoked when all images have loaded
};

After DOM loaded

The following illustrates using the plugin to preload images after the dom has loaded (e.g. using jQuery selectors):

$('#content img').imgpreload(function()
{
    // callback invoked when all images have loaded
    // this = array of dom image objects
    // check for success with: $(this[i]).data('loaded')
});
$('img.logos').imgpreload
({
    each: function()
    {
        // callback invoked when each image is loaded
        // this = dom image object
        // check for success with: $(this).data('loaded')
    },
    all: function()
    {
        // callback invoked when all images have loaded
        // this = array of dom image objects
        // check for success with: $(this[i]).data('loaded')
    }
});

Before DOM loaded

To preload images before the dom has loaded, for instance in the head of the document, you would have to use specific image paths.

$.imgpreload('/images/a.gif',function()
{
    // callback invoked when all images have loaded
    // this = array of dom image objects
    // check for success with: $(this[i]).data('loaded')
});

You can pass a single image path (as above) or an array of image paths.

$.imgpreload(['/images/a.gif','/images/b.gif'],function()
{
    // this = array of dom image objects
    // check for success with: $(this[i]).data('loaded')
    // callback executes when all images are loaded
});

each and all callbacks are available to use.

$.imgpreload(['/images/a.gif','/images/b.gif'],
{
    each: function()
    {
        // callback invoked when each image is loaded
        // this = dom image object
        // check for success with: $(this).data('loaded')
    },
    all: function()
    {
        // callback invoked when all images have loaded
        // this = array of dom image objects
        // check for success with: $(this[i]).data('loaded')
    }
});

jquery.imgpreload's People

Contributors

farinspace avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jquery.imgpreload's Issues

Preloaded Image Width and Size

Hey there, nice work with this plugin - thanks!

I just wanted to check if there was any case we can also extract the dimensions of the image in the data of that element.

I tried adding

$.data(this, 'dimensions', { 'width': img.width, 'height': img.height });

just after the src to that image object is allocated but its giving me an error or indefinite recursion... any clues to how we can go over this without removing the above code?

thanks a lot!

Is it supposed to work in IE8?

Throws error css() is null or not an object on line 59, which is

url = $(elem).attr('src') || $(elem).css('background-image').replace(/^url\((?:"|')?(.*)(?:'|")?\)$/mg, "$1");

I'm using it with array of images, rather than jquery elements.

load event fires multiple time with animated GIF on IE7

The load event is actually fired at each cycle of an animated GIF, causing infinite loop (the callback is called over and over)

A simple solution is to unbind the load and error event on the image, once we are sure the image is loaded.
It also "keep things clean"

            $(img).bind('load error', function(e)
            {
                loaded.push(img_obj);
                $(this).unbind('load error');
                $.data(img_obj, 'loaded', ('error'==e.type)?false:true);

                if (settings.each instanceof Function) { settings.each.call(img_obj); }

                // http://jsperf.com/length-in-a-variable
                if (loaded.length>=imgs.length && settings.all instanceof Function) { settings.all.call(loaded); }
            });

see here : http://fragged.org/preloading-images-using-javascript-the-right-way-and-without-frameworks_744.html

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.