vue 实现无限轮播_vue实现一个轮播效果

本文展示了如何使用Vue.js创建一个无限轮播组件。通过定义数据属性`cur`跟踪当前显示的图片索引,`banners`存储轮播图片数组,以及定时器`timer`来实现自动切换。在组件的`created`钩子中启动自动播放,在`beforeDestroy`钩子中停止播放。`prev`和`next`方法用于切换图片,同时确保在边界条件(首尾切换)下保持无缝效果。样式定义了轮播图容器及控制按钮的外观和位置。
摘要由CSDN通过智能技术生成

template lang="html">

data(){return{

cur:0,

banners: [{src: img1},{src: img2},{src: img3}],

timer:null}

},

created(){this.startAutoPlay();

},

beforeDestroy(){this.endAutoPlay();

},

methods:{

prev(){if(this.cur==0){this.cur=this.banners.length-1;

}else{this.cur--;

}

},

next(){if(this.cur==this.banners.length-1){this.cur=0;

}else{this.cur++;

}

},

startAutoPlay(){this.timer=setInterval(this.next, 2000);

},

endAutoPlay(){

clearInterval(this.timer);

}

}

}

/*banner*/.banner {height:420px;position:relative;}

.banner-container {width:1920px;height:420px;position:absolute;left:0;top:0;z-index:1;opacity:0;transition:.7s all ease}

.banner-container img {width:1920px;height:420px;}

.banner-container.cur {opacity:1;}

.banner .btn {

position:absolute;

z-index:2;

left:50%;top:200px;

text-indent:-99999px;

width:50px;height:50px;border-radius:50%;

background-color:#D0C4AF;

background-image:url(../assets/images/icon2.png);

background-repeat:no-repeat;

}

.banner .btn:hover {background-color:#B19E7A;}

.banner .btn.prev {background-position:0 -5384px;margin-left:-605px;}

.banner .btn.next {background-position:0 -5604px;margin-left:555px;}

.banner .number-list {position:absolute;left:0;width:100%;bottom:4px;text-align:center;z-index:2;}

.banner .number-list li {

display:inline-block;

width:8px;height:8px;

margin:08px 10px;

cursor:pointer;

background:#FFF;

border-radius:50%;

border:1px solid #cecece;

transition:.7s all ease

}

.banner .number-list .active {

background:#a7936e;

box-shadow:0 0 04px #dfcead;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值