【问题标题】:Unwanted space horizontal and vertical scroll space in HTML PageHTML页面中不需要的空间水平和垂直滚动空间
【发布时间】:2020-12-18 23:33:26
【问题描述】:

我正在研究 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>

【问题讨论】:

  • But there is extra scroll space being generated with the code. ... 容器控制滚动行为。试试overflow-x: auto
  • 适用于全屏视图但不适用于移动视图
  • 在移动端视图中,用户将能够对隐藏的内容进行触摸拖动
  • 不需要那个touch拖拽页面不包含超过2行html内容
  • 由于容器不是问题,代码的某些其他方面会导致不良结果。可能你有一个额外的结束标签或一个没有结束的开始标签。

标签: javascript html css layout responsive-design


【解决方案1】:

您可以将主体width 设置为100vw(视口宽度)和height 设置为100vh(视口高度),还可以通过将主体的overflow 设置为hidden 来隐藏滚动条。

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

【讨论】:

  • 这将使水平内容无法访问。
  • 适用于全屏视图但不适用于移动视图我仍在滚动移动视图
  • 对我来说,它适用于移动设备和全屏。您是否删除了任何高度或宽度?如果是这样,我可以尝试其他方法。如果没有,我真的不知道
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多