AnythingSlider Lazy Image Loading

Add, remove, adjust any or all options so you can see how it works.

by Steven Senkus

HTML

<script src="http://proloser.github.com/AnythingSlider/js/jquery.easing.1.2.js"></script>
<script src="http://proloser.github.com/AnythingSlider/js/swfobject.js"></script>
<script src="http://css-tricks.github.io/AnythingSlider/js/jquery.anythingslider.js"></script>
<link rel="stylesheet" href="http://css-tricks.github.io/AnythingSlider/css/anythingslider.css">
<div id="wrapper">
    
    <ul id="slider">
        
        <li><img src="http://placekitten.com/300/200" alt="" /></li>
        
        <li><img src="http://placehold.it/300x200" alt="" /></li>
        
        <li><img src="http://ipsumimage.appspot.com/300x200.png" alt="" /></li>
        
        <li><img src="http://dummyimage.com/300x200/000/fff.jpg" alt="" /></li>
        
    </ul>
    
    <br><div class="message"></div>
    
</div>

CSS

.message { text-align: center; }
#wrapper { width: 400px; height: 300px; margin: 20px auto; } /* used when "expand" is true */
#slider { width: 400px; height: 300px;}
/* Conditional comments don't work in this playground, If testing in IE7 or older browsers, uncomment out the CSS below:

Also note, this may not work properly on themes other than the default as the chosen theme stylesheet is loaded after this CSS and will over-ride it */

/*

div.anythingSlider .arrow {
top: 35% !important;
}
div.anythingSlider-metallic .arrow {
top: 40% !important;
}
div.anythingSlider-construction .arrow {
top: 14% !important;
}
div.anythingSlider-minimalist-round .arrow , div.anythingSlider-minimalist-square .arrow {
top: 45% !important;
}
div.anythingSlider-cs-portfolio .arrow {
bottom: -24px !important;
}

div.anythingSlider .arrow a,
div.anythingSlider-metallic .arrow a,
div.anythingSlider-minimalist-square .arrow a,
div.anythingSlider-construction .arrow a,
div.anythingSlider-minimalist-round .arrow a {
margin: 0 !important;
}

*/

JavaScript

$('#slider').anythingSlider({

    // *********** Appearance ***********
    // Override the default CSS width
    width: 300,
    // Override the default CSS height
    height: 200,
    
    // If autoPlay is on, this can force it to start stopped
    startStopped: true,

    // *********** Callbacks ***********
    // Callback when the plugin finished initializing
    onInitialized: function(e, slider) {
        var start = slider.options.startPanel;
        // allow cloned panel images to load
        slider.$el.find('.panel').eq(start).siblings(':not(.cloned)')
            .find('img').each(function(){
                var $el = $(this);
                $el.attr('src', function(i, src){
                    $el.attr('data-src', src);
                    return '';
                });
            });
            
    },
    
    // Callback when slide initiates, before control animation
    onSlideInit: function(e, slider) {
        slider.$targetPage.find('img').each(function(){
            if ($(this).attr('src') === '') {
                var newsrc = $(this).attr('data-src');
                $(this).attr('src', newsrc);
                
                // show loading message
                $('.message').html('loading: ' + newsrc).stop(true,true).fadeIn().delay(1000).fadeOut();
            }
        });
    }

});