Since a fixed positioned background seems to break for no reason in Chrome, you can potentially try playing around with the clip
and position:fixed
properties. It's not very well known, but the clip property when set on an absolute positioned element will actually even crop fixed positioned child elements.
There are some drawbacks, however. Most importantly, this trick sadly doesn't work flawlessly on iOS for some reason, whereas the browser has troubles rendering the entire image while the user is scrolling (you kinda get a pop-in effect). It's not something overly major, but perhaps something you should take in regard. Of course, you can still work around this by using for example some clever javascript that falls back to a fixed background. Another iOS workaround is by merely using -webkit-mask-image: -webkit-linear-gradient(top, #ffffff 0%,#ffffff 100%)
which is basically a webkit-specific alternative for clip: rect(auto,auto,auto,auto)
(i.e. crop everything outside the container).
I made a JSFiddle (codepen didn't want to play with me) implementation example for how you can do this. Look specifically at .moment
, .moment-image
and the new .moment-clipper
.
I hope this is of some help!
Update: Clip is now deprecated in favour of clip-path, but is as of writing still supported in all browsers. The same effect can however be achieved with:
-webkit-clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
overflow: hidden;
position: absolute
is no longer required on the container. Support for clip-path seems to be relatively limited, with only Chrome and Safari currently supporting it with prefixes. The safest bet is probably to include both clip and clip-path since they don't appear to interfere with each other.
I've updated the fiddle above to also include clip-path.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…