基于Vue实现左右滑动轮播图
<!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, init
·
<!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>Document</title>
<script src="./bower_components/vue/dist/vue.js"></script>
<style>
.img-wrap {
width: 500px;
height: 300px;
overflow: hidden;
position: relative;
}
img {
width: 500px;
height: 300px;
position: absolute;
top: 0;
left: 0;
}
.left-btn {
position: absolute;
left: 20px;
top: 140px;
z-index: 99;
}
.right-btn {
position: absolute;
right: 20px;
top: 140px;
z-index: 99;
}
.slide-left-enter-active,
.slide-left-leave-active {
transition: all 2s ease;
}
.slide-left-enter {
transform: translateX(500px);
}
.slide-left-leave-to {
transform: translateX(-500px);
}
.slide-right-enter-active,
.slide-right-leave-active {
transition: all 2s ease;
}
.slide-right-enter {
transform: translateX(-500px);
}
.slide-right-leave-to {
transform: translateX(500px);
}
.title-wrap {
width: 500px;
height: 50px;
position: absolute;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
color: #fff;
line-height: 50px;
}
.dot-wrap {
float: right;
}
i {
display: inline-block;
width: 10px;
height: 10px;
background-color: #fff;
border-radius: 50%;
margin: 0 5px;
}
.active {
background-color: aqua;
}
</style>
</head>
<body>
<div id="app">
<div class="img-wrap" @mouseover="mouseOver" @mouseleave="mouseLeave">
<button class="left-btn" @click="leftSlide"> left </button>
<button class="right-btn" @click="rightSlide"> right </button>
<template v-for="(item,index) in img">
<transition :name="transitionName">
<img :src="item.pic" alt="" v-show="index==current">
</transition>
</template>
<div class="title-wrap">
<template v-for="(item,index) in img">
<span v-show="index==current">{{item.title}}</span>
</template>
<div class="dot-wrap">
<template v-for="(item,index) in img">
<i :class="{'active':index==current}" @click="curr(index)"></i>
</template>
</div>
</div>
</div>
</div>
<script>
new Vue({
el: '#app',
data: {
img: [
{ pic: 'img/1.jpg', title: 'img1' },
{ pic: 'img/2.jpg', title: 'img2' },
{ pic: 'img/3.jpg', title: 'img3' },
{ pic: 'img/4.jpg', title: 'img4' },
{ pic: 'img/5.jpg', title: 'img5' }
],
transitionName: 'slide-left',
current: 0, //当前展示的下标
autoTimer: null //自动轮播定时器
},
computed: {
maxIndex() {
return this.img.length - 1;
}
},
methods: {
leftSlide() {
this.transitionName = 'slide-left'
if (this.current == 0) {
this.current = 5;
} else {
this.current--
}
},
rightSlide() {
this.transitionName = 'slide-right'
if (this.current == this.maxIndex) {
this.current = 0;
} else {
this.current++
}
},
curr(index) {
this.current = index;
},
mouseOver() {
clearInterval(this.autoTimer);
},
mouseLeave() {
this.timer();
},
timer() {
let that = this;
this.autoTimer = setInterval(function () {
that.transitionName = 'slide-right'
if (that.current == that.maxIndex) {
that.current = 0;
} else {
that.current++
}
}, 2000)
}
},
mounted() {
this.timer()
},
})
</script>
</body>
</html>
更多推荐
已为社区贡献1条内容
所有评论(0)