overflow: hidden为什么不起作用?或者它起作用了吗?

huangapple go评论77阅读模式
英文:

Why is overflow: hidden not working? Or is it?

问题

我正在尝试为这个网站创建一个无限滚动的背景,虽然它运行得非常完美,但我找不到一种隐藏超出我想要显示的部分的方法。如果我理解正确,背景图像本身要比其容器大,并且无限滚动的 div 的宽度需要是图像宽度的 3 倍。我不确定需要更改什么,以使背景仅占用窗口的大小,或者我只是不理解 CSS。

HTML:

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>template 002</title>
    <link href="https://willdotjpg.gay/templates/y2ktest.css" rel="stylesheet" type="text/css" media="all">
</head>

<body>
    <div class="container">
        <div class="infinite-scrolling"></div>
    </div>
    <div class="title">
    </div>
    <div class="main"></div>
</body>

</html>

CSS:

.container {
    overflow: hidden;
}

.infinite-scrolling {
    background: url("https://willdotjpg.gay/images/backgrounds/testbackground.png") repeat-x;
    height: 2520px;
    width: 10080px;
    /* 图像宽度的3倍 */
    animation: slide 60s linear infinite;
    position: absolute;
}

@keyframes slide {
    0% {
        transform: translate3d(0, 0, 0);
    }

    100% {
        transform: translate3d(-3360px, 0, 0);
    }
}

在浏览器中显示的站点截图,显示滚动条超出右侧和底部

英文:

I'm trying to make an infinite scrolling background for this site, and while it's working perfectly, I can't find a way to hide the part that extends beyond what I want to show. If I'm understanding this right, the background image itself to be larger than its container, and the width of the infinite-scrolling div needs to be 3x the width of the image. I'm not sure what I need to change so that the background only takes up the size of the window, or if I just don't understand the CSS.

The HTML:

&lt;!doctype html&gt;
&lt;html&gt;

&lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;template 002&lt;/title&gt;
    &lt;link href=&quot;https://willdotjpg.gay/templates/y2ktest.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;all&quot;&gt;
&lt;/head&gt;

&lt;body&gt;
    &lt;div class=&quot;container&quot;&gt;
        &lt;div class=&quot;infinite-scrolling&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;title&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;main&quot;&gt;&lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;

The CSS:

.container {
    overflow: hidden;
}

.infinite-scrolling {
    background: url(&quot;https://willdotjpg.gay/images/backgrounds/testbackground.png&quot;) repeat-x;
    height: 2520px;
    width: 10080px;
    /* The image width times 3 */
    animation: slide 60s linear infinite;
    position: absolute;
}

@keyframes slide {
    0% {
        transform: translate3d(0, 0, 0);
    }

    100% {
        transform: translate3d(-3360px, 0, 0);
    }
}

a screenshot of the site in-browser, showing the scrollbars extending way off to the right and bottom

答案1

得分: 2

这是您想要的:

.container {
    overflow: hidden;
    width: 100%;
    height: 100vh;
    position: fixed;
}

要隐藏溢出,首先您需要设置特定的高度或宽度,并且这里您需要设置为固定位置。

英文:

Here is what you wanted:

.container {
    overflow: hidden;
    width: 100%;
    height: 100vh;
    position: fixed;
}

To hide overflow first you have to set a specific height or width and also here you need to set the position fixed.

huangapple
  • 本文由 发表于 2023年6月26日 06:07:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定