【发布时间】:2019-07-26 11:54:57
【问题描述】:
body {
height: 150vh;
}
#hero {
position: relative;
border: none;
height: 100vh;
}
#hero .hero-image {
background-image: url(https://images.unsplash.com/photo-1518791841217-8f162f1e1131);
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
height: 95%;
}
#hero .hero-image:after {
content: "";
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: rgba(0,0,0,0.2);
}
#hero .skewhero-mask {
height: 100%;
position: absolute;
top: 0;
left: 10vw;
width: 45vw;
overflow: hidden;
transform: skew(24deg) translateX(0vh) translateY(0%);
}
#hero .skewhero-parallax {
transform: translateX(0vh);
width: 200%;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
#hero .skewhero-image {
background-image: url(https://images.unsplash.com/photo-1518791841217-8f162f1e1131);
height: 100%;
background-size: 110% auto;
background-attachment: fixed;
transform-origin: right top;
transform: skew(-24deg);
}
<section id="hero">
<div class="hero-image">
</div>
<div class="skewhero-mask">
<div class="skewhero-parallax">
<div class="skewhero-image"></div>
</div>
</div>
</section>
我真的很喜欢这个。我正在设计一个视差效果,我使用 jQuery 移动固定背景的background-position 属性。 jQuery在这里没有错,所以我不会在这里包含它以降低问题的复杂性。
在 Chrome 中,我得到了想要的效果。在 Firefox 或 Edge 中,这是一场噩梦。我还没有在 Opera 上测试过它。在这些浏览器中从 .skewhero-image 类中删除 background-attachment: fixed 时,我注意到没有任何区别。该属性没有做任何事情,因为当我在 Chrome 中删除相同的属性时,我会得到与其他浏览器相同的不良结果。
如何更改我的代码以实现与现在在 Chrome 中完全相同的效果,但在所有其他桌面浏览器中也是如此?不包括移动浏览器。
基本上,猫的图像必须不移动,只有围绕它的容器。在 Chrome 中,这可以按预期工作。在 Firefox 或 Edge 中,猫随容器移动,而不是固定在视口上。
编辑:我发现从自身和所有父级中省略 all transform 属性会将图像固定到视口。有什么办法补救吗?
【问题讨论】:
-
你能分享你的代码吗?
标签: css cross-browser css-transforms background-position background-attachment