1

I have following to do the photo slideshow:

$('#Yeah').click(function() {
    {% for photo in photos %}
    $('#slideShow').append('<img src="/image?blob_key={{ photo }}" />');
    % endfor %}

    $('#slideShow').cycle({ 
      fx:     'fade',
      speed:  300, 
      next:   '#slideShow', 
      timeout: 0 
    });
});

When I have empty img tag under the div tag, the slideshow won't work at all.

<div id="slideShow">

</div>

However, if I at least add one img tag under the div tag, it works with the same codes.

<div id="slideShow">
    <img class="pics" width="200" height="200"> 
</div>

Does anyone have any clue what's the problem ? Any helps are appreciated.

MrCooL
  • 926
  • 3
  • 15
  • 30

1 Answers1

2

I think that the problem is that the images are not fully loaded before the Cycle plugin is initialized.

Looking through the source of the "lite" version, I see:

    $slides.each(function() {
        var $el = $(this);
        this.cycleH = (opts.fit && opts.height) ? opts.height : $el.height();
        this.cycleW = (opts.fit && opts.width) ? opts.width : $el.width();
    });

Because you do not specify the width and height options, it defaults to the width and height of the <img> element, which are both 0 when the image is not fully loaded.

EDIT: Try:

    var imgEls = $([]);

    {% for photo in photos %}
    imgEls = imgEls.add($('<img src="/image?blob_key={{ photo|urlencode }}" />'));
    {% endfor %}

    var numLoaded = 0;
    imgEls.load(function () {
        ++numLoaded;

        if (numLoaded == imgEls.length) {
            $('#slideShow').cycle({ 
                fx:     'fade',
                speed:   300
            });
        }
    });

    imgEls.appendTo($('#slideShow'));

EDIT2: There is a syntax error:

$(this\).ajaxSubmit(options);

needs to be:

$(this).ajaxSubmit(options);
//    ^ No backslash character '\'
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
  • Do you know why I have to press the button (id="Yeah") twice only it will refresh accordingly? Actually, the button is for me to test it. My intention is to let it load under $(document).ready(function() Any idea how to put workaround on this? – MrCooL Aug 28 '11 at 04:41
  • It's actually working by placing the code imgEls.appendTo($('#slideShow')); before executing the slideShow function.... However, when I put it under $(document).ready(function() without being in the button function, it's no longer working. Any idea? – MrCooL Aug 28 '11 at 05:20
  • @MrCooL: Do you mean that you build up `imgEls`, append them to `$('#slideShow')`, and then immediately call the Cycle plugin? As I said in my answer, I don't think that that will work because the images have not been fully loaded. I edited my answer slightly. Please try that code and let me know if it works. – Daniel Trebbien Aug 28 '11 at 11:56
  • Still not working. BTW, I actually need to set the fixed width and height for each of this image, will that affect the codes you posted here? – MrCooL Aug 28 '11 at 12:14
  • @MrCooL: No, setting widths and heights explicitly should be fine. Do you have the web page on a web server so that I can see the issue(s) for myself? – Daniel Trebbien Aug 28 '11 at 12:19
  • http://foodsuggest.appspot.com/restaurant_profile?restaurant_key=ag1zfmZvb2RzdWdnZXN0chELEgpSZXN0YXVyYW50IgEyDA You should be able to access this. You have to click on the photo link. – MrCooL Aug 28 '11 at 12:24
  • @MrCooL: Looking through the generated code, you don't need `$(function () {` and the matching `});` because it's already within a DOM ready handler (`$(document).ready(function() { ... });`). What is not working? It seems to work in Firefox. Do I need to try a different browser? – Daniel Trebbien Aug 28 '11 at 12:43
  • @MrCooL: Note that the way you have the Cycle plugin configured, the user has to click on the image to see the next one. If you want it to flip through the images automatically, then remove the `next` and `timeout` options. (I updated my answer.) – Daniel Trebbien Aug 28 '11 at 12:48
  • @MrCooL: I see that you are making some changes already. Note that there is a syntax error in the current JavaScript code. See **EDIT2** of my answer. – Daniel Trebbien Aug 28 '11 at 13:01
  • I've now removed the $function... and now even the alert('Yeah'); is not even working? It didn't even show the message box Yeah for both Chrome and Firefox. – MrCooL Aug 28 '11 at 13:02
  • @MrCooL: There is an extra backslash character. `$(this\).ajaxSubmit(options);` should be `$(this).ajaxSubmit(options);`. – Daniel Trebbien Aug 28 '11 at 13:04
  • @MrCooL: Also, `imgEls.appendTo($('#slideShow'));` needs to be outside the `load` handler. – Daniel Trebbien Aug 28 '11 at 13:06
  • sorry for dragging this long. The slideshow still doesn't work in both Firefox and Chrome that I've tried. :( – MrCooL Aug 28 '11 at 13:35
  • @MrCooL: Now when I visit the page, Firebug says that all of the image requests are resulting in HTTP 500 Internal Server Error. It looks like you are using Django-nonrel. There must be an error that is occurring for the view servicing the images. Maybe try re-enabling debugging. – Daniel Trebbien Aug 28 '11 at 14:40
  • @MrCooL: The generated `` tags seem malformed as the double quote terminating the `src` attribute value is missing. For example, `imgEls = imgEls.add($(''));` There should be a double quote after the blob key: `imgEls = imgEls.add($(''));` (notice the extra double quote character before the width attribute). – Daniel Trebbien Aug 28 '11 at 14:43
  • Thanks for correcting that @Daniel Trebbien. Do you have idea why sometimes it works and sometimes it don't. Either in Chrome or Firefox, when I click on Photo, it works. When I click other link and then clicked back again the Photo link, again, it didn't work. Even when it worked, it seems very slow as in I could see multiple images before the slideshow starts. Is there other alternatives of Cycle plugin? Eventually, I would need to put this photo slideshow in a fancybox. – MrCooL Aug 28 '11 at 16:18
  • @MrCooL: The "multiple images" are due to the fact that the `#slideShow` `
    ` is visible while the images are loading. To fix this problem, add `style="display:none"` to the `
    `, and just before calling the Cycle plugin, run `$("#slideShow").show();`, as in `$("#slideShow").show().cycle(...);` As far as alternatives, there are a number of jQuery slideshow plugins, but this one seems pretty good. If you want it in a fancybox, then just use the fancybox plugin and see this question: http://stackoverflow.com/questions/4019533/slideshow-in-fancybox-image-gallery
    – Daniel Trebbien Aug 28 '11 at 16:34
  • hmnn...it seems that for now, the slideshow no longer working while just now it still worked sometimes... My eventual solution, I would prefer fancybox picture gallery just like how Google Plus does with their Google+ So, I guess I'm switching to them since it still couldn't work properly with this Cycle plugin. But thumbs up for your great help ! ;) – MrCooL Aug 28 '11 at 16:50