Animate using custom directive not working

461 views Asked by At

created a custom directive to extend v-show , v-showblock is adding on true style: display: block;

The transitions/animations are working with v-show but not with v-showblock, which is working without animation.

I simplified my code to focus on the issue:

<style>
 .fade-enter-active, .fade-leave-active {
  transition: opacity .5s
}
.fade-enter, .fade-leave-active {
  opacity: 0
}
</style>
<body>

<div id="demo">
  <button @click="toggle">Toggle</button>
    <transition name="fade">
    <p v-showblock="show">hello</p>
    </transition>
</div>



 Vue.directive('showblock',
         function (el, binding) {
            if (binding.value === true) {
                el.style.display = 'block';
            }
            else {
                el.style.display = 'none';
            }
        }
    );
    new Vue({
      el: '#demo',
      data: {
        show: true
      },
      methods: {
         toggle: function() {
             this.show =!this.show;
         }
      }
    });
1

There are 1 answers

0
Rommel Santor On BEST ANSWER

This isn't working because VueJS transitions work on elements within <transition> only in certain situations:

  1. v-if state changes
  2. v-show state changes
  3. components

In your case you're attempting to hide/show using a custom directive v-showblock, which is unsupported. If you instead used v-if or v-show there, then the transition will work.

Read more in the VueJS docs.