开发者

Issues with simple jQuery image gallery with Colorbox plugin

开发者 https://www.devze.com 2023-01-03 14:23 出处:网络
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 shou

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>
          <开发者_C百科/ul>
        </div>

Any help would be much appreciated!


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;
});


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;
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号