我找不到在新創建的dom元素上使用css轉換的方法。css轉換新元素
比方說,我有一個空的HTML文件。
<body>
<p><a href="#" onclick="return f();">click</a></p>
</body>
我也有這個CSS
#id {
-moz-transition-property: opacity;
-moz-transition-duration: 5s;
opacity: 0;
}
#id.class {
opacity: 1;
}
和這個js
function f() {
var a = document.createElement('a');
a.id = 'id';
a.text = ' fading in?';
document.getElementsByTagName('p')[0].appendChild(a);
// at this point I expect the span element to be with opacity=0
a.className = 'class';
// now I expect the css transition to go and "fade in" the a
return false;
}
但是,正如你可以看到http://jsfiddle.net/gwxkW/1/當您單擊元素瞬間出現。
如果我嘗試設置類timeout()
我經常找到結果,但對我來說,它似乎更多的JavaScript和CSS引擎之間的種族。是否有一些特定的事件要聽?我試圖使用document.body.addEventListener('DOMNodeInserted', ...)
,但它不工作。
如何在新創建的元素上應用css轉換?
使用'setTimeout'設置類名稱的作用,但只有當我的延遲是6ms或更多。不確定一個好方法。 – pimvdb
是的,我嘗試了一些低毫秒值(範圍0-10),大部分時間它的工作時間爲5-6毫秒,但我感覺這是錯誤的方法。我希望的是某種事件涉及到「從現在開始應用CSS」,但我不知道它們是否存在 –
請參見http:// stackoverflow。com/questions/18564942/clean-way-to-programmatically-use-css-transitions-from-js討論一個乾淨的方式來做到這一點。 – Nickolay