简体   繁体   English

单击缩略图以更改图库中的大图像

[英]Clicking a thumbnail to change the large image in a gallery

I'm trying to create the following gallery: 我正在尝试创建以下画廊:
- One large image -一幅大图
- Thumbnails of gallery images below -下面的画廊图像的缩略图
- Large image should open all images on click in a lightbox gallery -大图片应在灯箱图库中单击后打开所有图片

I have the lightbox gallery working using PhotoSwipe and it fires when I click the large image. 我有使用PhotoSwipe运作的灯箱图库,当我点击大图片时会触发。 I also have the thumbnails in place below the large image. 我还在大图像下方放置了缩略图。 My question now is how do I change the large image when I click one of the thumbnails? 我现在的问题是单击缩略图之一时如何更改大图像? I've seen a lot of examples (also quite simple ones), but none of them seem to work in my case. 我已经看到了很多示例(也很简单),但是在我看来,这些示例都不起作用。

Here's the code that I have for the thumbnail: 这是缩略图的代码:

<a href="<?php echo $image['url']?>" data-size="<?php echo $image['width']?>x<?php echo $image['height']?>" data-index="0">
  <img src="<?php echo $image['url']?>">
</a>

What I want is that when I click the href of the thumbnail that it changes the big image, which is display with this code: 我想要的是,当我单击缩略图的href时,它会更改大图像,并显示以下代码:

<img class="bigimg" src="imageurl.jpg">

The thumbnail needs to have the href tag because this is required for the lightbox function to work. 缩略图需要具有href标记,因为灯箱功能需要此标记。

I've seen some jQuery examples that replace the src of the bigimg with the src of the thumbnail, but I can't quite get it to work with the href also in place. 我已经看到一些jQuery示例,它们用缩略图的src替换了bigimg的src,但是我不能完全将它与href一起使用。

For reference, this is the situation. 供参考, 这是情况。

Example: JSFiddle 示例: JSFiddle

I'm not intimately familiar with lightbox, but if I understand you correctly, you want it to just do what it's already doing, but in addition, change the src of your img.bigimg . 我对lightbox并不是很熟悉,但是如果我正确理解了lightbox,则希望它只做已经做的事情,而且还要更改img.bigimg的src。 If that's the case, it should work to add your own listener on the a tag as long as you don't prevent the default action. 如果是这样的话,它应该工作,以增加自己的听众在a标签,只要你不阻止默认动作。 Something along the lines of this: 与此类似:

var thumbnailLinks = $('a.thumbnailLink') // Add class="thumbnailLink" where appropriate or use a different selector.
thumbnailLinks.on('click', function() {
    $('img.bigimg').attr('src', $(this).attr('href')); // Set img.bigimg src attribute to the href attribute of the link that was clicked.
});

There may be some weaknesses to this. 这可能有一些缺点。 For example I'm not sure if this will work if a user tabs through links and activates it using the enter key, though it should be possible to add other events than just click to help with that. 例如,我不确定如果用户通过链接制表并使用enter键激活它是否可以工作,尽管除了click以帮助之外,还可以添加其他事件。 This is also untested code at the moment, but have a go and see if it works for you. 目前,这也是未经测试的代码,但请试试看它是否对您有用。

Try, 尝试,

var thumbnailLinks = $('a.thumbnailLink') // Add class="thumbnailLink" where appropriate or use a different selector.
$('.thumbnailLink').click(function() {
  $('.big a').attr('href', $(this).attr('href'));
  $('.bigimg').attr('src', $(this).attr('href')); 
});

here is the fiddle https://jsfiddle.net/91oq8ja2/2/ 这是小提琴https://jsfiddle.net/91oq8ja2/2/

Is this what you are looking for? 这是你想要的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM