gpt4 book ai didi

javascript - 如何在循环中触发 onclick 事件时重置变量值

转载 作者:太空宇宙 更新时间:2023-11-04 00:56:43 24 4
gpt4 key购买 nike

我有几个涵盖不同类别的图片库,在下面的代码中我只包含 2 个(自然和城市)以使其更易于阅读。我在 onclick 事件之前使用带有“let”的“for”循环,以便可以单击 HTML 集合中的任何节点,我将获得索引。然后我将该索引应用于一组相同的图像,除了它们是高清图像而不是缩略图。

“rightArrow”和“leftArrow”div 用于通过将索引增加或减少一个来移动到数组中的下一个或上一个图像,非常标准。问题是当我退出全屏模式时,索引更改尚未重置。因此,当用户单击同一图库中的另一张图片以重新打开全屏模式时,他们不会获得相应的图片。

那么如何在触发“natureThumbnailImgs”和“urbanThumbnailImgs”的 onclick 事件之前重置索引?或者在重置索引的“exitFullscreen”事件中包含一些东西?

非常感谢任何帮助,

史蒂夫

var fullscreen = document.getElementsByClassName("fullscreen");
var fullImg = document.getElementById("fullImg");
var exitFullscreen = document.getElementById("exitFullscreen");

var rightArrow = document.getElementById("rightArrow");
var leftArrow = document.getElementById("leftArrow");

exitFullscreen.onclick = function() {
fullscreen[0].removeAttribute("id", "showFullscreen");
}

var natureThumbnailImgs = document.getElementsByClassName("natureThumbnailImgs");
var natureHDImgs = ["1 nature HD.jpg", "2 nature HD.jpg", "3 nature HD.jpg"];

// NATURE GALLERY ALBUM
for (let albumIndex = 0; albumIndex < natureThumbnailImgs.length; albumIndex++) {
natureThumbnailImgs[albumIndex].onclick = function() {
//DISPLAY FULLSCREEN
fullscreen[0].setAttribute("id", "showFullscreen");
//APPLY CHOSEN IMAGE
fullImg.src = "../../../Image Bank/" + natureHDImgs[albumIndex];

//NEXT
rightArrow.onclick = function() {
if (albumIndex < (natureThumbnailImgs.length - 1)) {
albumIndex++;
//APPLY CHOSEN IMAGE
fullImg.src = "../../../Image Bank/" + natureHDImgs[albumIndex];
}
}

//PREV
leftArrow.onclick = function() {
if (albumIndex > 0) {
albumIndex--;
//APPLY CHOSEN IMAGE
fullImg.src = "../../../Image Bank/" + natureHDImgs[albumIndex];
}
}
}
}

var urbanThumbnailImgs = document.getElementsByClassName("urbanThumbnailImgs");
var urbanHDImgs = ["1 urban HD.jpg", "2 urban HD.jpg", "3 urban HD.jpg"];

// URBAN GALLERY ALBUM
for (let albumIndex = 0; albumIndex < urbanThumbnailImgs.length; albumIndex++) {
urbanThumbnailImgs[albumIndex].onclick = function() {
//DISPLAY FULLSCREEN
fullscreen[0].setAttribute("id", "showFullscreen");
//APPLY CHOSEN IMAGE
fullImg.src = "../../../Image Bank/" + urbanHDImgs[albumIndex];

//NEXT
rightArrow.onclick = function() {
if (albumIndex < (urbanThumbnailImgs.length - 1)) {
albumIndex++;
//APPLY CHOSEN IMAGE
fullImg.src = "../../../Image Bank/" + urbanHDImgs[albumIndex];
}
}

//PREV
leftArrow.onclick = function() {
if (albumIndex > 0) {
albumIndex--;
//APPLY CHOSEN IMAGE
fullImg.src = "../../../Image Bank/" + urbanHDImgs[albumIndex];
}
}
}
}
[class$="ThumbnailImgs"] {
width: 100px;
height: auto;
}

#fullscreen {
position: absolute;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
z-index: 1;
}

#fullscreen {
position: absolute;
top: 50px;
right: 50px;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Image Gallery</title>
<link href="css/IG.css" rel="stylesheet" type="text/css"/>
</head>
<body>

<!-- NATURE ALBUM -->
<div id="natureAlbum">
<img class="natureThumbnailImgs" src="../../../Image Bank/1 nature thumbnail.jpeg"/>
<img class="natureThumbnailImgs" src="../../../Image Bank/2 nature thumbnail.jpeg"/>
<img class="natureThumbnailImgs" src="../../../Image Bank/3 nature thumbnail.jpeg"/>
</div>

<!-- URBAN ALBUM -->
<div id="urbanAlbum">
<img class="urbanThumbnailImgs" src="../../../Image Bank/1 urban thumbnail.jpeg"/>
<img class="urbanThumbnailImgs" src="../../../Image Bank/2 urban thumbnail.jpeg"/>
<img class="urbanThumbnailImgs" src="../../../Image Bank/3 urban thumbnail.jpeg"/>
</div>

<!-- FULL IMG DISPLAY -->
<div id="fullscreen">
<div id="exitFullscreen">X</div>
<img id="fullImg" src=""/>
<div id="leftArrow">Left Arrow Symbol</div>
<div id="rightArrow">Right Arrow Symbol</div>
</div>


</body>
</html>

最佳答案

首先尝试不要更新相册索引 - 每次单击缩略图时都复制它。单个画廊的模式可能如下所示:

// NATURE GALLERY ALBUM
for (let albumIndex = 0; albumIndex < natureThumbnailImgs.length; albumIndex++) {
natureThumbnailImgs[albumIndex].onclick = function() {
index = albumIndex; // copy the base album index
//DISPLAY FULLSCREEN
fullscreen[0].setAttribute("id", "showFullscreen");
//APPLY CHOSEN IMAGE
fullImg.src = "../../../Image Bank/" + natureHDImgs[index];

//NEXT
rightArrow.onclick = function() {
if (index < (natureThumbnailImgs.length - 1)) {
index++;
//APPLY CHOSEN IMAGE
fullImg.src = "../../../Image Bank/" + natureHDImgs[index];
}
}

//PREV
leftArrow.onclick = function() {
if (index > 0) {
index--;
//APPLY CHOSEN IMAGE
fullImg.src = "../../../Image Bank/" + natureHDImgs[index];
}
}
}
}

这至少应该产生与发布的代码不同的行为。

关于javascript - 如何在循环中触发 onclick 事件时重置变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54543322/

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