动态 DOM 元素上的 webKit 转换
我的 CSS 文件定义了用于转换的 div
类:
div.BlackOut {
-webkit-transition-property: opacity;
-webkit-transition-duration: 2s;
}
然后,在我的 javascript 中添加一个动态 dom 元素(通过 jQuery):
var cssObj = {
'background-color' : '#000',
'width' : '100%',
'height' : '400px',
'position' : 'absolute',
'top' : 0,
'z-index' : '9998'
};
var element = $("<div>").css(cssObj).addClass('BlackOut').appendTo( 'body' );
element.get(0).style.opacity = 0;
但转换没有开始!
为什么?
I my CSS file I define my div
class for transitions:
div.BlackOut {
-webkit-transition-property: opacity;
-webkit-transition-duration: 2s;
}
Then, in my javascript add a dynamic dom element (via jQuery):
var cssObj = {
'background-color' : '#000',
'width' : '100%',
'height' : '400px',
'position' : 'absolute',
'top' : 0,
'z-index' : '9998'
};
var element = $("<div>").css(cssObj).addClass('BlackOut').appendTo( 'body' );
element.get(0).style.opacity = 0;
But the transition doesn't start!
Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,发现我的转换会发生,但转换不会运行 - 没有动画。看起来,如果您尝试在元素进入 DOM 之前执行转换,它将忽略转换 - 同时仍然执行转换。我不知道这是否是一个错误或根据规范。
无论如何,如果您的新元素是图像,您可以通过
load
事件添加 Transitions/css 类。但是,我无法找到将 sans-url 元素添加到 DOM 时触发的事件。我选择了暂停,这对你也应该有效。
你的代码看起来像这样:
I was running into the same problem, and found that my transformations would occur, but the transition would not run - no animation. It appears that if you try to execute the transitions before the element is in the DOM, it will ignore the transitions - while still executing the transformation. I don't know enough to say whether that's a bug or according to spec.
Anyway, if your new element is an image, you could add your transitions/css class via the
load
event.However, I was unable to find an event that fires when sans-url elements are added to the DOM. I chose to fall back on a timeout, which should work for you as well.
Your code would look something like this:
我认为你需要一个缓动或者可能是一个
不透明度
。所以:这应该可行,但也许您还希望让您的 css 面向未来,还包括其他浏览器(例如 FF 4):
I think you need an easing or perhaps an
opacity
. So:That should work, but maybe you also would want to future proof your css, to also include other browsers (FF 4 for example):