html - 在这种情况下,为什么 `position: fixed` 是相对于文档而不是视口(viewport)的?

标签 html css

我有一个画廊,里面有我创建的某种灯箱。 灯箱是 position:fixedtop:0。所以,如果我是对的,它应该在同一个位置,不管我是否向下滚动。不幸的是,情况并非如此。

我无法在 fiddle 中重现该问题。您可以在这里查看:http://tobiasgla.us/portfolio .您可以在手机上打开,也可以使用 Chrome 中的设备工具栏。

以下 2 个对象获得了 position:fixed 属性,该属性不起作用:

.item-content{
   width: calc(100% - 100px);
   height: calc(100vh - 100px);
   position: fixed;
   top: 0;
   left: 0;
   margin: 50px;
}
.dupe.dupAnim{
   top:0;
   left:0;
   width:calc(100% - 100px);
   height:250px;
   margin:50px;
   position:fixed;
}

感谢您的帮助!

最佳答案

来自 MDN :

When an ancestor has the transform or perspective property set to something other than none, that ancestor is used as the container instead of the viewport

你的 <div class="container">使用包含 transform: translateY(...) 的动画,这符合文档中列出的异常。因此,您的 fixed元素是相对于主容器的,它确实会随页面滚动。

关于html - 在这种情况下,为什么 `position: fixed` 是相对于文档而不是视口(viewport)的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47633404/

相关文章:

html - 不同的边框图像顶部和底部

html - 浏览器无法识别字符串集 CSS 属性

html - 为什么 inner tbody 不拉伸(stretch)?

html - 如何使用 twitter-bootstrap 删除跨度上的水平滚动?

css - Django + Mac osx 如何少用css?

javascript - 几个div,如何查找图像并删除父div

javascript - 如何将 JS 动画限制在你点击进入的输入字段,而不是全部?

html - 相对定位的 parent 不作为绝对定位的 child 的 anchor

css - 如何覆盖 CSS

html - 不使用背景图像的纯 CSS 图像悬停