简体   繁体   English

CSS过渡不透明度在元素具有display:none的地方不起作用,然后更改为display:block

[英]css transition opacity is not working where element had display:none then changed to display:block

Like the title said. 就像标题说的那样。 I have this code: https://jsfiddle.net/fwo9ym1o/ 我有这个代码: https : //jsfiddle.net/fwo9ym1o/

//javascript
    var container = document.querySelector("#container");

    container.style.display = "block";

    //this is not working
    //container.style.opacity = 1;


    //this is working
    setTimeout(function() {
       container.style.opacity = 1;
    }, 0);

/css
    .container {
        height: 200px;
        width: 200px;
        background-color: salmon;
        display: none;
        border-radius: 5px;
        opacity: 0;
        transition: opacity 2s ease-in-out;
    }

//html
    <div id="container" class="container"></div>

So, I've changed the container.style.display = "block"; 因此,我更改了container.style.display = "block"; then applied container.style.opacity = 1; 然后应用container.style.opacity = 1; and the transition is not happening. 而且过渡没有发生。

It works if I run everything in a new thread. 如果我在新线程中运行所有程序,它将起作用。

NOTE: I can't use visibility. 注意:我不能使用可见性。 It has to be display:none 必须显示:无

It's because of the way styles are figured out. 这是因为找出样式的方式。 Style changes are expensive so they are effectively saved up until they are needed (a recalc check like .offsetHeight is called or the next frame needs to be drawn). 样式更改非常昂贵,因此可以有效地保存它们,直到需要它们为止(调用.offsetHeight类的.offsetHeight检查或需要绘制下一帧)。

The following code should work. 下面的代码应该工作。 It includes an explanation of what (I think) is going on: 它包括对(我认为)正在发生的情况的解释:

container.style.display = "block";
// container is actually still invisible
// current style hasn't been calculated

container.offsetHeight;
// this forces a style recalc
// so the current style for the container is figured out.
// without this recalc, this element effectively has no style,
// and so when you transition from its current style (null) to a different one (opacity: 1), it just snaps to the new value.

container.style.opacity = 1;
// this triggers the transition.
// because the style was recalced before the transition was triggered,
// it will transition _from_ those values.

jsFiddle jsFiddle

May I suggest you use animation instead, it is much more appropriate than force a redraw. 我可以建议您改用animation ,它比强制重animation更为合适。

 var container = document.querySelector("#container"); container.classList.add('animateme'); 
 .container { display: none; height: 150px; width: 150px; background-color: red; } .animateme { display: block; animation: animate 2s linear; } @keyframes animate { 0% { opacity: 0; } 100% { opacity: 1; } } 
 <div id="container" class="container"></div> 

Try this: 尝试这个:

var container = document.querySelector("#container");
container.style.opacity = 1;

<div id="container" class="container"></div>

.container {
height: 200px;
width: 200px;
background-color: salmon;
display: block;
border-radius: 5px;
opacity: 0;
transition: opacity 2s ease-in-out;
}

JSFiddle JSFiddle

Despite the marked 'right' answer I vote for the answer from LGSon above due to it being not a workaround but using natural CSS capability. 尽管答案很明显,但我还是投票赞成上述LGSon的答案因为它不是解决方法,而是使用自然的CSS功能。

Plus it gives a cleaner code in JS due to the ease of toggling a class ( container.classList.toggle('animateme') ) and separation of concerns (JS does not manipulate CSS properties directly). 另外,由于易于切换类( container.classList.toggle('animateme') )和关注点分离(JS不直接操作CSS属性),因此它在JS中提供了更container.classList.toggle('animateme')

However I experienced the animation animate getting reset at the end to its zero keyframe ie to opacity: 0 ; 但是我经历了动画animate在最后重置为零关键帧即opacity: 0 ;

To make it stay I added animation-fill-mode: forwards; 为了保持原状,我添加了animation-fill-mode: forwards; to .animateme selector like this (taken from this stackoverflow answer ) 到这样的.animateme选择器(来自此stackoverflow答案

.animateme {
  display: block;
  animation: animate 2s linear;
  animation-fill-mode: forwards;
}

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

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