【发布时间】:2016-10-26 15:56:30
【问题描述】:
我正在构建一个使用视差背景的网站。它在我的 Mac 上的 Safari 和 Chrome 中运行良好,但 background-size: cover 在移动设备上根本不起作用。 图片放大,视差功能关闭。
我一直在寻找一个简单的解决方案,但在任何地方都找不到。 只要背景图片具有适当的尺寸,我很乐意在移动设备上失去视差功能。
帮助? 附上基本代码。
HTML:
<body>
<div id="bg01" class="bg01">
</div>
<div class="divwrapper">
<div class="div" id="div">
CONTENT
</div>
</div>
<div id="bg02" class="bg02">
</div>
</body>
CSS:
body, html {
height: 100%;
margin: 0;
padding: 0;
font-size: 0;
background-color: red;
}
.divwrapper {
display: table;
width: 80%;
margin:auto;
font-size: 12px;
line-height: 150%;
text-align: center;
}
.div {
height:300px;
}
.bg01 {
background-image: url("https://s-media-cache-ak0.pinimg.com/originals/52/cc/af/52ccaf818ddc056e12e522b9395dd87d.jpg");
height: 100%;
width: 100%;
background-attachment: fixed;
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
}
.bg02 {
background-image: url("http://blog.hostbaby.com/backgrounds/bg_lightwoodfloor.jpg");
height: 100%;
width: 100%;
background-attachment: fixed;
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
}
编辑/更新
我尝试过使用不同的媒体查询,至少通过将 div 背景图像更改为较小的图像来获得正确的比例,但这根本不起作用(它仍然使用放大的大图像):
@media (max-width: 600px) {
.bg01 {
background-image: url("../images/bg_small.jpg");
background-size: 20% 20%;
background-repeat: repeat;
}
}
我也尝试过最小宽度和最大宽度。
【问题讨论】:
-
您的代码没有任何视差。视差通常涉及 JavaScript 来处理背景的偏移移动。
-
@j08691 实际上不,视差可以使用浏览器设备的内置 GPU 完全通过 3D CSS 完成,因为手机通常没有 GPU,这意味着视差在手机上的观看效果最好。 reference
-
嗯,至少有点视差吧?那种功能。但是,如果这对于移动设备来说是可行的,那么我可以将这些 div 放在一起。在桌面上看起来不错。