我有一堆应用于父元素及其子元素的 CSS:
.parent {
position: fixed;
top: 0px;
}
.el {
position: fixed;
top: 5px;
z-index: 100;
}
.bodycontent {
z-index: 1;
position: relative;
}<div class="parent">
<div class="el">
<button></button>
</div>
</div>
<div class="bodycontent"></div>
页面被制作成当它滚动时,.parent 位于 .bodycontent 下方,而 .el 位于其上方。这在 Firefox 中可以正常工作,但在 Chrome 中却不行。
有什么建议吗?我试过弄乱不同的 z-index 值和不同的 position 值,但没有成功。
最佳答案
从 version 22 开始,这是 Chrome 特意处理 fixed 元素堆叠的方式。正如谷歌自己在一篇文章中所述:
In Chrome 22 the layout behavior of
position:fixedelements is slightly different than previous versions. Allposition:fixedelements now form new stacking contexts. This will change the stacking order of some pages, which has the potential to break page layouts.
Firefox 也在按预期工作。 Mozilla 文档指出此行为仅限于移动 WebKit 和 Chrome 22 及更高版本:
on mobile WebKit and Chrome 22+, position: fixed always creates a new stacking context, even when z-index is "auto"
( https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context )
此更改的结果意味着即使父容器的 z-index 设置为 auto<,chrome>,chrome>(默认值)。这与 position: absolute; 和 position: relative; 不同,因为它们仅在 z-index 不是时形成自己的堆叠上下文 设置为 auto。
Most elements on a page are in a single, root stacking context, but absolutely or relatively positioned elements with non-auto
z-indexvalues form their own stacking contexts (that is, all of their children will be z-ordered within the parent and not be interleaved with content from outside the parent). As of Chrome 22,position:fixedelements will also create their own stacking contexts.
这意味着在您的示例中 .el 的 z-index 是相对于其父级 .parent 计算的。它显示在 .bodycontent 下,因为:
.bodycontent的z-index是相对于root.el的z-index是相对于.parent.parent的z-index是相对于根.parent 的 z-index 未指定,因此它被设置为默认的 auto(实际上,0 ).parent 的 z-index 低于 .bodycontent,因此显示在其下方。因为.el属于它,它也显示在.bodycontent下。body {
margin: 0;
}
div {
height: 100px;
width: 100px;
}
.parent {
background-color: red;
position: fixed;
top: 0;
}
.el {
background-color: blue;
left: 25px;
position: fixed;
top: 25px;
z-index: 100;
}
.bodycontent {
background-color: green;
left: 50px;
position: relative;
top: 50px;
z-index: 1;
}<div class="parent">
<div class="el"></div>
</div>
<div class="bodycontent"></div>
以上代码将在 Chrome 和 Firefox 中产生以下结果:
Chrome 似乎没有遵循 W3C 规范,进行此更改是为了让桌面实现与移动实现相匹配:
Mobile browsers (Mobile Safari, Android browser, Qt-based browsers) put position:fixed elements in their own stacking contexts and have for some time (since iOS5, Android Gingerbread, etc) because it allows for certain scrolling optimizations, making web pages much more responsive to touch. The change is being brought to desktop for three reasons:
1 - Having different rendering behavior on “mobile” and “desktop” browsers is a stumbling block for web authors; CSS should work the same everywhere when possible.
2 - With tablets it isn’t clear which of the “mobile” or “desktop” stacking context creation algorithms is more appropriate.
3 - Bringing the scrolling performance optimizations from mobile to desktop is good for both users and authors.
Firefox 正在以正确的方式处理堆叠。
唯一可以规避此行为的方法是将 .el 移出 .parent 并使其成为同级:
body {
margin: 0;
}
div {
height: 100px;
width: 100px;
}
.parent {
background-color: red;
position: fixed;
top: 0;
}
.el {
background-color: blue;
left: 25px;
position: fixed;
top: 25px;
z-index: 100;
}
.bodycontent {
background-color: green;
left: 50px;
position: relative;
top: 50px;
z-index: 1;
}<div class="parent"></div>
<div class="el"></div>
<div class="bodycontent"></div>
关于html - z-index 行为在 chrome 和 firefox 中是不同的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31646746/
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
我正在学习http://ruby.railstutorial.org/chapters/static-pages上的RubyonRails教程并遇到以下错误StaticPagesHomepageshouldhavethecontent'SampleApp'Failure/Error:page.shouldhave_content('SampleApp')Capybara::ElementNotFound:Unabletofindxpath"/html"#(eval):2:in`text'#./spec/requests/static_pages_spec.rb:7:in`(root)'
我正在尝试将一个简单的CSV文件读入HTML表格以在浏览器中显示,但我遇到了麻烦。这就是我正在尝试的:Controller:defshow@csv=CSV.open("file.csv",:headers=>true)end查看:输出:NameStartDateEndDateQuantityPostalCode基本上我只获取标题,而不会读取和呈现CSV正文。 最佳答案 这最终成为最终解决方案:Controller:defshow#OpenaCSVfile,andthenreaditintoaCSV::Tableobjectforda
两个gsub产生不同的结果。谁能解释一下为什么?代码也可在https://gist.github.com/franklsf95/6c0f8938f28706b5644d获得.ver=9999str="\tCFBundleDevelopmentRegion\n\ten\n\tCFBundleVersion\n\t0.1.190\n\tAppID\n\t000000000000000"putsstr.gsub/(CFBundleVersion\n\t.*\.).*()/,"#{$1}#{ver}#{$2}"puts'--------'putsstr.gsub/(CFBundleVersio
我在一段非常简单的代码(如我所想)中得到了一个错误的值:org=4caseorgwhenorg=4val='H'endputsval=>nil请不要生气,我希望我错过了一些非常明显的东西,但我真的想不通。谢谢。 最佳答案 这是典型的Ruby错误。case有两种被调用的方法,一种是你传递一个东西作为分支的基础,另一种是你不传递的东西。如果您确实在case中指定了一个表达式语句然后评估所有其他条件并与===进行比较.在这种情况下org评估为false和org===false显然不是真的。所有其他情况也是如此,它们要么是真的,要么是假的。