【发布时间】:2021-12-30 22:22:18
【问题描述】:
我有一个关于我正在做的 JS 项目的问题。我不知道JS逻辑不起作用的原因。尝试根据选择的缩略图实现滑动图像。提前致谢。enter image description here
I have attached my VS code and Website result
product.js
const productImages = document.querySelectorAll(".product-images img"); // selecting all image thumbs
const productImageSlide = document.querySelector(".image-slider"); // seclecting image slider element
let activeImageSlide = 0; // default slider image
productImages.forEach((item, i) => {
// loopinh through each image thumb
item.addEventListener("click", () => {
// adding click event to each image thumbnail
productImages[activeImageSlide].classList.remove("active"); // removing active class from current image thumb
item.classList.add("active"); // adding active class to the current or clicked image thumb
productImageSlide.style.backgroundImage = `url('${item.src}')`; // setting up image slider's background image
activeImageSlide = i; // updating the image slider variable to track current thumb
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="product.css" />
<script src="product.js"></script>
<link rel="shortcut icon" href="">
</head>
<body>
<section class="product-details">
<div class="image-slider">
<div class="product-images">
<img src="Images/33.png" class="active" alt="" />
<img src="Images/44.png" alt="" />
<img src="Images/55.png" alt="" />
<img src="Images/6.png" alt="" />
</div>
</div>
</section>
</body>
</html>
【问题讨论】:
-
请将代码嵌入文本和图像。
-
感谢您的评论。我现在已经嵌入了代码。
标签: javascript arrays