首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >前端视频播放器弹窗功能实现方案

前端视频播放器弹窗功能实现方案

原创
作者头像
搜罗万相
发布2025-07-07 16:10:29
发布2025-07-07 16:10:29
5820
举报
文章被收录于专栏:前端开发前端开发
功能特点
  • 视频弹窗播放功能,支持点击封面图打开弹窗
  • 响应式设计,适配不同屏幕尺寸
  • 播放控制栏(播放/暂停、音量调节、全屏等)
  • 视频进度条显示与控制
  • 视频时长与当前播放时间显示
  • 优雅的动画过渡效果
  • 支持ESC键关闭弹窗

下面是完整的代码实现:

代码语言:html
复制
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>视频播放器弹窗功能</title>
    <style>
        /* 基础样式重置 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Arial', sans-serif;
        }

        body {
            background-color: #f5f5f5;
            padding: 20px;
            color: #333;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        h1 {
            text-align: center;
            margin-bottom: 30px;
            color: #2c3e50;
        }

        /* 视频列表样式 */
        .video-list {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
        }

        .video-card {
            background-color: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            cursor: pointer;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .video-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }

        .video-thumbnail {
            position: relative;
            height: 170px;
            overflow: hidden;
        }

        .video-thumbnail img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.5s ease;
        }

        .video-card:hover .video-thumbnail img {
            transform: scale(1.05);
        }

        .video-duration {
            position: absolute;
            bottom: 5px;
            right: 5px;
            background-color: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 2px 5px;
            border-radius: 3px;
            font-size: 12px;
        }

        .video-info {
            padding: 15px;
        }

        .video-title {
            font-size: 16px;
            margin-bottom: 5px;
            font-weight: 600;
            color: #2c3e50;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .video-meta {
            font-size: 12px;
            color: #7f8c8d;
        }

        /* 视频弹窗样式 */
        .video-modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.8);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 1000;
            opacity: 0;
            visibility: hidden;
            transition: opacity 0.3s ease, visibility 0.3s ease;
        }

        .video-modal.active {
            opacity: 1;
            visibility: visible;
        }

        .video-modal-content {
            position: relative;
            width: 90%;
            max-width: 1200px;
            background-color: white;
            border-radius: 8px;
            overflow: hidden;
            transform: scale(0.9);
            transition: transform 0.3s ease;
        }

        .video-modal.active .video-modal-content {
            transform: scale(1);
        }

        .video-player {
            position: relative;
            width: 100%;
            padding-top: 56.25%; /* 16:9 比例 */
        }

        .video-player video {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .close-btn {
            position: absolute;
            top: 15px;
            right: 15px;
            width: 30px;
            height: 30px;
            background-color: rgba(0, 0, 0, 0.5);
            color: white;
            border: none;
            border-radius: 50%;
            cursor: pointer;
            font-size: 18px;
            display: flex;
            justify-content: center;
            align-items: center;
            transition: background-color 0.3s ease;
            z-index: 10;
        }

        .close-btn:hover {
            background-color: rgba(0, 0, 0, 0.8);
        }

        /* 播放控制栏样式 */
        .video-controls {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            padding: 10px;
            background: linear-gradient(to top, rgba(0, 0, 0, 0.8), transparent);
            color: white;
            opacity: 0;
            visibility: hidden;
            transition: opacity 0.3s ease, visibility 0.3s ease;
            z-index: 5;
        }

        .video-player:hover .video-controls,
        .video-controls.active {
            opacity: 1;
            visibility: visible;
        }

        .controls-inner {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .control-group {
            display: flex;
            align-items: center;
        }

        .control-btn {
            background: none;
            border: none;
            color: white;
            cursor: pointer;
            font-size: 20px;
            margin-right: 15px;
            transition: color 0.3s ease;
        }

        .control-btn:hover {
            color: #3498db;
        }

        .play-btn {
            font-size: 24px;
        }

        .progress-container {
            flex: 1;
            margin: 0 15px;
            height: 4px;
            background-color: rgba(255, 255, 255, 0.3);
            border-radius: 2px;
            cursor: pointer;
            position: relative;
        }

        .progress-bar {
            position: absolute;
            top: 0;
            left: 0;
            height: 100%;
            background-color: #3498db;
            border-radius: 2px;
            width: 0%;
        }

        .time-display {
            font-size: 12px;
            margin-left: 10px;
        }

        .volume-container {
            position: relative;
            margin-right: 15px;
        }

        .volume-slider {
            position: absolute;
            bottom: 30px;
            right: 0;
            width: 100px;
            height: 6px;
            background-color: rgba(255, 255, 255, 0.3);
            border-radius: 3px;
            transform: rotate(270deg);
            transform-origin: bottom right;
            appearance: none;
            cursor: pointer;
        }

        .volume-slider::-webkit-slider-thumb {
            appearance: none;
            width: 14px;
            height: 14px;
            border-radius: 50%;
            background-color: white;
            cursor: pointer;
        }

        .volume-slider::-moz-range-thumb {
            width: 14px;
            height: 14px;
            border-radius: 50%;
            background-color: white;
            cursor: pointer;
            border: none;
        }

        .fullscreen-btn {
            margin-left: 15px;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .video-modal-content {
                width: 95%;
            }

            .controls-inner {
                flex-direction: column;
                align-items: stretch;
            }

            .control-group {
                margin-bottom: 10px;
            }

            .progress-container {
                margin: 10px 0;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>视频播放器弹窗功能演示</h1>
        
        <div class="video-list">
            <!-- 视频卡片1 -->
            <div class="video-card" data-video-src="https://www.w3schools.com/html/mov_bbb.mp4" data-thumbnail="https://picsum.photos/600/338?random=1">
                <div class="video-thumbnail">
                    <img src="https://picsum.photos/600/338?random=1" alt="视频封面">
                    <div class="video-duration">03:45</div>
                </div>
                <div class="video-info">
                    <div class="video-title">前端开发入门教程 - HTML基础</div>
                    <div class="video-meta">播放: 12.5万 | 发布: 2025-07-01</div>
                </div>
            </div>
            
            <!-- 视频卡片2 -->
            <div class="video-card" data-video-src="https://www.w3schools.com/html/mov_bbb.mp4" data-thumbnail="https://picsum.photos/600/338?random=2">
                <div class="video-thumbnail">
                    <img src="https://picsum.photos/600/338?random=2" alt="视频封面">
                    <div class="video-duration">05:12</div>
                </div>
                <div class="video-info">
                    <div class="video-title">CSS布局技巧 - Flexbox完全指南</div>
                    <div class="video-meta">播放: 8.7万 | 发布: 2025-06-25</div>
                </div>
            </div>
            
            <!-- 视频卡片3 -->
            <div class="video-card" data-video-src="https://www.w3schools.com/html/mov_bbb.mp4" data-thumbnail="https://picsum.photos/600/338?random=3">
                <div class="video-thumbnail">
                    <img src="https://picsum.photos/600/338?random=3" alt="视频封面">
                    <div class="video-duration">04:30</div>
                </div>
                <div class="video-info">
                    <div class="video-title">JavaScript基础 - 从入门到精通</div>
                    <div class="video-meta">播放: 15.2万 | 发布: 2025-06-20</div>
                </div>
            </div>
            
            <!-- 视频卡片4 -->
            <div class="video-card" data-video-src="https://www.w3schools.com/html/mov_bbb.mp4" data-thumbnail="https://picsum.photos/600/338?random=4">
                <div class="video-thumbnail">
                    <img src="https://picsum.photos/600/338?random=4" alt="视频封面">
                    <div class="video-duration">02:48</div>
                </div>
                <div class="video-info">
                    <div class="video-title">响应式设计实战 - 适配各种设备</div>
                    <div class="video-meta">播放: 9.3万 | 发布: 2025-06-15</div>
                </div>
            </div>
        </div>
    </div>

    <!-- 视频弹窗 -->
    <div class="video-modal" id="videoModal">
        <div class="video-modal-content">
            <div class="video-player" id="videoPlayer">
                <video id="videoElement" controls>
                    你的浏览器不支持视频播放
                </video>
                <button class="close-btn" id="closeBtn">×</button>
                
                <div class="video-controls" id="videoControls">
                    <div class="controls-inner">
                        <div class="control-group">
                            <button class="control-btn play-btn" id="playBtn">
                                <i class="fas fa-play"></i>
                            </button>
                            <div class="progress-container">
                                <div class="progress-bar" id="progressBar"></div>
                            </div>
                            <div class="time-display" id="timeDisplay">00:00 / 00:00</div>
                        </div>
                        
                        <div class="control-group">
                            <div class="volume-container">
                                <button class="control-btn" id="volumeBtn">
                                    <i class="fas fa-volume-up"></i>
                                </button>
                                <input type="range" min="0" max="1" step="0.1" value="0.7" class="volume-slider" id="volumeSlider">
                            </div>
                            <button class="control-btn fullscreen-btn" id="fullscreenBtn">
                                <i class="fas fa-expand"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anonymous"></script>
    <script>
        // 获取DOM元素
        const videoModal = document.getElementById('videoModal');
        const videoModalContent = document.querySelector('.video-modal-content');
        const videoPlayer = document.getElementById('videoPlayer');
        const videoElement = document.getElementById('videoElement');
        const closeBtn = document.getElementById('closeBtn');
        const videoCards = document.querySelectorAll('.video-card');
        const playBtn = document.getElementById('playBtn');
        const progressBar = document.getElementById('progressBar');
        const timeDisplay = document.getElementById('timeDisplay');
        const volumeBtn = document.getElementById('volumeBtn');
        const volumeSlider = document.getElementById('volumeSlider');
        const fullscreenBtn = document.getElementById('fullscreenBtn');
        const videoControls = document.getElementById('videoControls');

        // 视频卡片点击事件 - 打开弹窗并加载视频
        videoCards.forEach(card => {
            card.addEventListener('click', () => {
                const videoSrc = card.getAttribute('data-video-src');
                const thumbnail = card.querySelector('.video-thumbnail img').src;
                
                // 设置视频源
                videoElement.src = videoSrc;
                
                // 显示弹窗
                openVideoModal();
                
                // 加载视频
                videoElement.load();
            });
        });

        // 关闭按钮点击事件 - 关闭弹窗
        closeBtn.addEventListener('click', closeVideoModal);

        // 播放/暂停按钮点击事件
        playBtn.addEventListener('click', togglePlay);

        // 视频进度条点击事件
        const progressContainer = document.querySelector('.progress-container');
        progressContainer.addEventListener('click', seekVideo);

        // 音量按钮点击事件
        volumeBtn.addEventListener('click', toggleVolume);

        // 音量滑块拖动事件
        volumeSlider.addEventListener('input', setVolume);

        // 全屏按钮点击事件
        fullscreenBtn.addEventListener('click', toggleFullscreen);

        // 视频控制栏显示/隐藏处理
        videoPlayer.addEventListener('mousemove', showControls);
        videoPlayer.addEventListener('mouseleave', hideControlsAfterTimeout);
        videoElement.addEventListener('mousemove', showControls);
        videoElement.addEventListener('mouseleave', hideControlsAfterTimeout);

        // 视频播放状态变化事件
        videoElement.addEventListener('play', () => {
            playBtn.innerHTML = '<i class="fas fa-pause"></i>';
        });

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

目录
  • 功能特点
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档