views:

56

answers:

5

Hi folks, I'm using the very nice PhotoSlider script from http://opiefoto.com/articles/photoslider to create a slide show of images for one of my clients.

This script replaces a previous hand-coded Javascript solution that allowed for the large image to be clicked resulting in a lightbox modal popup showing the full-size version of the clicked picture.

Of course the client insists that this functionality remain intact, but the HTML code for the large image is generated on-the-fly by the PhotoSlider script itself.

This means I'll need to modify the script slightly to attach a class ("lightbox") and an href (or just a click event, whichever makes more sense), but I'm not quite sure how best to accomplish this. I figure the event and class will have to be attached each time a thumbnail is clicked, but if this isn't the best way to do it any advice will be appreciated.

The script is implemented into my page as can be seen here, without the click or class. I'd really appreciate any assistance stackoverflowites can offer.

Thanks in advance!

+1  A: 

After the thumbnail is clicked, and the new image is shown, you can attach a click event to the new image using jQuery's bind. If you find you need to remove it later, you can use unbind.

Jonathan Julian
+1  A: 

Wait for the script to finish generating its HTML code and then modify it to your taste. Modification on each click is not needed as PhotoSlider generates its HTML once.

Ivo Sabev
+2  A: 

Take a look at the jquery live() method, which allows you to attach an event to a selector that exists either now or in the future. Effectively, you can do something like (based on the example at the photoslider site):

$(document).ready(function() { 
  $('.photoslider_main img').live('click', function() {
    $(this).showLightbox();
  });
});
gregmac
Thanks Greg! I've added another issue I'm having with this below, if you might be willing to take a look please?
Frank Bailey
I don't understand why when I use var src = $(".photoslider_main img").attr("src"); and then alert the resulting variable "src", I keep getting "undefined"? I'm calling this *after* photoslider sets up its structures, so surely it should work?
Frank Bailey
are you doing it within a document.ready()? If it's just "after" according to code flow (eg, after you call the photoslider stuff) that may not mean it really is done at that point, since it is possibly waiting for events etc at that point. (a very common error when starting out with javascript and libraries like jquery). Try doing console.log($('.photoslider_main img')) and see what you get (you'll need firebug installed, of course)
gregmac
I am doing it within a $(document).ready(function(){ block, and when I do the console.log as per your suggestion I get [img] on a line in my Firebug console - does that mean anything to you? I've added my complete code in an additional answer below, maybe there's something I'm missing. Thanks again for your help on this, Greg.
Frank Bailey
A: 

The live() method suggested by GregMac looks like it's going to be very useful. I also found another question on here that suggests an approach that closely matches the way I conceptualised the solution:

$(".photoslider_main img").each(function() {
    var src = $(this).attr('src').replace('pn.','.');
    var a = $('<a/>').attr('href', src);
    $(this).wrap(a);
});

Only problem is it tells me "$(this).attr("src") is undefined"

Any ideas?

Frank Bailey
A: 

Here's the full code that results in the variable 'src' being undefined -- not sure why.

<div class="photoslider" id="default"></div>

<script type="text/javascript">
$(document).ready(function(){
    //change the 'baseURL' to reflect the host and or path to your images
    FOTO.Slider.baseURL = '';

    //set images by filling our bucket directly
    FOTO.Slider.bucket = {
        'default': {
            <% if imgs1 <> "" then %> 0: {'thumb': '<%=replace(imgs1,"pn.","tn.")%>', 'main': '<%=imgs1%>'}<% end if %><% if imgs2 <> "" then %>,
            1: {'thumb': '<%=replace(imgs2,"pn.","tn.")%>', 'main': '<%=imgs2%>'}<% end if %><% if imgs3 <> "" then %>,
            2: {'thumb': '<%=replace(imgs3,"pn.","tn.")%>', 'main': '<%=imgs3%>'}<% end if %><% if imgs4 <> "" then %>,
            3: {'thumb': '<%=replace(imgs4,"pn.","tn.")%>', 'main': '<%=imgs4%>'}<% end if %><% if imgs5 <> "" then %>,
            4: {'thumb': '<%=replace(imgs5,"pn.","tn.")%>', 'main': '<%=imgs5%>'}<% end if %>
        }
    };
    FOTO.Slider.reload('default');  
    FOTO.Slider.preloadImages('default');  
    FOTO.Slider.enableSlideshow('default');  

    //or set our images by the bucket importer
    //var ids = new Array(0,1,2,3);
    //FOTO.Slider.importBucketFromIds('default',ids);


    console.log($('.photoslider_main img'));
    var src = $(".photoslider_main img").attr("src");
    $(".photoslider_main img").wrap($('<a/>').attr('href', 'waka').attr('class','lightbox'));

    $('a.lightbox').lightBox();

 // $('.photoslider_main img').live('click', function() {
 //   $(this).showLightbox();
 // });
});



</script> 
Frank Bailey