在页面上跳滚动刷新

发布于 2025-01-23 09:12:07 字数 3812 浏览 2 评论 0原文

我想将全屏背景视频作为“背景:url('img')封面中心”。我是由JS做到的,但是我在页面上遇到了问题。

当我的视口的最低部分位置高于< h2> content</h2>的中心时,我将在某些像素中跳高。在下一个刷新中,我将获得相同的效果,直到到达页面顶部。

我发现的唯一修复程序是:删除< video>,但这看起来不是一个很好的解决方案。

代码示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Example</title>
    <style>
        html, body, main {
            margin: 0;
            height: 100%;
        }
        * {
            box-sizing: border-box;
        }
        section {
            display: block;
            width: 100%;
            height: 100%;
            background-color: rgb(68, 72, 89);
            overflow: hidden;
            position: relative;
        }
        .section-1-colorize {
            z-index: 3;
            display: block;
            position: absolute;
            width: 100%;
            height: 100%;
            background-color: #0b1d2a;
            opacity: 0.65;
        }
        #background-video {
            position: absolute;
            z-index: 3;
            overflow: hidden;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }
        .s2, .s3 {
            display: flex;
            color: #fff;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            font-size: 3em;
            font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
        }
        .s3 {
            background-color: #0b1d2a;
        }
    </style>
</head>
<body>
    <main>
        <section>
            <video autoplay muted loop id="background-video">
                <!-- <source> appearing by JS code below -->
            </video>
            <div class="section-1-colorize"></div>
            <span style="position: absolute; z-index: 5; color: #fff; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 50px;">Hello, World!</span>
        </section>
        <section class="s2">
            <h1>Any</h1>
            <h2>Content</h2>
            <h1>HERE</h1>
        </section>
        <section class="s3">
            <h1>Any</h1>
            <h2>Content2</h2>
            <h1>HERE</h1>
        </section>
    </main>

<script>
    var video = document.getElementById('background-video');
    var source = document.createElement('source');
    source.src = 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerJoyrides.mp4';
    source.type = 'video/mp4';
    video.appendChild(source);
    // Appending source with src and type
    video.load();

    // Simulating "background-size: cover;" for video
    video.onloadeddata = function () {            
        function solution() { // Calculating what should be used "width" or "height" 100%
            var video = document.getElementById('background-video');

            var math_1 = Math.round(window.innerWidth / window.innerHeight*100)/100;
            var math_2 = Math.round(video.videoWidth / video.videoHeight*100)/100;
            if(math_1 > math_2) {
                video.style = "width: 100%"
            } else {
                video.style = "height: 100%"
            }
        }

        // Tracking resize and then recalculating
        window.addEventListener('resize', solution);
        solution()
    }
</script>
</body>
</html>

I wanted to make fullscreen background video as "background: url('img') cover center". I made It by JS, but I'm having a problem on page refresh.

When my bottom part of viewport locating higher than center of <h2>Content</h2> on page refresh I will get jumped higher for some pixels. And on next refreshes I will get the same effect till I get to the top of the page.

The only fix I found: remove <video>, but this doesn't looks like a good solution.

Code example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Example</title>
    <style>
        html, body, main {
            margin: 0;
            height: 100%;
        }
        * {
            box-sizing: border-box;
        }
        section {
            display: block;
            width: 100%;
            height: 100%;
            background-color: rgb(68, 72, 89);
            overflow: hidden;
            position: relative;
        }
        .section-1-colorize {
            z-index: 3;
            display: block;
            position: absolute;
            width: 100%;
            height: 100%;
            background-color: #0b1d2a;
            opacity: 0.65;
        }
        #background-video {
            position: absolute;
            z-index: 3;
            overflow: hidden;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }
        .s2, .s3 {
            display: flex;
            color: #fff;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            font-size: 3em;
            font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
        }
        .s3 {
            background-color: #0b1d2a;
        }
    </style>
</head>
<body>
    <main>
        <section>
            <video autoplay muted loop id="background-video">
                <!-- <source> appearing by JS code below -->
            </video>
            <div class="section-1-colorize"></div>
            <span style="position: absolute; z-index: 5; color: #fff; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 50px;">Hello, World!</span>
        </section>
        <section class="s2">
            <h1>Any</h1>
            <h2>Content</h2>
            <h1>HERE</h1>
        </section>
        <section class="s3">
            <h1>Any</h1>
            <h2>Content2</h2>
            <h1>HERE</h1>
        </section>
    </main>

<script>
    var video = document.getElementById('background-video');
    var source = document.createElement('source');
    source.src = 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerJoyrides.mp4';
    source.type = 'video/mp4';
    video.appendChild(source);
    // Appending source with src and type
    video.load();

    // Simulating "background-size: cover;" for video
    video.onloadeddata = function () {            
        function solution() { // Calculating what should be used "width" or "height" 100%
            var video = document.getElementById('background-video');

            var math_1 = Math.round(window.innerWidth / window.innerHeight*100)/100;
            var math_2 = Math.round(video.videoWidth / video.videoHeight*100)/100;
            if(math_1 > math_2) {
                video.style = "width: 100%"
            } else {
                video.style = "height: 100%"
            }
        }

        // Tracking resize and then recalculating
        window.addEventListener('resize', solution);
        solution()
    }
</script>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2025-01-30 09:12:07

解决方案:将视频用CSS包裹到块中:

    display: block;
    width: 100%;
    height: 100%;
    position: absolute;

Solution: wrap video into block with CSS:

    display: block;
    width: 100%;
    height: 100%;
    position: absolute;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文