gpt4 book ai didi

javascript - JS 照片库。显示大图像并添加可点击的缩略图

转载 作者:行者123 更新时间:2023-11-28 06:05:43 25 4
gpt4 key购买 nike

我有一个快速问题需要帮助,希望新的眼光能够查明我的问题。我有一个照片库,我想在单击时显示更大的缩略图图像。缩略图在底部显示良好,但不可单击,并且主图像(或更大的图像)不会显示。我正在使用外部 .js 文件。我也会上传 HTML,也许有人可以给我指出正确的方向并帮助我理解我所忽略的内容。

<!doctype html>

<html>


<head>
<title>Photo Gallery Final Project</title>
<meta charset = "UTF-8">
<link rel = "stylesheet" href = "gallery.css">


</head>

<body>

<div class = "main">

<div class = "wrapper">

<div id = "largeImage">

<img src = "images/machine_1_big.jpg" alt = "machining image" width = "920" height = "400" id = "myImage" class = "border"/>

</div>

<div class = "thumbnail">

<img src="machine_1.jpg" alt = "machining lathe" id="machine_1"/>
<img src="machine_2.jpg" alt = "machining lathe" id="machine_2"/>
<img src="machine_3.jpg" alt = "machining lathe" id="machine_3"/>
<img src="machine_4.jpg" alt = "machining lathe" id="machine_4"/>
<img src="machine_5.jpg" alt = "machining lathe" id="machine_5"/>
<img src="machine_6.jpg" alt = "machining lathe" id="machine_6"/>

</div>
</div>
</div>

<script src = "gallery.js"></script>


</body>




</html>

//this will load the gallery in the browser and enable the gallery function
//window.onload = gallery;
//gallery funtion declared
function gallery(){
//variable allGalleryImages created. This is where all the images will be held
var allGalleryImages = document.images;
//for loop declared for the image array
for(var i = 0; i < allGalleryImages.length; i++){
if(allGalleryImages[i].id.indexOf("small") > -1){
//this will add a listener to the thumb images
allGalleryImages[i].onclick = imgChanger;

}
}
}
//this is the image changer function
function imgChanger(){
//this will change the src attribute value of the large image.
//document.getElementById('myImage').src ="images/"+this.id+"_big.jpg";
document.getElementById('myImage').src = " " + this.id +"_big.jpg";

}

最佳答案

我使用了 JQuery。不必更改您的 HTML。并且没有理由制作和排列所有缩略图。

$(document).ready(function() {
$(".thumbnail img").on("click", function() {
imgChanger($(this).attr("src"));
})


//this is the image changer function
function imgChanger(theSRC) {
//this will change the src attribute value of the large image.
//document.getElementById('myImage').src ="images/"+this.id+"_big.jpg";
var beforeJPG = theSRC;
console.log(beforeJPG);
beforeJPG = beforeJPG.substring(0, beforeJPG.length-4);
console.log(beforeJPG + "_big.jpg");
$("myImage").attr("src", beforeJPG + "_big.jpg");
}
})

fiddle :https://jsfiddle.net/6v8arhp2/

  1. 获取点击图片的src
  2. 剪掉“.jpg”
  3. 在前面添加“images/”,在后面添加“_big.jpg”
  4. 更改#myImage的src

** 当然,您的图像不在 JSFiddle 内部,因此您看不到它工作,但控制台日志显示转换。

关于javascript - JS 照片库。显示大图像并添加可点击的缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36874010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com