vue 上下键li_vue2 实现简单的列表项上下移动

{{ msg }}

Home

List

  • {{item.title}}

    删除

    向上↑

    向下↓

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())

}

}

}

}

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;

}

效果如下:

59547c93803d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

1.jpg

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值