list.html 1.9 KB

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