最好的 jQuery“单击缩略图并更改主图像”模块是什么?
-
03-07-2019 - |
题
这是我所拥有的(全部都是动态生成的,如果这有影响的话):
- 图像列表
- 每张图片的标题
- 每个图像的缩略图
该页面应加载一张全尺寸图像和所有缩略图。当用户单击缩略图时,全尺寸图像会显示该新图像及其标题。如果他们单击另一个缩略图,图片(和标题)会再次更改。
这不是很复杂。几个月前我编写了一个解决方案,但我需要再做一次,我看着这个蹩脚的代码,并认为必须有一个更好的方法(并且知道 jQuery,其他人已经做到了,并且完成了)好吧)。
想法?链接?
谢谢!
其他提示
这里的大多数答案就像用大炮打苍蝇一样!!
$('#thumbs img').click(function(){
$('#largeImage').attr('src',$(this).attr('src').replace('thumb','large'));
$('#description').html($(this).attr('alt'));
});
这就是你所需要的..4行代码。
看这里 : 四行画廊
建立在 krembo99 的答案他链接在这里, ,我想分享我的解决方案,因为当我的客户请求这样的功能时,我已经上传了数百张照片。考虑到这一点,通过在提供的代码中添加几行额外的代码,我能够获得适合我的参数的解决方案。
我还处理较小的图像,因此我无需创建同一文件的小版本和大版本。
$('.thumbnails img').click(function(){
// Grab img.thumb class src attribute
// NOTE: ALL img tags must have use this class,
// otherwise you can't click back to the first image.
var thumbSrc = $('.thumb').attr('src');
// Grab img#largeImage src attribute
var largeSrc = $('#largeImage').attr('src');
// Use variables to replace src instead of relying on file names.
$('#largeImage').attr('src',$(this).attr('src').replace(thumbSrc,largeSrc));
$('#description').html($(this).attr('alt'));
});
这是我的 HTML 的外观。
<img src="path/to/file1.jpg" id="largeImage" class="thumb">
<div id="description">1st image Description</div>
<div class="thumbnails">
<img src="path/to/file1.jpg" class="thumb" alt="1st image description">
<img src="path/to/file2.jpg" class="thumb"alt="2nd image description">
<img src="path/to/file3.jpg" class="thumb" alt="3rd image description">
<img src="path/to/file4.jpg" class="thumb" alt="4th image description">
<img src="path/to/file5.jpg" class="thumb" alt="5th image description">
</div>
http://bradblogging.com/jquery/9-jquery-slideshow-applications-you-cannot-miss/
一个包含 9 个不同照片幻灯片的 jQuery 页面可供使用
查看我的画廊实施: 埃塞克斯画廊的花园设计园丁
尝试 加勒里菲克, ,它拥有您需要的一切。
很多这些脚本都已经过时了,对我来说不起作用,而且需要一组不同的缩略图图像。我认真地寻找了一下,发现了一些非常轻的东西,它是纯 js,不需要 jquery。
<html>
<head>
<style>
* {margin:0; padding:0; border:0; outline:0; box-sizing:border-box;}
.image, .thumb {width:100%; height:100%;}
#product-image-wrap {position:relative; float:left; width:250px; height:325px; margin:50px 0 50px 50px;}
#product-image {position:relative; float:left; width:250px; height:250px; border:1px solid #d0d0d0; padding:20px; cursor:pointer; display:inline-block; transition:0.9s;}
#product-image:hover {opacity:0.7;}
.product-image-thumbnail {position:relative; float:left; width:55px; height:55px; border:1px solid #d0d0d0; margin-top:20px; padding:10px; cursor:pointer; display:inline-block; transition:0.9s;}
.product-image-thumbnail:hover {opacity:0.7;}
.product-image-thumbnail-spacer {position:relative; float:left; width:10px; height:130px;}
</style>
</head>
<body>
<div id='product-image-wrap'>
<!-- Main -->
<div id='product-image'><img src='http://workshop.rs/demo/gallery-in-4-lines/images/image_01_large.jpg' id='0' class='image' alt='image 0'></div>
<!-- Thumbs -->
<div class='product-image-thumbnail'><img src='http://workshop.rs/demo/gallery-in-4-lines/images/image_01_large.jpg' id='1' class='thumb' onclick='preview(this)' alt='image 0'></div>
<div class='product-image-thumbnail-spacer'></div>
<div class='product-image-thumbnail'><img src='http://workshop.rs/demo/gallery-in-4-lines/images/image_02_large.jpg' id='2' class='thumb' onclick='preview(this)' alt='image 2'></div>
<div class='product-image-thumbnail-spacer'></div>
<div class='product-image-thumbnail'><img src='http://workshop.rs/demo/gallery-in-4-lines/images/image_03_large.jpg' id='3' class='thumb' onclick='preview(this)' alt='image 3'></div>
<div class='product-image-thumbnail-spacer'></div>
<div class='product-image-thumbnail'><img src='http://workshop.rs/demo/gallery-in-4-lines/images/image_04_large.jpg' id='4' class='thumb' onclick='preview(this)' alt='image 4'></div>
</div>
<!-- Javascript -->
<script>
var lastImg = 1;
document.getElementById(lastImg).className = "thumb selected";
function preview(img) {
document.getElementById(lastImg).className = "thumb";
img.className = "thumb selected";
document.getElementById(0).src = img.src;
lastImg = img.id;
}
</script>
</body>
</html>
不隶属于 StackOverflow