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

如何仅使用CSS实现轮播图自动轮播、悬停暂停及循环播放功能

问题原因

你现有代码的核心问题是动画绑定对象错误,你写的.carousel li.carousel-1这类选择器没有对应到实际的轮播内容元素.carousel-item,同时动画总时长设置错误,导致30秒停留要求没有实现,另外单选框的选中状态和动画切换不同步,手动切换后动画会错乱。

修改后的完整代码

CSS部分

<style>
.carousel {
    border-style:solid;
    border-width:1px;
    border-color:rgba(0, 0, 0, 0.05);
    box-shadow: 0px 1px 6px rgba(0, 0, 0, 0.1);
    width:100%;
    height:250px;
    position: relative;
}

.carousel-inner {
    position: relative;
    overflow: hidden;
    width: 100%;
    height:250px;
}

.carousel-open:checked + .carousel-item {
    position: absolute;
    opacity: 1;
    background-color:white;
    width:100%;
    height:250px;
    z-index: 2;
}

.carousel-item {
    position: absolute;
    opacity: 0;
    padding-top:10px;
    text-align:center;
    width: 100%;
    height: 250px;
    transition: all 0.5s ease;
}

/* 给三个轮播项分别绑定动画,总时长90s,每张停留30s */
.carousel-item:nth-of-type(1) {
  animation: cycle 90s linear infinite;
}
.carousel-item:nth-of-type(2) {
  animation: cycle2 90s linear infinite;
}
.carousel-item:nth-of-type(3) {
  animation: cycle3 90s linear infinite;
}
/* 悬停时暂停所有动画 */
.carousel:hover .carousel-item {
  animation-play-state: paused;
}

.carousel-control {
    background: rgba(0, 0, 0, 0);
    border-radius: 50%;
    color:#008c6c;
    cursor: pointer;
    display: none;
    font-size: 30px;
    height: 40px;
    line-height: 35px;
    position: absolute;
    top: 50%;
    -webkit-transform: translate(0, -50%);
    -ms-transform: translate(0, -50%);
    transform: translate(0, -50%);
    text-align: center;
    width: 40px;
    z-index: 10;
}

.carousel-control.prev {
    left: 2%;
}

.carousel-control.next {
    right: 2%;
}

.carousel-control:hover {
    background: rgba(0, 0, 0, 0.05);
    color: #008c6c;
}

#carousel-1:checked ~ .control-1,
#carousel-2:checked ~ .control-2,
#carousel-3:checked ~ .control-3 {
    display: block;
}

.carousel-indicators {
    margin: 0;
    padding: 0;
    position: absolute;
    bottom: 2px;
    left: 0;
    right: 0;
    text-align: center;
    z-index: 10;
}

.carousel-indicators li {
    display: inline-block;
    margin: 0 5px;
}

.carousel-bullet {
    color: rgba(0, 0, 0, 0.05);
    cursor: pointer;
    font-size: 25px;
}

.carousel-bullet:hover {
    color: rgba(0, 0, 0, 0.1);
}

#carousel-1:checked ~ .control-1 ~ .carousel-indicators li:nth-child(1) .carousel-bullet,
#carousel-2:checked ~ .control-2 ~ .carousel-indicators li:nth-child(2) .carousel-bullet,
#carousel-3:checked ~ .control-3 ~ .carousel-indicators li:nth-child(3) .carousel-bullet {
    color: #008c6c;
}

@keyframes cycle {
  0%   {opacity: 1; z-index: 2;}
  31.33%  {opacity: 1; z-index: 2;} /* 前30秒显示第一张 */
  33.33%  {opacity: 0; z-index: 1;}
  98%  {opacity: 0; z-index: 1;}
  100% {opacity: 1; z-index: 2;}
}
@keyframes cycle2 {
  0%   {opacity: 0; z-index: 1;}
  31.33%  {opacity: 0; z-index: 1;}
  33.33%  {opacity: 1; z-index: 2;}
  64.66%  {opacity: 1; z-index: 2;} /* 30-60秒显示第二张 */
  66.66%  {opacity: 0; z-index: 1;}
  100% {opacity: 0; z-index: 1;}
}
@keyframes cycle3 {
  0%   {opacity: 0; z-index: 1;}
  64.66%  {opacity: 0; z-index: 1;}
  66.66%  {opacity: 1; z-index: 2;}
  98%  {opacity: 1; z-index: 2;} /* 60-90秒显示第三张 */
  100% {opacity: 0; z-index: 1;}
}
</style>

HTML部分无需修改,和原有代码一致即可

<div class="carousel"> 
   <div class="carousel-inner"> 
      <input name="carousel" class="carousel-open" id="carousel-1" aria-hidden="true" type="radio" hidden="true" Checked/> 
      <div class="carousel-item"> 
         <h1>slide 1</h1> 
         <p>your content here</p> 
      </div> 
      <input name="carousel" class="carousel-open" id="carousel-2" aria-hidden="true" type="radio" hidden="true"/> 
      <div class="carousel-item"> 
         <h1>Slide 2</h1> 
      <p>your content here</p> 
      </div> 
      <input name="carousel" class="carousel-open" id="carousel-3" aria-hidden="true" type="radio" hidden="true"/> 
      <div class="carousel-item"> 
         <h2>slide 3</h2> 
        <p>Your content here</p> 
      </div> 
     
      <label class="carousel-control prev control-1" for="carousel-3">‹</label>
      <label class="carousel-control next control-1" for="carousel-2">›</label>
      <label class="carousel-control prev control-2" for="carousel-1">‹</label>
      <label class="carousel-control next control-2" for="carousel-3">›</label>
      <label class="carousel-control prev control-3" for="carousel-2">‹</label>
      <label class="carousel-control next control-3" for="carousel-1">›</label> 
      <ol class="carousel-indicators"> 
         <li> 
            <label class="carousel-bullet" for="carousel-1">•</label> </li> 
         <li> 
            <label class="carousel-bullet" for="carousel-2">•</label> </li> 
         <li> 
            <label class="carousel-bullet" for="carousel-3">•</label> </li> 

      </ol> 
   </div> 
</div>

实现效果说明

修改后完全满足要求的三个功能:

  • 每张幻灯片停留30秒后自动切换到下一张
  • 鼠标悬停在轮播区域时轮播立即暂停,移开后继续播放
  • 所有幻灯片无限循环播放,原有按钮切换、指示器点击功能不受影响

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 10:27:03