3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vue.jsのリストレンダリングにslideUp/slideDownアニメーション効果

Posted at

内容

Vue.jsのリストレンダリングのトランジションにjQueryのslideUp/slideDownのようなアニメーション効果をつける。

コード

HTML
<div id="app">
  <input v-on:keypress.enter="addItem" v-model="input" type="text">
  <button v-on:click="addItem">追加</button>
  <transition-group tag="ul" v-on:enter="enter" v-on:after-enter="afterEnter" v-on:leave="leave" v-on:after-leave="afterLeave">
    <li v-for="(item, index) in list" v-bind:key="item.id">{{ item.text }}<button v-on:click="removeItem(index)">削除</button></li>
  </transition-group>
</div>
CSS
ul {
  padding-left: 0;
  list-style-type: none;
}
li button {
  margin-left: 1em;
}
.v-enter-active,
.v-leave-active {
  overflow-y: hidden;
  transition: height 0.5s ease;
}
JavaScript
var counter = 0; // v-forのkey属性用
var app = new Vue({
  el: '#app',
  data: {
    input: '',
    list: []
  },
  methods: {
    addItem: function() {
      if (this.input.length !== 0) {
        this.list.push({
          id: counter,
          text: this.input
        });
        counter += 1;
        this.input = '';
      }
    },
    removeItem: function(index) {
      this.list.splice(index, 1);
    },
    enter: function(element) {
      var elementHeight = element.getBoundingClientRect().height;
      element.style.height = 0;
      element.offsetHeight; // reflow
      element.style.height = elementHeight + 'px';
    },
    afterEnter: function(element) {
      element.style.height = null;
    },
    leave: function(element) {
      var elementHeight = element.getBoundingClientRect().height;
      element.style.height = elementHeight + 'px';
      element.offsetHeight; // reflow
      element.style.height = 0;
    },
    afterLeave: function(element) {
      element.style.height = null;
    }
  }
});
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?