当代码是顺序的时,CSS3 转换不起作用

标签 css css-transitions

您将如何按顺序激活 CSS3 转换? 我正在尝试以下操作:

<script type="text/javascript">
    var sample = $('<div style="text-align:center;">hello</div>');
    sample.appendTo($('body'));

    sample.css('transform', 'scale(1.0)');
    sample.css('transition', 'all 1s ease-in-out');
    sample.css('transform', 'scale(2.0)');
</script>

它完全忽略过渡,只设置比例。

最佳答案

来自 Mozilla 的文档:

https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions

Care should also be taken when using a transition immediately after adding the element to the DOM using .appendChild() or removing its display: none; property. This is seen as if the initial state had never occured and the element was always in its final state. The easy way to overcome this limitation is to apply a window.setTimeout() of a handful of milliseconds before changing the CSS property you intend to transition to.

所以解决方案最终是:

<script type="text/javascript">
    var sample = $('<div style="text-align:center;">hello</div>');
    sample.appendTo($('body'));

    sample.css('transform', 'scale(1.0)');
    sample.css('-moz-transition', 'all 1s ease-in-out');

    setTimeout(function () {
        sample.css('transform', 'scale(2.0)');
    }, 10);
</script>

关于当代码是顺序的时,CSS3 转换不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22882755/

相关文章:

html - 表格内的幻影边框 Css

javascript - 网站上已加载页面之间的平滑过渡

jquery - CSS3/Javascript : Transitions IE support using jQuery/Modernizr

css - 位置为 : fixed? 的移动对象的转换

css - 使用 z-index 样式属性的 SVG2 堆叠系统似乎无法正确解释

python - 我将如何为每个小部件元素设置单独的颜色?

html - div 包含另一个 div 和图像获得意外高度

css3 过渡 : different time value when :hover In than Out?

html - CSS 元素过渡/动画 : Opacity Issue

html - 如何使切片图像的中间部分重复长菜单?