Nima Ajdari
Nima Ajdari

Reputation: 2914

fade in and fade out transition for two different elements at the same time

I am new to Vue js and the transitions and I am designing a Vue component, in which I intend to wrap data being loaded lazily from server so that when data is unavailable, a loading gif is displayed.

It works just fine. However, when I add a simple fade transition to make loading gif fade out and content fade in when data is available ( or vice versa) at the same time, the content and gif push each other up or down when one is appearing and the other disappearing.

This is my Lazy.vue component file:

<template>
  <div class="fixed-pos">
    <transition name="fade">
        <slot v-if="loaded"></slot>
        <div v-else>
            <img src="../assets/loading.gif"/>
        </div>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'Lazy',
  props: {
    loaded: {
      type: Boolean
    }
  }
}
</script>

And a sample usage of it:

<template>
  <div>
    <button @click="loaded = !loaded">Toggle</button>
        <lazy :loaded="loaded">
            <ul v-if="rendered">
                <transition-group name="fade">
                    <li v-for="notif in notifs" v-bind:key="notif">
                        <span>{{notif}}</span>
                    </li>
                </transition-group>
            </ul>
        </lazy>
  </div>
</template>

<script>
import Lazy from './Lazy'

export default {
  name: 'HelloWorld',
  components: {
    Lazy
  },
  data () {
    return {
      msg: 'Welcome to Your Vue.js App',
      rendered: true,
      notifs: [
        'Notif 1',
        'Notification 2 is here!',
        'Here comes another notification',
        'And another here ...'
      ],
      loaded: true
    }
  }
}
</script>

My animation.css file:

.fade-enter-active, .fade-leave-active {
  transition: opacity .5s
}
.fade-enter, .fade-leave-to {
  opacity: 0
}

Is there any way solving this issue using vue transitions or any other ways?

Upvotes: 5

Views: 3865

Answers (1)

Sui Dream
Sui Dream

Reputation: 550

You need position: absolute; CSS rule for your gif and content: JSFiddle example.

Doc says:

...no space is created for the element in the page layout. Instead, it is positioned relative to its closest positioned ancestor if any; otherwise, it is placed relative to the initial containing block.

You also will probably need position: relative; for parent element of gif and content.

Upvotes: 4

Related Questions