首页 文章

当父显示从display:none更改时,如何将CSS转换应用于模态窗口?

提问于
浏览
0

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

应该显示转换,因为应用了不透明度的CSS转换,我通过添加类'show'将不透明度更改为1 .

正确更改了不透明度:我的窗口可见 . 但是,没有显示过渡 .

小提琴: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类一样干净 .

如何正确地将转换添加到从display:none更改的元素的想法是受欢迎的 .

1 回答

  • 0

    看来,而不是使用可见性而不是显示属性是the solution .

    visibility: hidden;
    

    可以说它甚至可以用于渲染性能better . 它也适用于所有现代浏览器(IE 4) .

相关问题