I'm trying to rotate a set of 6 images in javascript, not jquery. The first image shows up, but the rest of them don't rotate. Here's my code:
<html>
<head>
<script type="text/javascript">
function rotatePic() {
var qutAd = document.getElementById("yumOreos");
var imgs = ["images/img1.png", "images/img2.png", "images/img3.png", "images/img4.png", "images/img5.png", "images/img6.png"];
var ad = 0;
qutAd.src = imgs[ad];
setInterval(function () {ad++;if (ad == imgs.length) {ad = 0;}}, 4000);
}
</script>
</head>
<body onload="rotatePic()">
<img id="yumOreos">
</body>
</html>
What am I doing wrong? Thanks.
You have to set the src
in the interval function as well.
setInterval(function () {
ad++;
if (ad == imgs.length) ad = 0;
qutAd.src = imgs[ad];
}, 4000);
You have forgotten to change the index of the array, to change the source. So you can do this
setInterval(function () {if (ad == imgs.length) {ad = 0;} qutAd.src =imgs[ad++]}, 4000);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.