【问题标题】:Pure CSS-Parallax: Inconsistent Scrolling between Firefox and Chrome纯 CSS-Parallax:Firefox 和 Chrome 之间的滚动不一致
【发布时间】:2019-10-15 09:55:25
【问题描述】:

我一直在尝试创建纯基于 CSS 的视差滚动效果(在透视、transformZ 和缩放方面,背景滚动比前景慢)。

在我在 Chrome 中测试之前,我对结果(在 Firefox 中工作)非常满意。

在 Firefox 中,我看到了完全期望的行为:屏幕显示的背景比视口大。滚动时,背景缓慢移动,文本快速移动到视图中。 当背景层底部与视口底部对齐时,滚动结束。但是在 Chrome(桌面和移动)中,我可以向下滚动很多,错位上层并向上滚动背景,显示后面的空白页。

不幸的是,我不明白哪个元素太大以至于允许进一步滚动。我只想在达到背景结束时结束滚动。似乎没有其他图层比背景延伸得更远。

我在CodePen 上设置了一个最小问题示例。它在那里显示了所需的(类似 Firefox 的)行为,但您可能希望通过复制这两个文件并在 Chrome 中测试来查看它。

<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8'>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Minimal Problem Example</title>
        <link rel='stylesheet' type='text/css' href='parallax.css'>
    </head>

    <body>
        <div class='parallaxisWrapper'>
            <div class='parallaxisGroup'>
                <div class='layer personalInfo-layer'>
                    This text should scroll up into the viewable area
                </div>

                <div class='layer backimage-layer'>
                </div>

            </div>
        </div>
    </body>

</html>
/*
   CSS Reset
   http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}

article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}



/*
My example starts here
*/

.parallaxisWrapper {
    /* fill screen vertically */
    height: 100vh;
    /* allow only vertical scrolling */
    overflow-x: hidden;
    overflow-y: auto;
    /* shift the perspective to allow for parallax scrolling effect
        (perspective - distance) / perspective = scaleFactor
        distance is defined from zero layer, closer objects have to be scaled down and smaller have to be scaled up
    */
    perspective: 4px;
}

.parallaxisGroup {
    position: relative;
    height: 100vh;
    transform-style: preserve-3d;
}

[class$='-layer'] {
    /* position all parallax layers absolutely */
    position: absolute;
}


.personalInfo-layer {
    /* keep in the default distance to scroll normally */
    transform: translateZ(0) scale(1);
    width: 80vw;
    height: 55vh;
    left: 10vw;
    top: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 10vh;
}

.backimage-layer {
    /* transform to the back to scroll slower and adjust scale */
    transform: translateZ(-20px) scale(6);
    background: url(http://placekitten.com/1000/1000) no-repeat center fixed;
    background-size: cover;
    background-attachment: scroll;
    width: 100vw;
    /* adjust background layer width and top anchor to cover the full scrollable background of the upper layers */
    height: 116vh;
}

请不要被开头的 CSS 重置代码所迷惑。这只是一个复制的标准。此外,某些层对您来说可能是多余的。然而,这是我实际页面的调低版本。

任何有关可能导致此行为的原因或如何使 Firefox 到 Chrome 的印象一致的提示将不胜感激!

【问题讨论】:

    标签: css google-chrome firefox scroll parallax


    【解决方案1】:

    根据this Google Developers articlethis Chromium bug report 中所述,Chrome(以及 Safari)在计算元素大小时不考虑比例 - 因此不需要滚动条/空白。 他们的建议是将透视原点设置在右下角,使元素生长到“负区域”。

    需要进行以下更改才能使您的示例在 Chrome 中运行:

    • .parallaxisWrapper 类添加perspective-origin: bottom right(即添加到设置透视的类)
    • [class$='-layer'] 类添加transform-origin: bottom right(即添加到所有应该执行转换的类)

    在您的示例中,backimage-layer 与personalInfo-layer 相比太小,如果您将personalInfo-layer 的高度从55vh 更改为35vh,空白将完全消失。

    【讨论】:

    • 非常感谢您的回答。这就像魅力!
    猜你喜欢
    • 1970-01-01
    • 2014-08-18
    • 2021-11-20
    • 2013-05-10
    • 2012-02-14
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    相关资源
    最近更新 更多