When I tried to add class to an element in the mounted lifecycle, the transition effect is not working. The element immediately appeared as the final state:
However, if I use setTimeout to delay the class change, the transition will be working.
new Vue({
el: '#example-1',
mounted: function () {
// setTimeout(function () {
let test = document.getElementsByClassName('test')[0]
test.classList.add('loaded')
// }, 0)
}
})
.test {
color:red;
font-size:60px;
transform:none;
transition: all 1s linear;
}
.test.loaded {
transform: translateX(400px)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>
<div id="example-1">
<h1 class='test'>
fa
</h1>
</div>
I understand why this is happening if it's in vanilla js: Transition needs to happen from one rendered state to another. The delay is there to allow the initial element to be rendered first.(Refer to this SO post)
However in Vue, I thought that Mounted already means that the initial view has been rendered. So there is already an initial rendered state.
It seems that's not true from this example.
So is mounted !== rendered?