簡體   English   中英

HTML 頁面中不需要的水平和垂直滾動空間

[英]Unwanted space horizontal and vertical scroll space in HTML Page

我正在研究 CSS 效果,在代碼中,我將用於顯示球的 div 附加到具有隨機大小和顏色的文檔正文中。 但是代碼會生成額外的滾動空間。 我對使用球在水平和垂直方向上生成的額外卷軸感到震驚。

HTML 頁面不會包含超過兩行。

到目前為止,我創建的 HTML 頁面的代碼Code Pen

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script src="https://kit.fontawesome.com/cae1531884.js" crossorigin="anonymous"></script>
    <link href="https://fonts.googleapis.com/css2?family=Montserrat&family=Rubik&display=swap" rel="stylesheet">
   
    <style>
        body {

            margin: 0;
            padding: 0;
            font-family: 'Montserrat', sans-serif;
        }

        .ball {
            position: absolute;
            border-radius: 100%;
            opacity: 0.7;
        }

        .headings {
            font-family: 'Rubik', sans-serif;
        }

        .container {
            height: 100vh;
            padding-left: 2rem;
            position: relative;
        }

        .vertical-center {
            margin: 0;
            position: absolute;
            top: 50%;
            -ms-transform: translateY(-50%);
            transform: translateY(-50%);
        }

        .social-links {
            padding: 5px;
            color: #121212;
        }
    </style>
</head>

<body>

    <div class="container">
        <div class="vertical-center">
            <div class="header headings">
                <h1>Hello World</h1>
            </div>
            <div class="content">
                <p>Lorem Ipsum</p>
            </div>
         
        </div>
    </div>


    <script>

        const colors = ["#3CC157", "#2AA7FF", "#d3d3d3", "#FCBC0F", "#F85F36"];

        const numBalls = 50;
        const balls = [];

        for (let i = 0; i < numBalls; i++) {
            let ball = document.createElement("div");
            ball.classList.add("ball");
            ball.style.background = colors[Math.floor(Math.random() * colors.length)];
            ball.style.left = `${Math.floor(Math.random() * 100)}vw`;
            ball.style.top = `${Math.floor(Math.random() * 100)}vh`;
            ball.style.transform = `scale(${Math.random()})`;
            ball.style.width = `${Math.random()}em`;
            ball.style.height = ball.style.width;

            balls.push(ball);
            document.body.append(ball);

        }

        // Keyframes
        balls.forEach((el, i, ra) => {
            let to = {
                x: Math.random() * (i % 2 === 0 ? -11 : 11),
                y: Math.random() * 12
            };

            let anim = el.animate(
                [
                    { transform: "translate(0, 0)" },
                    { transform: `translate(${to.x}rem, ${to.y}rem)` }
                ],
                {
                    duration: (Math.random() + 1) * 2000, // random duration
                    direction: "alternate",
                    fill: "both",
                    iterations: Infinity,
                    easing: "ease-in-out"
                }
            );
        });
    </script>

</body>

</html>

您可以將主體width設置為100vw (視口寬度)和height100vh (視口高度),還可以通過將主體的overflow設置為hiddenhidden滾動條。

body {
    height: 100vh;
    width: 100vw;
    overflow: hidden;
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM