您的当前位置:首页正文

实现网页全屏的5种方式

2024-11-13 来源:个人技术集锦

第一种

使用html body设置宽高100%

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        html,body{
            width: 100%;
            height: 100%;
        }
        div{
            width: 100%;
            height: 100%;
            background-color: lightskyblue;
        }
        img{
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
    <div>
        <img src="./2.jpg" alt="">
    </div>
</body>
</html>

第二种

使用height: calc(100vh)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 100%;
            height: calc(100vh);
            background-color: lightskyblue;
        }
        img{
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
    <div>
        <img src="./1.jpg" alt="">
    </div>
</body>
</html>

第三种

使用绝对定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            position: absolute;
            width: 100%;
            height: 100%;
            background-color: lightskyblue;
        }
        img{
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
    <div>
        <img src="./3.jpg" alt="">
    </div>
</body>
</html>

第四种

使用js document.body.clientHeight

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        html, body {
            width: 100%;
            height: 100%;
        }
        img{
            width: 100%;
            height: 100%;
        } 
    </style>
</head>
<body>
    <div>
        <img src="./3.jpg" alt="">
    </div>

    <script>
        let div  = document.querySelector('div')
        div.style.width = document.body.clientWidth+'px'
        div.style.height = document.body.clientHeight+'px'
    </script>
</body>
</html>

第五种

使用jquery $(window).height()

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./jquery.min.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        html, body {
            width: 100%;
            height: 100%;
        }
        img{
            width: 100%;
            height: 100%;
        } 
    </style>
</head>
<body>
    <div>
        <img src="./4.jpg" alt="">
    </div>

    <script>
        $('div').width($(window).width())
        $('div').height($(window).height())
    </script>
</body>
</html>
Top