gpt4 book ai didi

javascript - 创建一个在鼠标悬停时更改图片的事件

转载 作者:行者123 更新时间:2023-11-30 05:41:46 25 4
gpt4 key购买 nike

我正在尝试创建一个在鼠标悬停时更改图片的事件。

var myImage = document.getElementById("mainImage");

var imageArray = ["_images/overlook.jpg","_images/winery_sign.jpg","_images/lunch.jpg",
"_images/bigSur.jpg","_images/flag_photo.jpg","_images/mission_look.jpg"];
var imageIndex = 0;

mainImage.mousover = function changeImage() {
myImage.setAttribute("src",imageArray[imageIndex]);
imageIndex++;
if (imageIndex >= imageArray.length) {
imageIndex = 0;
}
}

// setInterval is also in milliseconds

mainImage.mousover = function () {
var intervalHandle = setInterval(changeImage,1000);
}

myImage.onclick = function() {
clearInterval(intervalHandle);
};

mainImage 是 HTML 中的一个对象,changeImage 函数改变当前图片。

我应该如何更改我的代码以使其工作?

最佳答案

删除了中间只有“mainImage.mousover =”的行

var myImage = document.getElementById("mainImage");

var imageArray = ["_images/overlook.jpg","_images/winery_sign.jpg","_images/lunch.jpg",
"_images/bigSur.jpg","_images/flag_photo.jpg","_images/mission_look.jpg"];
var imageIndex = 0;



function changeImage() {
myImage.setAttribute("src",imageArray[imageIndex]);
imageIndex++;
if (imageIndex >= imageArray.length) {
imageIndex = 0;
}
}

// setInterval is also in milliseconds

mainImage.onmousover = function () {

var intervalHandle = setInterval(changeImage,1000);

}

myImage.onclick = function() {
clearInterval(intervalHandle);
};

关于javascript - 创建一个在鼠标悬停时更改图片的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20419265/

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