【发布时间】:2013-08-22 22:33:33
【问题描述】:
这里是一个例子:http://jsfiddle.net/MyDkR/8/
代码如下:
.fixed {
height: 250px;
width: 250px;
background: #000 url('http://placekitten.com/200/600') no-repeat;
background-size: auto 100%;
background-attachment: fixed;
}
.notfixed {
height: 250px;
width: 250px;
background: #000 url('http://placekitten.com/200/600') no-repeat;
background-size: auto 100%;
}
顶部元素不保留 background-size 属性,它声明背景的高度将等于元素的高度。
底部元素是相同的,只是它没有 background-attachment: fixed 规则。
这是渲染错误吗?我已经在最新版本的 IE 和 Chrome 中对其进行了测试。如果是这样,有没有办法更新背景的大小?
【问题讨论】:
-
如果我正确理解MDN:background-size 的部分,这是正确的行为:
If the background's attachment is fixed, the background positioning area is instead the entire area of the browser window, not including the area covered by scrollbars if they are present. Negative percentages are not allowed.这个问题可能会有所帮助:stackoverflow.com/q/15713668/1960455 -
@t.niese 发布作为答案。要查看效果,您可以调整窗口大小,其行为与定义的行为完全相同。还要注意调整背景大小以获得完整答案的可能方法
-
^^ 如果您将
background-attachment: fixed移动/添加到.notfixed元素,您将看到类似的行为。更好地理解这些行为的一个好方法是在不同的组合中尝试它们,看看它们做了什么,然后返回并重新阅读规范和/或 MDN 文档 - 然后不断重复。 -
@Zeaklous 我有点没时间了(无法做出包括解决方案在内的完整答案)。所以我只是将它添加为评论,以便它至少得到部分回答。
-
@t.niese 希望大家等到你发帖,因为这是唯一正确的答案