开发者

webkit - css transition effect on a div

I am trying to do fadein/out effect when I change innerHTML of a div. Even tried example given at safari 开发者_运维技巧doc

I want the div to fade-out, change the contents and then fade-in again with new content.

<style>
.cv { background-color: #eee; -webkit-transition: all 1s ease-in-out;}
.cf {opacity: 0;}
</style>
<body> 
 <div id="main"> 
  <div id="c"> OLD </div>
 </div> 
</body>
<script> 
var c = document.getElementById("c");
c.setAttribute("class", "cf");
c.innerHTML = '';
c.appendChild(fragment);
c.setAttribute("class", "cv");
</script>

Please help me here


<style>
.cv { opacity:1; background-color: #eee; -webkit-transition: all 1s ease-in-out;}
.cv.hide {opacity: 0;}
</style>

[...]

<script>
c.setAttribute("class", "cv hide");
[...]
c.setAttribute("class", "cv");
</script>


I re-wrote your code -- see http://jsfiddle.net/Kai/ec64b/

I've also included some class manipulation functions for you to use.


<style>
.cv { background-color: #eee; -webkit-transition: all 1s ease-in-out;}
/* .cf {opacity: 0;} */
</style>
<body> 
 <div id="main"> 
  <div id="c"> OLD </div>
 </div> 
<script> 
var c = document.getElementById("c");
c.setAttribute("class", "cf");
// c.innerHTML = '';
// c.appendChild(fragment);
c.setAttribute("class", "cv");
</script>
</body>

Try out this code. Put script before body closing tag, not after.


This is what I finally ended doing. Thank you all for the answers

init: function(fragment) {
        var c = document.getElementById("c");
        c.addEventListener( 'webkitTransitionEnd', IDR.callHook(c, fragment), false);
        c.setAttribute("class", "cv hide"); 
     },

     callHook: function(c, fragment) {
        var fp = function() {
           c.removeEventListener('webkitTransitionEnd', fp, false);
           c.innerHTML = '';
           c.setAttribute("class", "cv");
           c.appendChild(fragment);
        };
        return fp;
     },
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜