views:

44

answers:

4

Ok, so I'm loading a list of images by directory using PHP:

<?php
    # fetch image details
    $images = getImages("marc/img/livingrooms/");

    # display on page
    foreach($images as $img) {
        echo "<li><img src=\"{$img['file']}\" title=\"\" alt=\"\"></li>\n";
    }
?>

Then I'm using the Galleria JQuery plugin to style those items into a gallery:

$(document).ready(function(){

    $('.dynolist').addClass('gallery_group'); // adds new class name to maintain degradability

    $('ul.gallery_group').galleria({
        history   : true, // activates the history object for bookmarking, back-button etc.
        clickNext : true, // helper for making the image clickable
        insert    : '#main_image', // the containing selector for our main image
        onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes

            // fade in the image & caption
            image.css('display','none').fadeIn(1000);
            caption.css('display','none').fadeIn(1000);

            // fetch the thumbnail container
            var _li = thumb.parents('li');

            // fade out inactive thumbnail
            _li.siblings().children('img.selected').fadeTo(500,0.3);

            // fade in active thumbnail
            thumb.fadeTo('fast',1).addClass('selected');

            // add a title for the clickable image
            image.attr('title','Next image >>');
        },
        onThumb : function(thumb) { // thumbnail effects goes here

            // fetch the thumbnail container
            var _li = thumb.parents('li');

            // if thumbnail is active, fade all the way.
            var _fadeTo = _li.is('.active') ? '1' : '0.3';

            // fade in the thumbnail when finnished loading
            thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);

            // hover effects
            thumb.hover(
                function() { thumb.fadeTo('fast',1); },
                function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
            )
        }
    });
});

The issue is that I need to give the first item pulled from that list a class of "active", so the first image will be loaded into the slideshow. Everything else works right now, other than getting that first image loaded.

Any suggestions?

Thanks.

+3  A: 
$('selector:first').addClass('active');

Maybe?

Or what about

# display on page
$class = 'class="active" ';
foreach($images as $img) {
    echo "<li><img ".$class."src=\"{$img['file']}\" title=\"\" alt=\"\"></li>\n";
    $class="";
}
Chacha102
No luck there, thanks for trying though. :)
Anders H
Didn't see the edit before testing, this may work but I just took the PHP solution above.
Anders H
@Anders H: This is a PHP solution as well, and a more elegant one at that.
Justin Johnson
+1  A: 

Try this:

# display on page
$counter = 0;
foreach($images as $img) {

  if ($counter == 0) { // first
    echo "<li class=\"active\"><img src=\"{$img['file']}\" title=\"\" alt=\"\"></li>\n";
  }
  else
  {
    echo "<li><img src=\"{$img['file']}\" title=\"\" alt=\"\"></li>\n";
  }

  $counter++;
}
Sarfraz
Works like a charm! Thank you very much.
Anders H
Ummmm, you will need to put $counter++ after the if statement or test for 1...
jeroen
@jeroen: rightly spotted i think, thanks :)
Sarfraz
@Anders H: That is great news :)
Sarfraz
+1  A: 

If you want to do that on the PHP side, you could count, in your foreach loop, how many images have been displayed -- and if 0 have been displayed, add a class="active" to the one you are currently echoing.

For instance, something like this should do the trick :

$counter = 0;
foreach($images as $img) {
    echo "<li><img src=\"{$img['file']}\" ";
    if ($counter === 0) {
        echo 'class="active"';
    }
    echo " title=\"\" alt=\"\"></li>\n";
    $counter++;
}

The first time you'll loop, $counter will be 0, and class="active" will be echoed ; next times, it won't be 0 anymore, and no addtionnal active class will be added.

Pascal MARTIN
+1  A: 

In jquery:

$(".gallery_group li:first-child").addClass("active");

In php:

# display on page
$first = true;
foreach($images as $img) {
    echo "<li " . ($first ? "class='active'" : "") . "><img src=\"{$img['file']}\" title=\"\" alt=\"\"></li>\n";
    $first = false;
}
jeroen