简体   繁体   English

javascript交换缩略图较大的图像

[英]javascript swap thumbnail with larger image

Im pretty new to web programming and im working on a site now. 我是网络编程的新手,现在正在网站上工作。 In one part of this site, I have collections of 3 pictures. 在该站点的一部分中,我收藏了3张图片。 1 larger one and two smaller thumbnails below it. 1个较大的缩略图和2个较小的缩略图。 The goal is to create a way in which i can click on one of the thumbnails and they swap spots with the one large picture. 目标是创建一种我可以单击其中一个缩略图并将它们与一张大图片交换点的方法。 any idea how I would go about doing this? 知道我将如何去做吗? Heres a snippet of code. 这是一段代码。 Thanks! 谢谢!

<div class = 'picture-container'>
            <div class = 'large-picture' id = 'lp1'>
                <figure style = 'float:left;width:45%;'>
                    <img src = 'close_table_dupontstudios.png' width = '100%' height = '100%' class = 'no-mobile'>
                    <figcaption class = 'red-cap'>Our Set-Up</figcaption>
                </figure>
                <div class = 'picture-content'>
                    <div class = 'picture-title'>BOUTIQUE PRODUCTION STUDIO</div>
                    <div class = 'picture-text'>We built a boutique full service production studio that allows for one, two and three person filmed interviews and conversations. We have studio lights, a three camera set-up and remote monitoring. Additionally, our Infinity Wall creates a clean and professional look that allows the film to be about the message.</div>
                    <!--<div class = 'small-picture'>
                        <img src = 'hair_and_makeup_dupontstudios.png' width = '175' height = '100'>
                    </div>
                    <div class = 'small-picture'>
                        <img src = 'infinity_wall_dupontstudios.png' width = '175' height = '100'>
                    </div>-->
                </div>
                <div class = 'thumbnail-container'>
                    <figure class = 'thumbnail'>
                        <img src = 'infinity_wall_dupontstudios.png' width = '100%' height = '100%'>
                    </figure>
                    <figure class = 'thumbnail'>
                        <img src = 'infinity_wall_dupontstudios.png' width = '100%' height = '100%'>
                    </figure>
                </div>
            </div>
        </div>

There are many ways to solve this problem. 有很多方法可以解决此问题。 The easiest way is to dump all your images (large and small) and only show one at a time. 最简单的方法是转储所有图像(大和小),一次只显示一个。

So in the source code all your large images except the first one will have a class of hidden , which makes them display: none . 因此,在源代码中,除第一个图像外,所有大图像都将具有hidden类,这使它们display: none And then it's just a matter of showing the right large image when the thumbnail is clicked. 然后,单击缩略图时只显示正确的大图像即可。

To show the right large image you need to associate the thumbnails with the large image by an identifier. 要显示正确的大图像,您需要通过标识符将缩略图与大图像相关联。 Below is an example of setting the href of a thumbnail link to the large image id. 以下是将缩略图链接的href设置为大图片ID的示例。

<a href="#lp1">
  <figure class="thumbnail">...</figure>
</a>

Now you add the javascript (jQuery). 现在,您添加了javascript(jQuery)。

// preselect all large images
var largeImages = $('figure.large');
// add handler for thumbnail clicks
$('.thumbnail-container').on('click', 'a', function (e) {
    e.preventDefault();
    var thumbnailLink = $(this),
        selectedLarge = $(thumbnailLink.attr('href'));
    // hide all the large images
    largeImages.addClass('hidden');
    // show the large image that corresponds to the clicked thumbnail
    selectedLarge .removeClass('hidden');
});

So, the easies way is hide/show, but this means is not the most efficient. 因此,简便的方法是隐藏/显示,但这并不是最有效的方法。 It makes the client load all the images even if they are hidden. 即使隐藏图像,它也使客户端加载所有图像。

A more efficient approach is to add data- attributes in the thumbnails and inside the thumbnail click handler update the large content area with the data from the clicked thumbnail.To "replace" the image you just need to replace src attribute. 一种更有效的方法是在缩略图中添加data-属性,然后在缩略图单击处理程序内部使用单击的缩略图中的数据更新较大的内容区域。要“替换”图像,您只需要替换src属性即可。

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

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