简体   繁体   English

用z-index和position解释这种模态行为?

[英]Explain this modal behavior with z-index and position?

I actually solved this issue for myself but was wondering if anyone could explain why my fix works. 我实际上是为自己解决了这个问题,但是想知道是否有人能解释为什么我的修复工作正常。 I was styling a popup modal on a mobile site using the MoovWeb SDK and sass. 我使用MoovWeb SDK和sass在移动网站上设置了一个弹出模式。 The modal consists of a mask div, which is located immediately inside a container div, and the modal div itself, which was buried more deeply in the DOM. 模态由一个掩码div组成,它直接位于一个容器div中,而modal div本身则深埋在DOM中。

mask styling: 面具造型:

#modalMask{
  opacity: .8;
  position: absolute;
  display:none;
  height:100%;
  width: 100%;
  z-index:9990;
}

modal styling: 模态造型:

.mw-popup-modal { 
  top: 80px !important;
  left: 0 !important;
  position:fixed;
  z-index:9999;
  display:none;
}

This resulted in the mask sitting on top of the modal and the buttons inside the modal being un-tappable - their tap area was actually located around 3cm below where the button was appearing onscreen on the phone. 这导致面具位于模态顶部,并且模态内的按钮是不可拆卸的 - 他们的拍子区域实际上位于手机屏幕上按钮出现的下方3cm处。 However, on the desktop version of the site, this styling looked fine. 但是,在该网站的桌面版本上,这种样式看起来很好。

When I changed the positioning of the modal from "fixed" to "absolute" this fixed the problem, but I'd like to understand why. 当我将模态的定位从“固定”更改为“绝对”时,这解决了问题,但我想了解原因。 Do fixed- and absolute-positioned elements each work on their own z-index stack? 固定和绝对定位元素是否都在自己的z-index堆栈上工作?

Official W3 documentation states 官方W3文件说明

Fixed positioning is a subcategory of absolute positioning. 固定定位是绝对定位的子类别。 The only difference is that for a fixed positioned box, the containing block is established by the viewport. 唯一的区别是,对于固定定位的框,包含块由视口建立。 For continuous media, fixed boxes do not move when the document is scrolled. 对于连续介质,滚动文档时固定框不会移动。 In this respect, they are similar to fixed background images. 在这方面,它们类似于固定的背景图像。

Absolute and Fixed positioning do not work on their own z-index stack context. 绝对和固定定位不适用于他们自己的z-index堆栈上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM