使用视差效果优化滚动性能

xvzwx

我有一个页面,其中有两个部分相互堆叠。上半部分有一个固定位置的背景图像,以创建视差效果。因为我在滚动时遇到了巨大的性能问题,所以我不得不更改布局。

由此:

.upper-section {

    height: 100vh;

    background: url("./img/background.png") no-repeat fixed;
    background-size: cover;
}

.lower-section { 
    height: 100vh;
    ...
}

对此:

.upper-section {

    height: 100vh;

    overflow: hidden; // added for pseudo-element
    position: relative; // added for pseudo-element

    &::before {
        content: ' ';
        display: block;
        position: fixed; // instead of background-attachment
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        background: url("./img/background.png") no-repeat fixed;
        background-size: cover;
        will-change: transform; // creates a new paint layer
        z-index: -1;
    }
}

将背景放在它自己的容器中。我的问题是我的背景图像容器没有继承上部容器的高度并且也覆盖了下部。如果我更改position: fixed;为,position: absolute;我会遇到与以前相同的性能问题。知道如何解决这个问题吗?

更新 1

致所有未来的读者:我通过将下半部分的背景设置为白色来解决我的问题:

.lower-section { 
    height: 100vh;
    background: white;
}
G-西里尔

根据您的尝试和@MrLister 的建议,回答这个问题:

正如之前评论的那样,在评论流中迷失了方向,你错过了.lower-section隐藏以前的背景

html,
body {
  margin: 0
}

.upper-section {
  height: 100vh;
  overflow: hidden;
  position: relative;
}

.upper-section::before {
  content: ' '; 
  position: fixed; 
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background: url("http://lorempixel.com/700/700") no-repeat fixed;
  background-size: cover;
  will-change: transform;  
  z-index: -1;
}

.lower-section {
  height: 100vh;
  background: white;
}
<div class="upper-section">
  Upper section
</div>

<div class="lower-section">
  Lower section
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用 CSS 以视差效果滚动

来自分类Dev

视差动画滚动效果

来自分类Dev

视差滚动效果变得迟钝

来自分类Dev

视差透明滚动效果,“窗帘”div 放置在视差 div 之外

来自分类Dev

对滚动文本的水平视差效果

来自分类Dev

好的滚动视差效果不起作用

来自分类Dev

多幅图像的视差效果滚动

来自分类Dev

使用JavaScript的视差效果问题

来自分类Dev

使用JavaScript的视差效果问题

来自分类Dev

如何提高视差滚动脚本的性能?

来自分类Dev

使用ios 7视差效果移动图像

来自分类Dev

使用ios 7视差效果移动图像

来自分类Dev

如何在滚动的Recycler View Item上创建视差效果?

来自分类Dev

像TodoMovies 3一样的视差滚动效果

来自分类Dev

在滚动条上创建视差幻灯片效果

来自分类Dev

网站显示效果中的视差滚动(初始样式)

来自分类Dev

CSS / JS滚动故障效果(性能)

来自分类Dev

仅使用CSS进行视差滚动?

来自分类Dev

使用margin-top滚动视差

来自分类Dev

仅使用CSS进行视差滚动?

来自分类Dev

使用CSS或JavaScript滚动速度更快-视差

来自分类Dev

iOS视差滚动效果(例如Yahoo News Digest应用程序中的效果)

来自分类Dev

使用视差效果时Chrome出现严重延迟

来自分类Dev

视差效果的公式

来自分类Dev

RecyclerView的视差标题效果

来自分类Dev

jQuery视差效果算法

来自分类Dev

平滑视差效果

来自分类Dev

伪元素的视差效果

来自分类Dev

CSS过渡的视差效果