list.html 1.8 KB

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