javascript - 为什么我的 js 在导航到视图之前会导致数组的最后一个图像出现?
问题描述
到目前为止,我的代码在单击它时成功地从一个图像转到另一个图像。
但是,我面临的问题是,一旦您到达最后一张图片并单击(将用户重定向到indexTwo.html
),它会将用户带回到第一张图片大约 2 秒钟,然后重定向到indexTwo.html
.
我的问题是:我将如何防止这种行为发生?换句话说:点击所有图片,点击最后一张图片后,将用户重定向到indexTwo.html
根本看不到第一张图片。
注意:我知道if (imageId === 0) {...}
语句是罪魁祸首,但不确定如何修复它。
let theImage = document.getElementById('the-image');
let index = [
"http://tech21info.com/admin/wp-content/uploads/2013/03/chrome-logo-200x200.png",
"https://cdn.tutsplus.com/net/uploads/legacy/155_drupal/200x200.png",
"https://townandcountryremovals.com/wp-content/uploads/2013/10/firefox-logo-200x200.png"
];
let op = 1;
let imageId = 0;
let clickedImage = () => {
// start animation opacity
if(op === 1) {
let timer = setInterval(() => {
if(op <= 0.1) {
// load the next image
imageId = (1 + imageId) % index.length;
theImage.src = index[imageId];
// reset the opacity
theImage.style.opacity = op = 1;
clearInterval(timer);
if (imageId === 0) {
window.location = "indexTwo.html";
}
} else {
op -= 0.1;
theImage.style.opacity = op;
}
}, 50)
}
};
解决方案
您是否需要验证 imageId 和数字 0 的数据类型匹配?如果没有,请尝试使用 == 而不是 ===
推荐阅读
- android - FrameLayout 中的 RecyclerView 与其他元素重叠
- c# - 在 Windows 上使用 Embeddinator 4000 从 .NET 标准项目生成 Objective-C 框架
- php - 使用 nextSyncToken 作为 listCalendarList 或 listEvents 中的 syncToken 的选项返回相同的结果
- c# - 如何将 Windows 窗体的背景设置为与用户的桌面墙纸相同?
- java - 新的 Google Places SDK 不会在 Android 中获取用户选择
- jekyll - 我的 Jekyll 帖子在生产中抛出 404 错误
- youtube-data-api - publishedAfter 参数现在似乎被破坏了
- installation - 使用 CustomAction 卸载 Wix 的问题
- java - java - 如何在java中以某个角度向前移动形状?
- matlab - 如何在此图像中找到边界表面