You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

电商图片轮播:实现鼠标悬停时显示下方切换圆点的效果

实现电商轮播图悬停显示切换圆点及颜色切换效果

要实现鼠标悬停轮播图时才显示切换圆点的效果,只需调整HTML结构和CSS样式,原有JS逻辑无需修改,具体操作如下:

1. 调整HTML结构

将原来独立在轮播容器外的切换圆点容器,移入slideshow-container内部,这样可以通过父元素的hover状态控制圆点的显示/隐藏,同时给圆点容器添加类名方便CSS控制:

<!-- Slideshow container -->
<div class="slideshow-container">

  <!-- Full-width images with number and caption text -->
  <div class="mySlides fade">
    <div class="numbertext">1 / 4</div>
    <img src="https://imgur.com/cO9OxGF.png" style="width:100%">
  </div>

  <div class="mySlides fade">
    <div class="numbertext">2 / 4</div>
    <img src="https://imgur.com/mN2ZEE5.png" style="width:100%">
  </div>

  <div class="mySlides fade">
    <div class="numbertext">3 / 4</div>
    <img src="https://imgur.com/l1Q0Irp.png" style="width:100%">
  </div>

  <div class="mySlides fade">
    <div class="numbertext">4 / 4</div>
    <img src="https://imgur.com/2qQaYoG.png" style="width:100%">
  </div>

  <!-- Next and previous buttons -->
  <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
  <a class="next" onclick="plusSlides(1)">&#10095;</a>

  <!-- 把圆点容器移入轮播容器内 -->
  <div class="dot-container">
    <span class="dot" onclick="currentSlide(1)"></span>
    <span class="dot" onclick="currentSlide(2)"></span>
    <span class="dot" onclick="currentSlide(3)"></span>
    <span class="dot" onclick="currentSlide(4)"></span>
  </div>
</div>

2. 修改CSS样式

默认隐藏圆点容器,当鼠标悬停在轮播容器上时显示,同时添加过渡动画让显示/隐藏更平滑,调整圆点的位置和样式适配电商平台风格:

* {
  box-sizing: border-box
}

/* Slideshow container */
.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
  /* 防止圆点容器超出可视范围 */
  overflow: hidden;
}

/* Hide the images by default */
.mySlides {
  display: none;
}

/* Next & previous buttons */
.prev,
.next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin-top: -22px;
  padding: 16px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
  /* 默认隐藏箭头,hover时显示(贴合主流电商交互) */
  opacity: 0;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* 轮播容器hover时显示箭头 */
.slideshow-container:hover .prev,
.slideshow-container:hover .next {
  opacity: 1;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover,
.next:hover {
  background-color: rgba(0, 0, 0, 0.8);
}

/* Caption text */
.text {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* 圆点容器样式 */
.dot-container {
  position: absolute;
  bottom: 15px;
  left: 50%;
  transform: translateX(-50%);
  /* 默认隐藏 */
  opacity: 0;
  /* 添加过渡动画 */
  transition: opacity 0.3s ease;
}

/* 轮播容器hover时显示圆点容器 */
.slideshow-container:hover .dot-container {
  opacity: 1;
}

/* The dots/bullets/indicators */
.dot {
  cursor: pointer;
  height: 10px;
  width: 10px;
  margin: 0 4px;
  background-color: rgba(255,255,255,0.5);
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

.active,
.dot:hover {
  background-color: #fff;
}

3. 保留原有JS代码

轮播的切换逻辑无需修改,直接使用你原来的代码即可:

let slideIndex = 1;
showSlides(slideIndex);

// Next/previous controls
function plusSlides(n) {
  showSlides(slideIndex += n);
}

// Thumbnail image controls
function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  let i;
  let slides = document.getElementsByClassName("mySlides");
  let dots = document.getElementsByClassName("dot");
  if (n > slides.length) {
    slideIndex = 1
  }
  if (n < 1) {
    slideIndex = slides.length
  }
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";
  }
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex - 1].style.display = "block";
  dots[slideIndex - 1].className += " active";
}

关键细节说明

  • 将圆点容器移入轮播容器内,确保hover事件作用在整个轮播区域时触发圆点显示
  • 使用opacity配合transition实现平滑的显示/隐藏动画,比display:none更符合用户体验
  • 额外添加了箭头的hover显示效果,贴合主流电商平台的交互逻辑
  • 调整了圆点的默认背景色为半透明白色,激活/hover时变为纯白,更适配深色背景的轮播图

内容的提问来源于stack exchange,提问作者Fiolber Studios

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 07:25:34