【问题标题】:Basic jQuery image gallery with thumbnails带有缩略图的基本 jQuery 图像库
【发布时间】:2012-02-10 09:41:48
【问题描述】:

我有一个基本图片库,其中包含四个缩略图和四个大图像,其中一个显示,我希望能够单击每个缩略图并将活动大图像替换为之前单击的相应大图像我能够更改源的单个部分,如下所示,但此选项将不再起作用,因为我不再依赖图像的 src:

jQuery('#thumbs').delegate('img','click', function() {
    jQuery('#panel img').attr('src', jQuery(this).attr('src').replace('99_99','600_399'));
});

我已将四个图像和缩略图中的每一个动态添加到两个数组中:

var large_photos = jQuery('#panel').find('img').map(function(){
    return jQuery(this).attr('src');
}).get();

var thumbnails = jQuery('#thumbs').find('img').map(function(){
    return jQuery(this).attr('src');
}).get();

如何修改脚本,以便我可以单击缩略图并显示更大的图像?

HTML:

<div id="panel">
    <img src="/cache/data/691294/IMGP1617_600_399_s_c1.JPG" alt="" width="600" height="399" />
    <img src="/cache/data/691294/IMGP1618_600_399_s_c1.JPG" alt="" width="600" height="399" style="display: none;" />
    <img src="/cache/data/691294/IMGP1619_600_399_s_c1.JPG" alt="" width="600" height="399" style="display: none;" />
    <img src="/cache/data/691294/IMGP1620_600_399_s_c1.JPG" alt="" width="600" height="399" style="display: none;" />
</div>

<div id="thumbs">
    <img src="/cache/data/691294/IMGP1617_99_99_c1.JPG" alt="" width="99" height="99" />
    <img src="/cache/data/691294/IMGP1618_99_99_c1.JPG" alt="" width="99" height="99" />
    <img src="/cache/data/691294/IMGP1619_99_99_c1.JPG" alt="" width="99" height="99" />
    <img src="/cache/data/691294/IMGP1620_99_99_c1.JPG" alt="" width="99" height="99" />
</div>

【问题讨论】:

    标签: jquery


    【解决方案1】:

    这使用被点击的拇指的索引将一个类添加到大图像的相应索引中。

    $('#thumbs img').click( function() {
        var thumbindex = $(this).index();
        $('.active').removeClass('active');
        $('#panel img').eq(thumbindex).addClass('active');
    });
    

    【讨论】:

    • 我已经对此进行了测试,并且效果很好,谢谢,当将活动类分配给图像时,是否有任何方法可以从所有其他图像中删除活动类,目前它保留了该类所有图片?
    • 在添加活动类之前调用​​$('.active').removeClass('active');
    猜你喜欢
    • 2015-10-02
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多