gpt4 book ai didi

javascript - 如何让按钮更改图像?

转载 作者:太空宇宙 更新时间:2023-11-03 22:40:46 25 4
gpt4 key购买 nike

我制作了一个内部有图像的 div,然后制作了一个按钮。我希望它是这样的,当我按下按钮时,它会在我要添加的多个图像之间切换。

这是按钮和图像的 HTML:

#gallery {
width: 800px;
height: 450px;
position: absolute;
left: 310px;
top: 180px;
padding: 0;
margin: 0;
z-index: -1;
}

#UCL {
position: relative;
width: 100%;
height: 100%;
border-radius: 5px;
}

button {
width: 100px;
height: 60px;
font-family: "Economica";
font-size: 18px;
background-color: #383838;
color: white;
font-weight: bold;
font-size: 23px;
border: 0;
margin-left: 670px;
margin-top: 110px;
border-radius: 12px;
position: absolute;
opacity: 1;
}

button:hover {
background-color: white;
transition: ease 0.7s;
color: black;
}
<button onclick="myFunction()"> 
Next
</button>
<div id=gallery>
<img id="UCL" src="http://byuinsider.com/wp-
content/uploads/2015/09/Champions-League-Stadium-Football-Wallpapers-
HD.jpg" alt="Stadium">
</div>

最佳答案

您可以使用包含图像 url 的数组来完成此操作,如下所示:

var images=['http://placeskull.com/300x300','http://placeskull.com/400x400','http://placeskull.com/500x500'];
var idx=0; // we're at the first image so, 0
function myFunction() {
$('#UCL').attr('src',images[idx]);
idx=(idx+1)%(images.length); // to avoid the index getting bigger than the number of images, we use the modulo operator % to make it circular, 0 1 2 0 1 2 0 1 2 ...
}
myFunction();
window.setInterval(function(){
myFunction();
}, 2000); // 2000ms, change to whatever timing you want
#gallery {
width: 800px;
height: 450px;
position: absolute;
left: 310px;
top: 180px;
padding: 0;
margin: 0;
z-index: -1;
}

#UCL {
position: relative;
width: 100%;
height: 100%;
border-radius: 5px;
}

button {
width: 100px;
height: 60px;
font-family: "Economica";
font-size: 18px;
background-color: #383838;
color: white;
font-weight: bold;
font-size: 23px;
border: 0;
margin-left: 670px;
margin-top: 110px;
border-radius: 12px;
position: absolute;
opacity: 1;
}

button:hover {
background-color: white;
transition: ease 0.7s;
color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="myFunction()" id="nextImage">
Next
</button>
<div id=gallery>
<img id="UCL" src="" alt="Puppy">
</div>

关于javascript - 如何让按钮更改图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44099308/

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