所以我在js中有一个小脚本可以随着时间的推移改变图像,我想添加两个按钮,如果有人不想等待,可以跳过图像,但它们并没有真正起作用,点击它们只会让我回来到第一张图片。
这是我找到的用于随时间更改图像的脚本:
<script>
var imageSources = ["image1.png", "image2.png","image3.png"];
var index = 0;
setInterval (function(){
if (index === imageSources.length) {
index = 0;
}
document.getElementById("image").src = imageSources[index];
index++;
} , 2000);
这是我尝试制作的用于在单击时更改它们的脚本:
function changeImage1() {
if (document.getElementById("image").src == "image1.png")
{
document.getElementById("image").src = "image3.png";
}
else if (document.getElementById("image").src == "image2.png")
{
document.getElementById("image").src = "image1.png";
}
else if (document.getElementById("image").src == "image3.png")
{
document.getElementById("image").src = "image2.png";
}
else {}
}
function changeImage2() {
if (document.getElementById("image").src == "image1.png")
{
document.getElementById("image").src = "image2.png";
}
else if (document.getElementById("image").src == "image2.png")
{
document.getElementById("image").src = "image3.png";
}
else if (document.getElementById("image").src == "image3.png"){
document.getElementById("image").src = "image1.png";
}
else {}
}
</script>
和 HTML:
<button name="button1" id="button1" onclick="changeImage1()">
<img src="arrow_left.png" width="50px" height="50px"/>
</button>
<img name="image" id="image" src="image1.png" width="800px" height="300px"/>
<button name="button2" id="button2" onclick="changeImage2()">
<img src="arrow_right.png" onclick="changeImage2()" width="50px" height="50px"/>
</button>
互换的青春
繁星点点滴滴
江户川乱折腾
相关分类