Search code examples
jqueryajaxflickr

Ajax + Flickr loading images one-by-one with fadeIn effect


i'm re-using this piece of code:

$(function () {
    'use strict';
    // Load demo images from flickr:
    $.ajax({
        url: (window.location.protocol === 'https:' ?
                'https://secure' : 'http://api') +
                '.flickr.com/services/rest/',
        data: {
            format: 'json',
            method: 'flickr.interestingness.getList',
            api_key: ''
        },
        dataType: 'jsonp',
        jsonp: 'jsoncallback'
    }).done(function (result) {
        var 
            linksContainer = $('#links'),
            baseUrl;
        // Add the demo images as links with thumbnails to the page:
        $.each(result.photos.photo, function (index, photo) {
            baseUrl = 'http://farm' + photo.farm + '.static.flickr.com/' +
                photo.server + '/' + photo.id + '_' + photo.secret;
            $('<a/>')
                .append($('<img>').prop('src', baseUrl + '_s.jpg'))
                .hide()
                .fadeIn('slow')
                .prop('href', baseUrl + '_b.jpg')
                .prop('title', photo.title)
                .attr('data-gallery', '')
                .appendTo(linksContainer);
        });
    });
});

It makes his dirty job, but i have fadeIn effect ONLY with entire "wall of photo" (they are about 50), instead i would that FOR EVERY ONE SINGLE photo jquery applies fadeIn effect.

Thank you to all!


Solution

  • Try this

    $(function () {
        'use strict';
        // Load demo images from flickr:
        $.ajax({
            url: (window.location.protocol === 'https:' ?
                    'https://secure' : 'http://api') +
                    '.flickr.com/services/rest/',
            data: {
                format: 'json',
                method: 'flickr.interestingness.getList',
                api_key: ''
            },
            dataType: 'jsonp',
            jsonp: 'jsoncallback'
        }).done(function (result) {
            var 
                linksContainer = $('#links'),
                baseUrl;
            // Add the demo images as links with thumbnails to the page:
            $.each(result.photos.photo, function (index, photo) {
                baseUrl = 'http://farm' + photo.farm + '.static.flickr.com/' +
                    photo.server + '/' + photo.id + '_' + photo.secret;
                $('<a/>')
                    .append($('<img>').prop('src', baseUrl + '_s.jpg'))
                    .hide()
                    //.fadeIn('slow')
                    .prop('href', baseUrl + '_b.jpg')
                    .prop('title', photo.title)
                    .attr('data-gallery', '')
                    .appendTo(linksContainer);
            });
    
            setTimeout(function(){ showImg($('#links a:first'))}, 1000);
        });
    });
    
    function showImg(el)
    {
        el.fadeIn('slow');
        if(el.next().is('a'))
        {
            setTimeout(function(){ showImg(el.next())}, 1000);
        }
    }