• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
初心Syn
博客园    首页    新随笔    联系   管理    订阅  订阅
Vuejs实现轮播图

css:

<style type="text/css">
* {
margin: 0;
padding: 0;
list-style: none;
}
.clearfix {
content: '';
clear: both;
display: block;
}
.carousel {
width: 500px;
height: 300px;
margin: 0 auto;
overflow: hidden;
position: relative;
}
.slide {
width: 500px;
height: 300px;
}
li {
position: absolute
}
img {
width: 500px;
height: 300px;
}
.bullet {
width: 100%;
position: absolute;
bottom: 10px;
margin: 0 auto;
text-align: center;
z-index: 10;
}
span {
width: 20px;
height: 5px;
border: 1px solid;
background: white;
display: inline-block;
margin-right: 10px;
}
.active {
background: red;
}
.image-enter-active {
transform: translateX(0);
transition: all 1s ease;
}
.image-leave-active {
transform: translateX(-100%);
transition: all 1s ease;
}
.image-enter {
transform: translateX(100%)
}
.image-leave {
transform: translateX(0)
}
</style>

html:

<div class="carousel">
<transition-group tag='ul' class="clearfix slide" name='image'>
<li v-for='(image,index) in img' :key='index' v-show='index===mark'>
<a><img :src="image"></a>
</li>
</transition-group>

<div class="bullet">
<span v-for='(item,index) in img.length' :class="{'active':index===mark}" @click='change(index)'></span>
</div>

</div>

js:

new Vue({
el: '.carousel',
data: {
mark: 0,
timer: null,
img: ['//upload-images.jianshu.io/upload_images/3360875-5625658440cb542d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240',
'//upload-images.jianshu.io/upload_images/3360875-b70e9d81d26e2a27.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240',
'//upload-images.jianshu.io/upload_images/3360875-dc724649454c2ddc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240',
'//upload-images.jianshu.io/upload_images/3360875-d2148a1bb7ea9d21.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240'
]
},
created() {
this.play()
},
methods: {
change(i) {
this.mark = i
},
autoPlay() {
this.mark++
if (this.mark === 4) {
this.mark = 0
return
}
},
play() {
setInterval(this.autoPlay, 3000)
}
}
})

不忘初心,方得始终,初心易得,始终难守。
posted on 2017-12-04 17:04  初心Syn  阅读(1312)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3