1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| <template> <div class="demo"> <ul id="items"> <li v-for="item in items" :key="item">{{ item }}</li> </ul> {{ items.join(', ') }} </div> </template>
<script>
import Sortable from 'sortablejs';
export default { data() { return { items: [0, 1, 2, 3, 4, 5], }; }, mounted() { const e = document.querySelector('#items');
Sortable.create(e, { animation: 150, onEnd: (event) => { const item = this.items[event.oldIndex]; this.items.splice(event.oldIndex, 1); this.items.splice(event.newIndex, 0, item); } });
const COLORS = ['red', 'green', 'blue', 'yellow', 'pink', 'violet']; document.querySelectorAll('li').forEach((li, i) => { li.className = COLORS[i++]; }); }, }; </script>
<style lang="scss"> .demo { ul#items { list-style: none;
li { margin-bottom: 10px; width: 100px; text-align: center; font-weight: bold; } }
.red { background: red; } .green { background: green; } .blue { background: blue; } .yellow { background: yellow; } .pink { background: pink; } .violet { background: violet; } } </style>
|