gpt4 book ai didi

javascript - JS 点击事件监听器仅工作一次,并且不能与预期的 if 语句一起工作

转载 作者:行者123 更新时间:2023-12-02 21:18:08 27 4
gpt4 key购买 nike

我正在使用一个库来比较不同的照片,例如“之前和之后”之类的东西,它们显示在另一张之上,并且中间有一个按钮,您可以向左或向右拖动查看差异。

现在,我想做的是实现两个按钮 - 一个在图片的左侧,一个在右侧。当您单击这些按钮时,我希望照片与前两张或后两张发生变化。

我为按钮做了一个 if 语句,但它们无法正常工作,尽管我没有注意到任何明显的错误。

HTML 代码


<div id="wrapper">
<div class="arrowsContainer">
<div id="leftArrow">
<p>Left</p>
</div>
</div>

<div class="cd-image-container">
<img src="" id="originalImage" alt="Original Image">

<div class="cd-resize-img">
<img src="" id="modifiedImage" alt="Modified Image">
</div>

<span class="cd-handle"></span>
</div>

<div class="arrowsContainer">
<div id="rightArrow">
<p>Right</p>
</div>
</div>
</div>

JavaScript 代码


let originalImage = document.querySelector('#originalImage');
let modifiedImage = document.querySelector('#modifiedImage');

document.querySelector('#leftArrow').addEventListener("click", function(){
if ((modifiedImage.src = "img/3.jpg") && (originalImage.src = "img/4.jpg")){
modifiedImage.src = "img/1.jpg";
originalImage.src = "img/2.jpg";
}
});

document.querySelector('#rightArrow').addEventListener('click', function(){
if ((modifiedImage.src = "img/1.jpg") && (originalImage.src = "img/2.jpg")){
modifiedImage.src = "img/3.jpg";
originalImage.src = "img/4.jpg";
}
if ((modifiedImage.src = "img/3.jpg") && (originalImage.src = "img/4.jpg")){
modifiedImage.src = "img/5.jpg";
originalImage.src = "img/6.jpg";
}
if ((modifiedImage.src = "img/5.jpg") && (originalImage.src = "img/6.jpg")){
modifiedImage.src = "img/7.jpg";
originalImage.src = "img/8.jpg";
}
});

现在,如果我打开页面,就会显示照片 1 和 2。如果我点击“右”按钮,它将显示图片 7 和 8,从那里,如果我点击“左”按钮,它将再次显示图片 1 和 2,尽管我在“左”上的条件按钮甚至不包括您位于图 7 和 8 的场景。

请帮忙,我真的不明白发生了什么

最佳答案

我认为您想要实现的目标是,当您单击按钮时,您希望显示接下来的两张图像,当您单击上一页时按钮您想要显示前两张图像。如果是这种情况,那么您可以检查我的解决方案。

这是 js 文件。

let originalImage = document.querySelector("#originalImage");
let modifiedImage = document.querySelector("#modifiedImage");

const totalImages = 8;
let currentCount = 0;
document.querySelector("#leftArrow").addEventListener("click", function() {
currentCount = (currentCount - 2 + totalImages) % totalImages;
originalImage.src = `./img/${currentCount + 1}.jpg`;
modifiedImage.src = `./img/${currentCount + 2}.jpg`;
});

document.querySelector("#rightArrow").addEventListener("click", function() {
currentCount = (currentCount + 2) % totalImages;
originalImage.src = `./img/${currentCount + 1}.jpg`;
modifiedImage.src = `./img/${currentCount + 2}.jpg`;
});

这是 .html 文件

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<!-- <div class="container">Item</div> -->
<div id="wrapper">
<div class="arrowsContainer">
<div id="leftArrow">
<p>Left</p>
</div>
</div>

<div class="cd-image-container">
<img src="./img/1.jpg" id="originalImage" alt="Original Image" />

<div class="cd-resize-img">
<img src="./img/2.jpg" id="modifiedImage" alt="Modified Image" />
</div>

<span class="cd-handle"></span>
</div>

<div class="arrowsContainer">
<div id="rightArrow">
<p>Right</p>
</div>
</div>
</div>

<script src="main.js"></script>
</body>
</html>

let originalImage = document.querySelector("#originalImage");
let modifiedImage = document.querySelector("#modifiedImage");

const totalImages = 8;
let currentCount = 0;
document.querySelector("#leftArrow").addEventListener("click", function() {
currentCount = (currentCount - 2 + totalImages) % totalImages;
originalImage.src = `https://i.picsum.photos/id/${currentCount + 1}/200/300.jpg`;
modifiedImage.src = `https://i.picsum.photos/id/${currentCount + 2}/200/300.jpg`;
});

document.querySelector("#rightArrow").addEventListener("click", function() {
currentCount = (currentCount + 2) % totalImages;
originalImage.src = `https://i.picsum.photos/id/${currentCount + 1}/200/300.jpg`;
modifiedImage.src = `https://i.picsum.photos/id/${currentCount + 2}/200/300.jpg`;
});
#wrapper {
display: flex;
}
.cd-image-container {
display: flex;
}
#leftArrow,
#rightArrow {
cursor: pointer;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<!-- <div class="container">Item</div> -->
<div id="wrapper">
<div class="arrowsContainer">
<div id="leftArrow">
<p>Left</p>
</div>
</div>

<div class="cd-image-container">
<img src="https://i.picsum.photos/id/1/200/300.jpg" id="originalImage" alt="Original Image" />

<div class="cd-resize-img">
<img src="https://i.picsum.photos/id/2/200/300.jpg" id="modifiedImage" alt="Modified Image" />
</div>

<span class="cd-handle"></span>
</div>

<div class="arrowsContainer">
<div id="rightArrow">
<p>Right</p>
</div>
</div>
</div>

<script src="main.js"></script>
</body>
</html>

关于javascript - JS 点击事件监听器仅工作一次,并且不能与预期的 if 语句一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60915793/

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