简体   繁体   English

样式渲染后的回调

[英]Callback after the style has been rendered

All non-transition styles should be applied and rendered before applying transition styles. 在应用过渡样式之前,应先应用所有非过渡样式并进行渲染 That's why such callback should exist. 这就是为什么这样的回调应该存在的原因。

demo 演示

<button class="toggle">toggle</button>
<div class="overlay"></div>

.overlay {
  width      : 400px;
  height     : 400px;
  background : gray;

  display    : none;
  opacity    : 0;
  transition : opacity 0.3s ease-in-out;
}
.overlay.visible {
  opacity : 1;
}

var overlay = $(".overlay");
$(".toggle").click(function() {
  if (overlay.hasClass("visible")) {
    overlay.removeClass("visible").one("transitionend", function () {
      overlay.css("display", "none");
    });
  } else {
    overlay.css("display", "block");
    setTimeout(function () {
      overlay.addClass("visible");
    }, 0);
  }
});

You can see that gray block fades smoothly in chrome, but it jumps in firefox. 您可以看到灰色块在chrome中平滑消失,但在Firefox中跳变。

setTimeout(function () {
}, 0);

Zero timeout is not enough for firefox. 零超时对于Firefox是不够的。 I've checked that at my machine 5 miliseconds works fine 50/50. 我检查过,在我的机器上5毫秒可以正常工作50/50。

Should I contact the fathers of this pain or there are any solutions? 我应该联系遭受这种痛苦的父亲吗,或者有什么解决办法?

Yes, on firefox and IE you need to use getComputedStyle additionaly 是的,在Firefox和IE上,您需要另外使用getComputedStyle

var el = overlay[0];
el.style.display = 'block';

// force reflow
getComputedStyle(el).width;

setTimeout(function() {
    overlay.addClass("visible");
}, 0);

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

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