我一直在尝试创建一个纯基于 CSS 的视差滚动效果(背景滚动速度比前景慢,具有透视、transformZ 和缩放)。
在 Chrome 中测试之前,我对结果(在 Firefox 中工作)非常满意。
在 Firefox 中,我完全看到了所需的行为:屏幕显示的背景比视口(viewport)大。滚动时,背景缓慢移动,文本快速移动到 View 中。 当背景层底部与视口(viewport)底部对齐时,滚动结束。然而,在 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-Reset Code 搞糊涂了。它只是一个复制的标准。此外,某些层对您来说似乎是多余的。然而,这是我实际页面的调低版本。
任何有关可能导致此行为的原因或如何使从 Firefox 到 Chrome 的印象保持一致的提示都将不胜感激!
最佳答案
根据 this Google Developers article如 this Chromium bug report 中所述,Chrome(以及 Safari)在计算元素大小时不考虑比例 - 因此不需要滚动条/空白。 他们的提议是将透视原点设置在右下角,让元素长入“负区域”。
要使您的示例在 Chrome 中运行,需要进行以下更改:
- 在
.parallaxisWrapper
类中,添加perspective-origin: bottom 对
(即设置透视图的类) - 在
[class$='-layer']
类中,添加transform-origin: bottom right
(即添加到应执行转换的所有类)
在您的示例中,如果您将 personalInfo 层的高度从 55vh
更改为 35vh
,空白完全消失。
关于css - 纯 CSS-视差 : Inconsistent Scrolling between Firefox and Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58391945/