【问题标题】:background-attachment: fixed for <img> with object-fit cover?背景附件:为 <img> 修复了适合对象的封面?
【发布时间】:2020-07-12 18:30:17
【问题描述】:

是否有技巧或解决方法可以使用object-fit:cover 和父级中的绝对定位制作图像,使其表现得像css 中的background-attachment: fixed

我想要这个:

<section>
    <img src="" style="object-fit: cover; position: absolute; width: 100%; height: 100%;"
</section>

具有与此相同的功能:

<section style="background: url(..); background-attachment: fixed;">
<section>

【问题讨论】:

  • 也许背景尺寸:封面; ?
  • @shai_sharakanski 我用 html 更新了问题,我在问如何使 img 表现得像 background-attachment:fixed
  • @TemaniAfif 如果有多个这样的部分,它仍然有效吗?这需要弄乱 z-index 吗?

标签: html css


【解决方案1】:

position:fixed 结合 clip-path 技巧可以做到

section {
  height: 100px;
  margin: 30px 5px;
  clip-path: inset(0);
}

body {
  min-height: 300vh;
}
<section>
  <img src="https://picsum.photos/id/1/800/800" 
    style="object-fit: cover; 
           position: fixed;
           left:0;
           top:0;
           width: 100%; 
           height: 100%;">
</section>

<section 
  style="
    background-image: url(https://picsum.photos/id/1/800/800); 
    background-attachment: fixed;
    background-size:cover;
    background-position:center;">
</section>

【讨论】:

  • 谢谢!它会在任何地方都可以工作还是某些浏览器不支持剪辑路径?
  • @Runnick only IE 不支持剪辑路径,但这并不奇怪,它是一个死机浏览器。所有其他浏览器都支持它
猜你喜欢
  • 2023-03-15
  • 2018-11-21
  • 1970-01-01
  • 1970-01-01
  • 2013-11-12
  • 2014-08-15
  • 1970-01-01
  • 2018-10-13
  • 2014-05-27
相关资源
最近更新 更多