Why is ele.style.top animated only when it is placed in setTimeout?

style part

.aa{position:absolute;transition:top 2s;}

html part

<div id="ele" class="aa"></div>

js part

var ele=document.getElementById("ele")
ele.onclick=function(){
    ele.style.top=0
//ele.style.top=100+"px"setTimeouttop0100
setTimeout(function(){
    ele.style.top=100+"px"
},0)
}

online preview address
http://jsbin.com/pucedelowa/e.

now there is a doubt, why only put ele.style.top=100+"px" in seTimeout, click on the text will have the animation effect of transition?
are there any other functions that can achieve the same effect as setTimeout?


ele.style.top=0
ele.style.top=100+'px'

because the Synchronize method will not be rendered until the execution of the method is completed, and the final result is 100px, so the asynchronous method can achieve this effect.

Menu