今天我遇到了一个问题,我正在重复使用一个position: fixed
的模态框。我无法将其设置为display:none
,然后再进行动画处理,因为它会突然出现,并且z-index
(负值等)也会发生奇怪的事情。
我还使用了height:0
到 height:100%
,但是它只在模态框出现时起作用。这与使用left:-100%
或类似的效果相同。
然后我想到了一个简单的答案。看这里:
首先,你的隐藏模态框。请注意height
是0
,并检查在过渡中的height
声明……它有一个500ms
,比我的opacity
过渡时间长。请记住,这会影响退出淡出过渡:将模态框返回到其默认状态。
#modal-overlay {
background: #999;
background: rgba(33,33,33,.2);
display: block;
overflow: hidden;
height: 0;
width: 100%;
position: fixed;
top: 0;
left: 0;
opacity: 0;
z-index: 1;
-webkit-transition: height 0s 500ms, opacity 300ms ease-in-out;
-moz-transition: height 0s 500ms, opacity 300ms ease-in-out;
-ms-transition: height 0s 500ms, opacity 300ms ease-in-out;
-o-transition: height 0s 500ms, opacity 300ms ease-in-out;
transition: height 0s 500ms, opacity 300ms ease-in-out;
}
其次,你的可见模态框。假设你将 .modal-active
设置到 body
上。现在 height
是 100%
,并且我的过渡也已经改变了。我希望 height
立即改变,并且 opacity
花费 300ms
时间进行过渡。
.modal-active #modal-overlay {
height: 100%;
opacity: 1;
z-index: 90000;
-webkit-transition: height 0s, opacity 300ms ease-in-out;
-moz-transition: height 0s, opacity 300ms ease-in-out;
-ms-transition: height 0s, opacity 300ms ease-in-out;
-o-transition: height 0s, opacity 300ms ease-in-out;
transition: height 0s, opacity 300ms ease-in-out;
}
就是这样,它像魔法一样工作。
z-index:0
的内容时才行。 - DanManvisibility: hidden
,除非你希望屏幕阅读器读取它(而典型的浏览器不会)。它仅定义元素的可见性(就像说opacity: 0
一样),它仍然是可选、可点击和以前的任何状态;它只是不可见。 - Forest Katschpointer-events
,因此并不总是可行。 - Steven Pribilinskiy