views:

51

answers:

2

I'm putting together an image gallery for an ecommerce site and wanting to use colorbox to launch larger images. My problem is that image launched in colorbox stays as the first one launched and should reflect the image shown as img#bigpic - the link to the image does appear to be updating correctly.

Here's the jQuery I have:

$(document).ready(function(){

$("#largeimage").colorbox();
imageSwapper(".thumbnails a");


function imageSwapper(link) {
$(link).click(function(){
$("#bigpic").attr("src", this.href);
$("#largeimage").attr("href", this.rel);
return false;
});
};


$("#largeimage").bind('mouseenter mouseleave', function(event) {
        $("#largeimage span").toggleClass('showspan');
    });

});

...and the HTML

<a href="_images/products/large/bpn0001_1.jpg" id="largeimage"><span></span><img src="_images/products/bpn0001_1.jpg" id="bigpic" /></a>
        <div class="thumbnails">
            <ul>
                <li><a href="_images/products/bpn0001_1.jpg" rel="_images/products/large/bpn0001_1.jpg"><img src="_images/products/mini/bpn0001_1.jpg" /></a></li>
                <li><a href="_images/products/bpn0001_2.jpg" rel="_images/products/large/bpn0001_2.jpg"><img src="_images/products/mini/bpn0001_2.jpg" /></a></li>
                <li><a href="_images/products/bpn0001_3.jpg" rel="_images/products/large/bpn0001_3.jpg"><img src="_images/products/mini/bpn0001_3.jpg" /></a></li>
          </ul>
        </div>

Any help would be much appreciated!

A: 

I don't think you need the imageSwapper() function. Try replacing this:

imageSwapper(".thumbnails a");


function imageSwapper(link) {
 $(link).click(function(){
  $("#bigpic").attr("src", this.href);
  $("#largeimage").attr("href", this.rel);
  return false;
 });
};

With:

$(".thumbnails a").click(function(){
  $("#bigpic").attr("src", $(this).attr("href"));
  $("#largeimage").attr("href", $(this).attr("rel"));
  return false;
});
Pablo
Thanks for that. Much better.
Chris
A: 

Instead of binding the colorbox directly to the link call the color box from clicking on that link and pass in the current href as it would seem it's not rechecking what the current value is.

$("#largeimage").click(function(){
  $.fn.colorbox({href:$(this).attr("href")});
  return false;
});
Jeff Beck
That does the trick - nice one!
Chris
No problem. hope the rest of the site goes well.
Jeff Beck