index.html 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Move Animations</title>
  6. <style>
  7. .container {
  8. position: relative;
  9. padding: 0;
  10. }
  11. .item {
  12. width: 100%;
  13. height: 30px;
  14. background-color: #f3f3f3;
  15. border: 1px solid #666;
  16. box-sizing: border-box;
  17. }
  18. /* 1. declare transition */
  19. .fade-move, .fade-enter-active, .fade-leave-active {
  20. transition: all .5s cubic-bezier(.55,0,.1,1);
  21. }
  22. /* 2. declare enter from and leave to state */
  23. .fade-enter, .fade-leave-to {
  24. opacity: 0;
  25. transform: scaleY(0.01) translate(30px, 0);
  26. }
  27. /* 3. ensure leaving items are taken out of layout flow so that moving
  28. animations can be calculated correctly. */
  29. .fade-leave-active {
  30. position: absolute;
  31. }
  32. </style>
  33. <script src="https://cdn.jsdelivr.net/lodash/4.3.0/lodash.min.js"></script>
  34. <!-- Delete ".min" for console warnings in development -->
  35. <script src="../../dist/vue.min.js"></script>
  36. </head>
  37. <body>
  38. <div id="el">
  39. <button @click="insert">insert at random index</button>
  40. <button @click="reset">reset</button>
  41. <button @click="shuffle">shuffle</button>
  42. <transition-group tag="ul" name="fade" class="container">
  43. <item v-for="item in items"
  44. class="item"
  45. :msg="item"
  46. :key="item"
  47. @rm="remove(item)">
  48. </item>
  49. </transition-group>
  50. </div>
  51. <script>
  52. var items = [1, 2, 3, 4, 5]
  53. var id = items.length + 1
  54. var vm = new Vue({
  55. el: '#el',
  56. data: {
  57. items: items
  58. },
  59. components: {
  60. item: {
  61. props: ['msg'],
  62. template: `<div>{{ msg }} <button @click="$emit('rm')">x</button></div>`
  63. }
  64. },
  65. methods: {
  66. insert () {
  67. var i = Math.round(Math.random() * this.items.length)
  68. this.items.splice(i, 0, id++)
  69. },
  70. reset () {
  71. this.items = [1, 2, 3, 4, 5]
  72. },
  73. shuffle () {
  74. this.items = _.shuffle(this.items)
  75. },
  76. remove (item) {
  77. var i = this.items.indexOf(item)
  78. if (i > -1) {
  79. this.items.splice(i, 1)
  80. }
  81. }
  82. }
  83. })
  84. </script>
  85. </body>
  86. </html>