前端实现视频Banner + 滚屏视频

视频Banner

效果图:

贴代码:

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>视频 Banner 示例</title>
    <style>
        body,
        html {
            margin: 0;
            padding: 0;
            height: 100%;
            overflow-x: hidden;
        }

        .video-banner {
            position: relative;
            width: 100%;
            height: 100vh;
            overflow: hidden;
        }

        #video-background {
            position: absolute;
            top: 50%;
            left: 50%;
            min-width: 100%;
            min-height: 100%;
            width: auto;
            height: auto;
            z-index: -1;
            transform: translate(-50%, -50%);
        }

        .video-content {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
            color: white;
            z-index: 1;
        }

        .video-content h1 {
            font-size: 3em;
            margin: 0;
        }

        .video-content p {
            font-size: 1.5em;
            margin: 10px 0 30px;
        }

        .video-content .btn {
            display: inline-block;
            padding: 10px 20px;
            background-color: #662d91;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            font-size: 1.2em;
            transition: background-color 0.3s ease;
        }

        .video-content .btn:hover {
            background-color: #50247a;
        }

    </style>
</head>

<body>
    <header class="video-banner">
        <video autoplay muted loop id="video-background">
            <source src="./assets/1080p.mp4" type="video/mp4">
            您的浏览器不支持 video 标签。
        </video>
        <div class="video-content">
            <h1>欢迎来到立桥银行</h1>
            <p>立足港澳,桥连世界</p>
            <a href="#more" class="btn">了解更多</a>
        </div>
    </header>
</body>

</html>

滚屏视频

贴代码:

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>
        body,
        html {
            margin: 0;
            padding: 0;
            height: 100%;
            overflow: hidden;
            font-family: Arial, sans-serif;
        }

        .scroll-video-container {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            overflow: hidden;
            z-index: -1;
        }

        #scroll-video {
            position: absolute;
            top: 50%;
            left: 50%;
            min-width: 100%;
            min-height: 100%;
            width: auto;
            height: auto;
            transform: translate(-50%, -50%);
            object-fit: cover;
        }

        #content {
            height: 1000px;
            /* 调整高度以适应滚动 */
            overflow-y: scroll;
            scroll-behavior: smooth;
        }

        .section {
            height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            text-align: center;
            padding: 20px;
            box-sizing: border-box;
        }

        .section h1 {
            font-size: 3em;
            margin: 0;
        }

        .section p {
            font-size: 1.5em;
            margin: 10px 0 30px;
        }
    </style>
</head>

<body>
    <div class="scroll-video-container">
        <video id="scroll-video" src="./assets/section-3-desktop-900-264-crf-20-g-1.mp4" muted></video>
    </div>

    <section id="content">
        <div class="section" id="section1">
            <h1>欢迎来到立桥银行</h1>
            <p>立足港澳,桥连世界</p>
        </div>
        <div class="section" id="section2">
            <h1>我们的服务</h1>
            <p>提供多种金融服务,满足您的需求</p>
        </div>
        <div class="section" id="section3">
            <h1>联系我们</h1>
            <p>了解更多详情,请联系我们</p>
        </div>
    </section>

    <script>

        document.addEventListener('DOMContentLoaded', () => {
            const video = document.getElementById('scroll-video');
            const content = document.getElementById('content');
            const totalHeight = content.scrollHeight - window.innerHeight;
            let lastScrollTop = 0;
            let timeoutId;

            content.addEventListener('scroll', () => {
                const scrollPosition = content.scrollTop / totalHeight;
                const videoDuration = video.duration;
                const currentTime = scrollPosition * videoDuration;

                // 播放视频
                video.currentTime = currentTime;
                video.play();

                // 清除之前的定时器
                clearTimeout(timeoutId);

                // 设置一个新的定时器,在用户停止滚动后暂停视频
                timeoutId = setTimeout(() => {
                    video.pause();
                }, 100); // 100毫秒后暂停视频
            });
        });
    </script>
</body>

</html>
相关推荐
gnip1 小时前
企业级配置式表单组件封装
前端·javascript·vue.js
一只叫煤球的猫2 小时前
写代码很6,面试秒变菜鸟?不卖课,面试官视角走心探讨
前端·后端·面试
excel3 小时前
Three.js 材质(Material)详解 —— 区别、原理、场景与示例
前端
掘金安东尼3 小时前
抛弃自定义模态框:原生Dialog的实力
前端·javascript·github
hj5914_前端新手7 小时前
javascript基础- 函数中 this 指向、call、apply、bind
前端·javascript
薛定谔的算法7 小时前
低代码编辑器项目设计与实现:以JSON为核心的数据驱动架构
前端·react.js·前端框架
Hilaku7 小时前
都2025年了,我们还有必要为了兼容性,去写那么多polyfill吗?
前端·javascript·css
yangcode7 小时前
iOS 苹果内购 Storekit 2
前端
LuckySusu7 小时前
【js篇】JavaScript 原型修改 vs 重写:深入理解 constructor的指向问题
前端·javascript
LuckySusu7 小时前
【js篇】如何准确获取对象自身的属性?hasOwnProperty深度解析
前端·javascript