vue2 实现简单的列表项上下移动

<template>
  <div class="list">
    <h1>{{ msg }}</h1>
    <router-link to="/">Home</router-link>
    <router-link to="/">List</router-link>
    <ul>
      <li v-for="(item,index) in newsList" track-by="index">
        <span class="title">{{item.title}}</span>
        <div class="operate">
          <span @click="del(index)"> 删除  </span>
          <span @click="sortUp(index)">  向上↑  </span>
          <span @click="sortDown(index)">  向下↓ </span>
        </div>
      </li>
    </ul>
  </div>
</template>
<script>
export default {
  name: 'list',
  data () {
    return {
      msg: 'list',
      newsList: [
        {
          id: 1,
          title: '11111',
          descript: 'descript1'
        },
        {
          id: 2,
          title: '22222',
          descript: 'descript2'
        },
        {
          id: 3,
          title: '33333',
          descript: 'descript3'
        },
        {
          id: 4,
          title: '44444',
          descript: 'descript4'
        }
      ]
    }
  },
  methods: {
    del (index) {
      this.newsList.splice(index, 1)
    },
    sortUp (index) {
      if (index === 0) {
        alert('到顶了!')
      } else {
        this.newsList.push(this.newsList.shift())
      }
    },
    sortDown (index) {
      if (index === (this.newsList.length - 1)) {
        alert('到底了!')
      } else {
        this.newsList.unshift(this.newsList.pop())
      }
    }
  }
}
</script>
<style scoped>
h1,
h2 {
  font-weight: normal;
}

ul {
  list-style-type: none;
  padding: 0;
  text-align: left;
}

li {
  width: 500px;
  border: 1px solid #ccc;
  margin: 10px;
  line-height: 30px;
  height: 30px;
  padding: 5px;
}

a {
  color: #42b983;
}

.operate {
  float: right;
}

.operate span:first-of-type {
  color: #f00;
}

.operate span {
  cursor: pointer;
  color: #42b983;
}
</style>

效果如下:

1.jpg

使用了相关的数组方法:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前端知识体系http://www.cnblogs.com/sb19871023/p/3894452.html 前端...
    秋风喵阅读 12,585评论 7 163
  • 初入皇城,满眼尽是新鲜的物什,凌亲王府郡主入主东宫,国之上下,普天同庆,除却轿中的她. 大婚之夜,他一身皇袍随着盖...
    顾沛若阅读 199评论 0 1
  • 认识芳芳后、看到每天无论工作多么繁忙,她都自己做饭吃,也萌生了自己做饭的想法!于是从2017年的七月一日开始付...
    初心的小小阅读 168评论 0 1
  • 2017年春节前夕,央行又发布了《关于开展违规”聚合支付“服务清理整治工作的通知》,对第三方支付的整顿延伸到聚合支...
    i聚合阅读 989评论 1 2
  • 作者:西子 写诗的心,不是化了妆、戴了面具的心,而是一颗素雅真颜的心。我写诗,只为安顿心灵。 在这个...
    紫雨轩尼诗阅读 240评论 0 2