当父窗口从display更改为none时,如何将CSS转换应用于模式窗口



我想显示一个灰色背景的模式窗口(div)(父div)。由于我不理解的原因,应用于模式窗口的转换只有在父元素的显示属性没有更改时才有效。

应该显示转换,因为有一个CSS转换应用于不透明度,我通过添加类"show"将不透明度更改为1。

不透明度已正确更改:我的窗口可见。但是,没有显示任何转换。

Fiddle:http://jsfiddle.net/3CD7U/1/

HTML

<button id="button">click me</button>
<div id="background">
<div id="box">
<h1>Modal window</h1>
<p>I'd expect to see a transition when adding the show class. Unfortunately, the transition is only shown when the parent's display property isn't changed at the same time.</p>
<p id="show-later">Here the transition is applied correctly, because of a delay. To work reliably however, the delay seems to have to be over 100ms, making the UI seem sluggish.<p>
</div>
</div>

CSS

#background{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: silver;
}
#box{
position: relative;
width: 50%;
margin-top: 30px;
margin-left: auto;
margin-right: auto;
padding: 20px;
background: white;
border: 1px solid grey;
opacity: 0;
}
#show-later{
opacity: 0;
}
#box.show,
#show-later.show{
opacity: 1;
transition: opacity 500ms linear;
}

JQuery

$("#background").hide();
$("#button").click( function(){
$("#background").show();
$("#box").addClass("show");
setTimeout( function(){
$("#show-later").addClass("show");
}, 2000);
})

我不想仅仅依赖于更改不透明度,因为旧的浏览器。

我见过其他网站有带转换的模式窗口,但我不明白为什么这些窗口有效,而我的小提琴却无效。

我有一个解决办法:在必须显示模式窗口的那一刻添加一个包含动画的类,但它不如只应用/删除带有转换的CSS类那么干净。

关于如何正确地将转换添加到从显示更改的元素的想法:不欢迎。

解决方案似乎不是使用可见性而不是显示属性。

visibility: hidden;

可以说,它在渲染性能方面甚至更好。它也适用于所有现代浏览器(IE 4+)。

最新更新