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

解决.item元素切换active/prev/next类时的视频播放暂停问题

问题描述

我有多个包含video的.item元素,这些元素具备active、prev、next三种状态类。需求为:

  • 当.item元素拥有active类时,其内部的video自动播放
  • 当元素切换为prev或next类时,对应的video暂停

当前代码存在以下问题:

  • 使用querySelectorAll无法实现预期效果
  • 使用querySelector仅能控制第一个.item元素的视频
  • 当元素类从active切换为prev/next时,视频不会暂停
问题根源
  1. 原代码仅获取了第一个.vd元素,无法控制所有幻灯片内的视频
  2. 状态判断逻辑错误,检查的是父容器.items的类,而非每个.item的实际状态
  3. 幻灯片切换后没有触发视频状态更新的逻辑
修复后的代码

HTML(无需修改)

<div class="items">
    <div class="item active">
        <video class="vd" src="https://www.oleofarm24.pl/data/include/cms/0cms-podstrony-l/2_film_FORTE_mobilka.mp4?v=1720421734786"
        type="video/mp4" muted loop>
        </video>
    </div>
    <div class=" item next">
        <video class="vd" src="https://www.oleofarm24.pl/data/include/cms/0cms-podstrony-l/1_film_KIDS_mobilka.mp4?v=1720421734787"
        type="video/mp4" muted loop>
           </video>
    </div>
    <div class="item prev">
        <video class="vd" src="https://www.oleofarm24.pl/data/include/cms/0cms-podstrony-l/3_film_K2_mobilka.mp4?v=1720421734786"
       type="video/mp4" muted loop>
          </video>
    </div>
    <div class="button-container">
        <div class="button"><i class="fas fa-angle-left"></i></div>
        <div class="button"><i class="fas fa-angle-right"></i></div>
    </div>
</div>

<style>
@import url("https://use.typekit.net/ewm4fzc.css");
 @import url('https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap');
</style>

CSS(无需修改)

.items {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  user-select: none;
}

.items .item {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 350px;
  height: 750px;
  overflow: hidden;
  transition: all 300ms ease-in-out;
  z-index: -1;
  opacity: 0;
}

.item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.item.active {
  opacity: 1;
  z-index: 99;
  box-shadow: 0px 0px 105px -35px rgba(0, 0, 0, 0.75);
}

.item.prev {
  z-index: 2;
  opacity: 0.25;
  transform: translate(-125%, -50%);
}

.item.next {
  z-index: 2;
  opacity: 0.25;
  transform: translate(25%, -50%);
}

.items .button-container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 275px;
  z-index: 100;
}

.button-container .button {
  color: #fff;
  font-size: 32px;
  cursor: pointer;
  position: relative;
  opacity: 0.75;
  transition: all 300ms ease-in-out;
}

.button-container .button:hover {
  opacity: 1;
}

.button-container .button:before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 50px;
  height: 50px;
  background-color: rgba(0, 0, 0, 1);
  border-radius: 50%;
  z-index: -99;
}

.button-container .button:nth-child(1) {
  float: left;
}

.button-container .button:nth-child(2) {
  float: right;
}
div > h2{
   position: absolute;
   color: #f99f00;
   font-size: 8em;
   font-weight: bold;
   rotate: 270deg;
   font-family: 'eurostile',Arial,sans-serif;
   top: 25%;
   left: 60%;
   transform: translate(-50%, -50%);
}
.active1{
  width:250px;
  height:750px;
}

JavaScript(核心修改)

const slider = document.querySelector(".items");
const slides = document.querySelectorAll(".item");
const button = document.querySelectorAll(".button");

let current = 0;
let prev = 2;
let next = 1;

for (let i = 0; i < button.length; i++) {
  button[i].addEventListener("click", () => i == 0 ? gotoPrev() : gotoNext());
}

const gotoPrev = () => current > 0 ? gotoNum(current - 1) : gotoNum(slides.length - 1);

const gotoNext = () => current < 2 ? gotoNum(current + 1) : gotoNum(0);

const gotoNum = number => {
  current = number;
  prev = current - 1;
  next = current + 1;

  for (let i = 0; i < slides.length; i++) {
    slides[i].classList.remove("active");
    slides[i].classList.remove("prev");
    slides[i].classList.remove("next");
  }

  if (next == 3) {
    next = 0;
  }

  if (prev == -1) {
    prev = 2;
  }

  slides[current].classList.add("active");
  slides[prev].classList.add("prev");
  slides[next].classList.add("next");
  
  // 每次切换幻灯片后更新视频状态
  updateVideoState();
}

// 遍历所有幻灯片,根据类状态控制视频播放/暂停
function updateVideoState() {
  slides.forEach(item => {
    const video = item.querySelector(".vd");
    if (item.classList.contains("active")) {
      video.play();
    } else {
      video.pause();
    }
  });
}

// 页面加载时初始化视频状态
updateVideoState();

关键修改说明

  • 移除单个视频变量,改为遍历每个.item获取对应视频元素
  • 新增updateVideoState函数,根据每个幻灯片的类状态控制视频播放/暂停
  • 在幻灯片切换函数gotoNum末尾调用状态更新函数,确保切换后视频状态同步
  • 页面加载时调用初始化函数,确保初始状态下视频正常播放

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 05:50:55