热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

位置:固定位置:固定:哪个浏览器正确?-Position:fixedwithinposition:fixed:whichbrowseriscorrect?

PositioningafixedelementinsideanotherfixedelementisbehavingdifferentlyinChromeSafari

Positioning a fixed element inside another fixed element is behaving differently in Chrome / Safari vs Firefox.

将固定元素定位在另一个固定元素中的行为在Chrome / Safari和Firefox中表现不同。

This answer explains well the expected behavior for a fixed div inside a relative one, and MDN is pretty clear on this:

这个答案很好地解释了相对一个内部固定div的预期行为,MDN对此很清楚:

Fixed Positioning Do not leave space for the element. Instead, position it at a specified position relative to the screen's viewport and don't move it when scrolled. When printing, position it at that fixed position on every page.

固定定位不要为元素留出空间。而是将其定位在相对于屏幕视口的指定位置,并且在滚动时不要移动它。打印时,将其放置在每页的固定位置。

What I don't understand is what Firefox is doing with a fixed div inside a fixed div. What I expect is that the child element moves along with the wrapper on hover.

我不明白的是Firefox正在使用固定div中的固定div做什么。我期望的是,子元素在悬停时与包装器一起移动。

.wrapper, .header {
  position:fixed;
  width:320px;
}

.wrapper:hover{
  left:0px;
}
.wrapper{
  width:320px;
  height:100%;
  background:white;
  overflow:scroll;
  left:-200px;
  transition: all ease-out .3s;
}
ul {
  margin-top:120px;
}
 .header {
   background:rgba(255,255,255,0.9);
}

body{
  background:gray;
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repudiandae vitae a, itaque commodi, odio et. Excepturi, obcaecati? Architecto repellendus omnis mollitia animi rem quasi at, odit aperiam voluptatibus voluptates earum!
  • Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusantium quam maiores, voluptas facere, iste quis iusto reiciendis delectus, quod blanditiis tempora. Earum voluptatum dicta quae, explicabo placeat at rerum assumenda!
  • Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusantium quam maiores, voluptas facere, iste quis iusto reiciendis delectus, quod blanditiis tempora. Earum voluptatum dicta quae, explicabo placeat at rerum assumenda!
  • Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusantium quam maiores, voluptas facere, iste quis iusto reiciendis delectus, quod blanditiis tempora. Earum voluptatum dicta quae, explicabo placeat at rerum assumenda!
  • Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusantium quam maiores, voluptas facere, iste quis iusto reiciendis delectus, quod blanditiis tempora. Earum voluptatum dicta quae, explicabo placeat at rerum assumenda!

Any thoughts? I'm looking for a workaround to have consistency across browsers, too.

有什么想法吗?我正在寻找一种解决方法,以便在浏览器之间实现一致性。

Edit : more fun?

Add this to glitch it even more on FF :

添加这个以便在FF上出现更多故障:

.header:hover{
  height:200px;
}

On hover, it triggers a repaint, then FF recalculate the position of the element.

在悬停时,它会触发重绘,然后FF重新计算元素的位置。

Tests made with FF 46.0.1, Chrome 54.0.2840.71 and Safari Version 9.1.1 (11601.6.17). Note: I already read this question

使用FF 46.0.1,Chrome 54.0.2840.71和Safari版本9.1.1(11601.6.17)进行测试。注意:我已经读过这个问题

2 个解决方案

#1


5  

To see two workarounds for the behavior you want, scroll down below the horizontal rule.


11/18/16 Update - The CSSWG got back to me and said that it should create a new stacking context:

11/18/16更新 - CSSWG回复我并说它应该创建一个新的堆叠上下文:

You're right, this was supposed to be merged into the positioning spec as well - reflected now. Thanks.

你是对的,这也应该被合并到定位规范中 - 现在反映出来。谢谢。

  • New CSS Position specification diff
  • 新的CSS位置规范差异


On the subject of which browser is correct:

关于哪个浏览器是正确的:

fixed position elements should always be placed relative to the viewport, specifically that the position: fixed element's containing block is established "by the viewport" in 10.1.3:

固定位置元素应始终相对于视口放置,特别是位置:固定元素的包含块是由10.1.3中的“视口”建立的:

If the element has 'position: fixed', the containing block is established by the viewport [...]

如果元素具有'position:fixed',则包含块由视口[...]建立

This containing block is formally called the "initial containing block".

这个包含块正式称为“初始包含块”。

9.3.1 also backs this up by saying that, for normal non-paged media (like this),

9.3.1还说明了这一点,对于普通的非分页媒体(像这样),

[...] In the case of handheld, projection, screen, tty, and tv media types, the box is fixed with respect to the viewport and does not move when scrolled.

[...]对于手持,投影,屏幕,tty和tv媒体类型,该框相对于视口是固定的,滚动时不会移动。

What's happening in your code is that you are changing the value of the left property of the parent element on hover, and you are expecting the child element to move, too. However, the child element is (properly) not moving.

您的代码中发生的是您在悬停时更改父元素的left属性的值,并且您也希望子元素也可以移动。但是,子元素(正确)不移动。

10.3.7 says

For the purposes of calculating the static position, the containing block of fixed positioned elements is the initial containing block instead of the viewport.

出于计算静态位置的目的,固定定位元素的包含块是初始包含块而不是视口。

(static position here meaning the position of the element if it were placed in the normal flow).

(此处的静态位置表示元素放置在正常流中时的位置)。

It also says:

它还说:

[If] 'left' and 'right' are 'auto' and 'width' is not 'auto', [...] set 'left' to the static position, otherwise set 'right' to the static position. Then solve for 'left' (if 'direction is 'rtl') or 'right' (if 'direction' is 'ltr').

[If]'left'和'right'是'auto'且'width'不是'auto',[...]将'left'设置为静态位置,否则将'right'设置为静态位置。然后解决'左'(如果'方向是'rtl')或'右'(如果'方向'是'ltr')。

This explains, I believe, why the child position: fixed element is initially set to left: -200px; per where it would be within its parent element if it were position: static.

我相信,这解释了为什么子位置:固定元素最初设置为左:-200px;如果它是position:static,那么它在其父元素中的位置。

At this point, it looks like you believe the parent's new left value should move the child element, I'm assuming, either because you expect the new left property to be inherited by the child (which is not how left works), or you expect it to re-flow the document, which doesn't happen on :hover as I recall; the browser only re-paints on :hover, which doesn't change the document flow, but does change the appearance of elements (e.g. background-color, opacity, visibility: hidden; etc).

在这一点上,看起来你认为父的新左值应该移动子元素,我假设,或者是因为你希望新的左边属性被子节点继承(这不是左边的工作),或者你期待它重新流动文件,这不会发生:我记得悬停;浏览器只重新绘制:悬停,它不会改变文档流,但确实会改变元素的外观(例如背景颜色,不透明度,可见性:隐藏等)。

So... elements on re-paint shouldn't move unless there are pseudo-selectors that change the properties during temporary states (like :hover), or transitions/animations at play.

因此,重新绘制的元素不应该移动,除非有伪选择器在临时状态(例如:悬停)或游戏中的过渡/动画期间更改属性。

In this situation, it appears that Chrome and Safari are doing something other than what the spec suggests; they are either causing a full re-flow, or they have set position: fixed elements to inherit left properties from ancestors. This appears to be above the board, if you will, according to the CSS Working Group draft linked by Oriol below. However, it's still non-standard behavior until the spec is updated.

在这种情况下,Chrome和Safari似乎正在做一些不同于规范建议的事情;它们要么导致完全重新流动,要么它们设置了位置:固定元素以从祖先继承左侧属性。根据Oriol下面链接的CSS工作组草案,如果你愿意,这似乎是在董事会之上。但是,在规范更新之前,它仍然是非标准行为。

  • Long-story short, Chrome and Safari are wrong right now, but eventually once the spec is updated, they will be correct, and Firefox will have to update its rendering behavior.
  • 长话短说,Chrome和Safari目前都是错误的,但最终一旦规范更新,它们就会正确,Firefox将不得不更新其渲染行为。


Make the .header div inherit your new left property, since that's how Chrome is doing it and that is the behavior you seek. I also adjusted .header's width just a bit, so that it won't cover the scroll bar on .wrapper:

让.header div继承你的新左侧属性,因为Chrome就是这样做的,这就是你寻求的行为。我也调整了.header的宽度,所以它不会覆盖.wrapper上的滚动条:

.wrapper, .header {
  position: fixed;
}

.wrapper:hover {
  left:0px;
}
.wrapper{
  width:320px;
  height:100%;
  background:white;
  overflow:scroll;
  left:-200px;
  transition: all ease-out .3s;
}
ul {
  margin-top:120px;
}
.header {
  background:rgba(255,255,255,0.9);
  left: inherit;
  width: 303px;
}

body{
  background:gray;
}
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repudiandae vitae a, itaque commodi, odio et. Excepturi, obcaecati? Architecto repellendus omnis mollitia animi rem quasi at, odit aperiam voluptatibus voluptates earum!
  • Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusantium quam maiores, voluptas facere, iste quis iusto reiciendis delectus, quod blanditiis tempora. Earum voluptatum dicta quae, explicabo placeat at rerum assumenda!
  • Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusantium quam maiores, voluptas facere, iste quis iusto reiciendis delectus, quod blanditiis tempora. Earum voluptatum dicta quae, explicabo placeat at rerum assumenda!
  • Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusantium quam maiores, voluptas facere, iste quis iusto reiciendis delectus, quod blanditiis tempora. Earum voluptatum dicta quae, explicabo placeat at rerum assumenda!
  • Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusantium quam maiores, voluptas facere, iste quis iusto reiciendis delectus, quod blanditiis tempora. Earum voluptatum dicta quae, explicabo placeat at rerum assumenda!

#2


-2  

So, I think problem has arisen due to the bug in implementation of left in Firefox.

所以,我认为由于Firefox中实现左边的错误而出现了问题。

When hovering .wrapper, .header should get new left value from its parent .wrapper i.e. 0px. When hovering .wrapper, the left position of .header should be calculated using the left value from its parent .wrapper as no explicit left value is given to .header.

当悬停.wrapper时,.header应该从其父.wrapper获得新的左值,即0px。当悬停.wrapper时,.header的左侧位置应使用其父.wrapper中的左值计算,因为没有给.header明确的左值。

I think its due to bug in Firefox. If you activate :hover pseudo class of .wrapper using Firebug or the default developer tool, the left position of .header is maintained like in Chrome (but in sudden manner).

我认为这是由于Firefox中的错误。如果你使用Firebug或默认的开发人员工具激活:.wrapper的hover伪类,.header的左侧位置就像在Chrome中一样(但是以突然的方式)。

Tested on Firefox 49.0.2 and Chrome 54.0.2840.71

在Firefox 49.0.2和Chrome 54.0.2840.71上测试过


推荐阅读
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文整理了Java中org.apache.solr.common.SolrDocument.setField()方法的一些代码示例,展示了SolrDocum ... [详细]
  • 工作经验谈之-让百度地图API调用数据库内容 及详解
    这段时间,所在项目中要用到的一个模块,就是让数据库中的内容在百度地图上展现出来,如经纬度。主要实现以下几点功能:1.读取数据库中的经纬度值在百度上标注出来。2.点击标注弹出对应信息。3 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了css回到顶部按钮相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 涉及的知识点-ViewGroup的测量与布局-View的测量与布局-滑动冲突的处理-VelocityTracker滑动速率跟踪-Scroller实现弹性滑动-屏幕宽高的获取等实现步 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
  • 本文整理了Java中com.evernote.android.job.JobRequest.getTransientExtras()方法的一些代码示例,展示了 ... [详细]
  • 本文整理了Java中org.apache.ibatis.mapping.ParameterMapping.getNumericScale()方法的一些代码示例,展示了 ... [详细]
  • vb.net面试题,请大家帮忙,谢谢。如果需要讲详细一点,那就加我QQ531412815第4题,潜在的错误,这里的错误不是常规错误,属于那种只有在运行是才知道的错误:Catchex ... [详细]
author-avatar
keleesen
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有