背景
( 这一直是 asked before , many times ,我知道。但是,它似乎每次都是由不同的事情引起的。我经历了大约四个不同的 StackOverflow 答案线程并尝试一切。似乎没有任何工作了,所以我相信这是一个新问题。)
无论如何,我有一个带有背景图像的 HMTL 元素需要修复,使用 background-attachment:fixed;
- 所有桌面浏览器 - 有效
- 移动版 Firefox - 有效
- 默认 Android/Samsung 浏览器 - 工作
- 移动版 Chrome - 不工作
我已将问题转化为一个更简单的可复制测试,它是单个 section
元素,设置为页面高度的 200%
,背景为全屏和固定。
代码
html,body {
padding:0;
margin:0;
height:100%;
width:100%;
}
section {
background-position:center center;
background-attachment:fixed;
background-size:cover;
background-image:url(http://www.andymercer.net/wp-content/themes/andymercer/images/background/home.png);
height:200%;
width:100%;
}
<section>Test</section>
用于测试的 JSFiddle
为了在智能手机上比代码 fragment 更容易测试:http://jsfiddle.net/LerLz1L2/
我尝试过的事情
backface-visibility: hidden;
-webkit-backface-visibility:inherit;
-webkit-transform: translate3d(0,0,0);
- 将元素和所有父级设置为
position:static
最佳答案
下面的代码在 android chrome 中对我来说效果很好。
html {
height: 100%;
background: url(bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
我从 here 得到这个
关于android - 背景附件:固定不工作 - Android Chrome (v40),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28532327/