html - 固定背景图像和绝对定位包装
问题描述
我只在 Google Chrome 上遇到问题。基本 CSS 样式在两个浏览器上的行为不同,我不明白为什么。
这是 Firefox 上的行为,这是预期的结果:
这是 Chrome 上的行为,这不好,因为图像在滚动时下降:
您可以在此 JSFiddle 上重现它:https ://jsfiddle.net/zcugdayv/26/
我试图将代码减少到最少以解决问题:
.app {
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
}
.parallax-wrapper {
height: 100vh;
background: url('http://m0.libe.com/blogs/cache/3e/9f/3e9f3c94db337827f6d1d0a859a433f4.jpg') fixed center;
background-size: cover;
}
.normal-wrapper {
height: 600px;
background: green;
}
<div class="app">
<div class="parallax-wrapper"></div>
<div class="normal-wrapper"></div>
</div>
我知道如果我不在课堂上使用 a position: absolute;
,app
它会再次起作用,但出于不同的原因我需要它。
你认为这是谷歌浏览器本身的一个错误,它会在未来的版本中得到修复,或者它是谷歌浏览器本身出于特定原因实现的新东西?
如果 Google Chrome 上的行为正常,为什么图像会下降?我所看到的数学是什么?
解决方案
我不确定为什么在 Chrome 中会发生这种情况,但我已经为你做了一个解决方法。将以下内容添加到每个规则中,它应该可以正常工作。
.parallax-wrapper {
position: fixed;
top: 0;
width: 100%;
}
.normal-wrapper {
margin-top: 100vh;
z-index: 1;
position: relative;
}
推荐阅读
- qt - 如何为 pro 文件中不存在的文件使用通配符制作安装规则?
- c# - IOException - 另一个进程正在使用 FileShare.ReadWrite 使用的文件
- c++ - C++ Order of class members when smart pointers are involved matters
- loopbackjs - 在将 opentelemetry 跟踪与环回 4 集成时面临多个问题
- excel - Excel 工作簿到 Word 模板 + 邮件合并 VBA
- matlab - 如何在 MATLAB App Designer GUI 中实现数据流?
- javascript - Vue.js 2:如何将异步回调\承诺返回到自定义 $emit 事件
- php - Doctrine - 嵌套实体子目录/命名空间
- java - 什么是通过继承获得正确返回类的好习惯?
- excel - 将单列中的多个数据合并到excel中的单行