video全屏視頻背景適配不同分辨率


需求:

給到一段視頻,首屏要求鋪滿整個屏幕,並且適配不同分辨率。

解決思路:

獲取當前視口寬高,調整視頻寬高比進行填充,調整視頻窗口位置以水平居中。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>video_demo</title>
    <script src="https://cdn.bootcss.com/jquery/3.5.0/jquery.js"></script>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    .pageIndex {
        width: 100%;
        height: 100%;
        position: relative;
        background-color: #0f0f12;
    }

    .videobox {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }

    .pageIndex .fullvid {
        position: absolute;
        width: 100%;
        left: 50%;
        margin-left: -50%;
        top: 0px;
        overflow: hidden;
        background-color: #0f0f12;
    }
</style>

<body>
    <div class="page pageIndex">
        <div class="fullvid">
            <div class="videobox">
                <video muted="muted" loop="loop" autoplay="autoplay" height="100%" width="100%">
                    <source src="https://static.web.sdo.com/jijiamobile/pic/ff14/ffweb850/vindex0130_2.mp4?123" type="video/mp4">
                </video>
            </div>
        </div>
    </div>
</body>
<script>
    var n = document.documentElement.clientHeight,
        e = document.documentElement.clientWidth,
        t = n / e;
    $(".page,.fullvid").height(n);
    // 9/16=0.5625視頻寬高比
    if (t > .5625) {
        $(".videobox").width(n / .5625);
        $(".videobox").height(n);
        $(".videobox").css("marginLeft", 0 - (n / .5625 - e) / 2);
        $(".videobox").css("marginTop", 0);
    }
    else if (t < .5625) {
        $(".videobox").width(e);
        $(".videobox").height(.5625 * e);
        $(".videobox").css("marginLeft", 0);
        $(".videobox").css("marginTop", 0 - (.5625 * e - n) / 2);
    }
    else {
        $(".videobox").width(e);
        $(".videobox").height(n);
        $(".videobox").css("marginLeft", 0);
        $(".videobox").css("marginTop", 0);
    }
</script>

</html>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM