完成这样的一个功能:
在列表随机的位置添加数字,每次添加的数字增加1;也可以单击按钮,随机移除列表中的数字
样例图:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>列表过渡</title>
<script src="../js/vue.js"></script>
<style>
/* 数字圆圈部分样式 */
.list-item {
display: inline-block;
margin-right: 10px;
background-color: red;
border-radius: 50%;
width: 25px;
height: 25px;
text-align: center;
line-height: 25px;
color: #fff;
}
</style>
</head>
<body>
<div id="app">
<button @click='addNum'>随机插入一个数字</button>
<button @click="removeNum">随机移除一个数字</button>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
items: [1, 2, 3, 4],
nextNum: 5,
removedNum: []
}
})
</script>
</body>
</html>
题主要的代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>列表过渡</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<style>
/* 数字圆圈部分样式 */
.list-item {
display: inline-block;
margin-right: 10px;
background-color: red;
border-radius: 50%;
width: 25px;
height: 25px;
text-align: center;
line-height: 25px;
color: #fff;
}
</style>
</head>
<body>
<div id="app">
<button @click='addNum'>随机插入一个数字</button>
<button @click="removeNum">随机移除一个数字</button>
<br /><br />
<div v-for="item in items" class="list-item">
{{item}}
</div>
<div><br />将插入的数字是:{{nextNum}}</div>
<div><br />被移除的是:{{removedNum}}</div>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
items: [1, 2, 3, 4],
nextNum: null,
removedNum: null
},
methods: {
addNum() {
this.nextNum = Math.floor(Math.random() * 10);
var index = Math.floor(Math.random() * this.items.length);
this.items.splice(index, 0, this.nextNum)
},
removeNum() {
var index = Math.floor(Math.random() * this.items.length);
this.removedNum = this.items[index];
this.items.splice(index,1)
}
}
})
</script>
</body>
</html>
定义一个数组,随机数组里面的数字,然后循环div,或者img标签显示对应的图片。
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!