如何让我的 img src 在悬停时在 3-4 张图像之间不断变化?

How can I get my img src to keep changing between 3-4 images when hovered?

我目前正在为一个服装网站做一个大学项目,所以我想要的是当用户将鼠标悬停在主供稿上的产品图片上时,产品图片会在该产品的 3-4 张图片之间不断变化有几毫秒的过渡。

这是我的HTML代码

<div class="imagebox">
                <a href="#"><img src="../img/Mom Sarees/5pcs/A93A5321.JPG" alt="Saree no: A93A5321 "></a>
            </div>

这是CSS

.imagebox{
display: inline-block;}

.imagebox img{
margin-top: 20px;
height: 400px;
margin-right: 10px;}

有没有办法使用 CSS 或 JS 来做到这一点?

我用JS实现了一个例子,希望对你有帮助。

let intervalId;
let i = 0;

document.querySelectorAll('.product-images').forEach((poroduct) => {
  const productImages = poroduct.querySelectorAll('img');

  poroduct.addEventListener('mouseenter', function() {
    fadeImg(productImages);
    intervalId = setInterval(() => fadeImg(productImages), 1500);
  });

  poroduct.addEventListener('mouseleave', function() {
    clearInterval(intervalId);
    productImages[i].classList.remove('active');
    productImages[0].classList.add('active');
    i = 0;
  });
});

function fadeImg(productImages) {
  productImages[i].classList.remove('active');
  i = (i + 1) % 4;
  productImages[i].classList.add('active');
}
.container {
  display: flex;
}

.imagebox {
  position: relative;
  flex: 1;
  margin: 15px;
}

.imagebox img {
  max-height: 200px;
  max-width: 100%;
  position: absolute;
  opacity: 0;
  transition: opacity 500ms;
}

.imagebox img.active {
  opacity: 1;
}
<div class="container">
  <div class="imagebox">
    <a href="#" class="product-images">
      <img class="active" src="https://fakeimg.pl/350x200/550000/?text=Image1" />
      <img src="https://fakeimg.pl/350x200/005500/?text=Image2" />
      <img src="https://fakeimg.pl/350x200/000055/?text=Image3" />
      <img src="https://fakeimg.pl/350x200/005555/?text=Image4" />
    </a>
  </div>

  <div class="imagebox">
    <a href="#" class="product-images">
      <img class="active" src="https://fakeimg.pl/350x200/000000/?text=Image1" />
      <img src="https://fakeimg.pl/350x200/faaa22/?text=Image2" />
      <img src="https://fakeimg.pl/350x200/885500/?text=Image3" />
      <img src="https://fakeimg.pl/350x200/f500aa/?text=Image4" />
    </a>
  </div>

  <div class="imagebox">
    <a href="#" class="product-images">
      <img class="active" src="https://fakeimg.pl/350x200/0000ff/?text=Image1" />
      <img src="https://fakeimg.pl/350x200/00ff00/?text=Image2" />
      <img src="https://fakeimg.pl/350x200/ff0000/?text=Image3" />
      <img src="https://fakeimg.pl/350x200/ffff00/?text=Image4" />
    </a>
  </div>
</div>