Vue 轮播图实现 点击数字按钮时会过渡显示对应的图片

roujennie 2020-11-23 10:00:34
为什么我按数字按钮只能实现一次图片跳转?怎么把图片和按钮对应起来?
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>轮播图切换效果</title>


<style type="text/css">
.banner {
position: relative;
}

.bannerImg {
position: relative;
height: 360px;
}

.bannerImg span {
/* 设置图片定位 */
position: absolute;
top: 0;
left: 0;
}

.bannerBtn {
width: 200px;
position: absolute;
left: 30%;
bottom: -20px;
text-align: center;
}

.bannerBtn li {
list-style: none;
border-radius: 50%;
float: left;
}

.bannerBtn li a {
/* 设置按钮样式 */
display: block;
width: 20px;
height: 20px;
border-radius: 50%;
margin: 5px;
color: #160303;
font-weight: bolder;
text-decoration: none;
}


</style>
<script src="js/vue.js"></script>
</head>

<body>
<div id="box">
<div class="banner">
<!--切换的图片-->
<div class="bannerImg">
<span v-for="(v,i) in banners" :key="i">
<img width="700" :src="banners[currentIndex].imgSrc" alt="">
</span>
</div>
<!--切换的小按钮-->
<ul class="bannerBtn">
<li v-for="num in 3">
<a href="javascript:;" class="aBtn" @click="change(num)">{{num}}</a>
</li>
</ul>
</div>
</div>
<script type="text/javascript">
//创建根实例
var vm = new Vue({
el: '#box',
data(){
return{
banners:[
{id:1,imgSrc:"img/ad1.png"},
{id:2,imgSrc:"img/ad2.png"},
{id:3,imgSrc:"img/ad3.png"},
],
currentIndex:0
}
},
methods:{
change(){
if (this.currentIndex == 3){
this.currentIndex = 0;
}
if (this.currentIndex == 0){
this.currentIndex = 1;
}
}
}



});
</script>

</body>

</html>
...全文
2289 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
涿鹿中猿 2020-11-24
  • 打赏
  • 举报
回复
建议看element文档中的走马灯 https://element.eleme.cn/#/zh-CN/component/carousel
Lotto . 2020-11-24
  • 打赏
  • 举报
回复
change(num){ this.currentIndex = num-1 }

87,994

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧