Vue实现一个简单的点击箭头上下移动效果

Vue实现一个简单的点击箭头上下移动效果_第1张图片
Vue实现一个简单的点击箭头上下移动效果_第2张图片

<body>
    <div id="app">
        <ul>
            <li v-for="(item,i) in list">{
     {
     item.name}}
                 //i
                <button v-show="i @click="down(i)"></button>
                <button v-show="i>0" @click="up(i)"></button>
            </li>
        </ul>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    
        Vue.config.productionTip = false//不去提示
        new Vue({
     
            el: "#app",
            data() {
     
                return {
     
                    list:[
                        {
     id:111,name:"aaa"},
                        {
     id:222,name:"bbb"},
                        {
     id:333,name:"ccc"},
                        {
     id:444,name:"ddd"}
                    ]
                }
            },
        
            methods:{
     
                down(i){
     
                    let temp = this.list[i]
                    this.$set(this.list,i,this.list[i+1])
                    this.$set(this.list,i+1,temp)
                },
                up(i){
     
                    let temp = this.list[i]
                    this.$set(this.list,i,this.list[i-1])
                    this.$set(this.list,i-1,temp)
                }
            }
        })
    </script>
</body>

$set(检测数组的变动)

你可能感兴趣的:(vue,javascript)