问题
Why is background-size:cover
different if background-attachment: scroll
or background-attachment: fixed
used?
Example:
http://jsfiddle.net/enriqg9/Yn43U/
回答1:
The difference isn't really in background-size: cover
. The difference between background-attachment: scroll
and background-attachment: fixed
is that
"...scroll means that the background is fixed with regard to the element itself and does not scroll with its contents. (It is effectively attached to the element's border.)
"...fixed means that the background is fixed with regard to the viewport. Even if an element has a scrolling mechanism, a ‘fixed’ background doesn't move with the element."
as MDN says. So you'll see in your fiddle that the background-attachment: fixed
background doesn't remain in its containing element <div id="two">
border. It is, instead taking on the fixed point of absolute positioning 0, 0 in the entire body's background.
In essence, background-attachment: fixed
is overwriting background-size: cover
and not allowing the latter style to take effect.
回答2:
When you assign background-size:cover
to a background-attachment: fixed
item its container will be the actual view port the item is sitting in. In your case the cat image is stretched to fit the total width of the fiddle result box. The reason why it is this way might be because since it applies position absolutely to the viewport it also gathers the size required from the viewport.
来源:https://stackoverflow.com/questions/23050326/css-background-attachment-scroll-fixed-and-background-size-cover