具体代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/vue.js"></script>
</head>
<style>
.img1{
width: 400px;
height: 400px;
display: inline-block;
border: 1px solid red;
}
.parent{
text-align: center;
}
</style>
<body>
<div id="box" class="parent">
<button @click="change(0)">点我改变图片</button>
<div class="img1">
<img :src="pic" alt="">
</div>
<button @click="change(1)">点我改变图片1</button>
</div>
</body>
<script>
new Vue({
el:'#box',
data:{
pic:"img/运动鞋 _1.jpg",
index:0,
img:["img/运动鞋 _1.jpg","img/运动鞋 _2.jpg","img/运动鞋 _3.jpg"],
},
methods: {
change(index){
if (index==0) {
if(this.index>0){
this.index-=1;
}else{
alert("亲没有下一张图片了...")
}
this.pic = this.img[this.index]
}else if(index==1){
if(this.index<2){
this.index+=1;
}else{
alert("亲没有下一张图片了...")
}
this.pic = this.img[this.index]
}
}
},
})
</script>
</html>