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