App.vue
<template>
<swiper>
<swiperItem>
<div class="content">页面1div>
swiperItem>
<swiperItem>
<div class="content">页面2div>
swiperItem>
<swiperItem>
<div class="content">页面3div>
swiperItem>
swiper>
template>
<script>
import swiperItem from './components/SwiperItem'
import swiper from './components/Swiper'
export default {
components: {
swiper,
swiperItem
}
};
script>
<style lang="stylus">style>
swiper.vue
<template>
<div class="swiper">
<div class="viewport">
<slot>slot>
div>
div>
template>
<script>
export default {
}
script>
swiperItem.vue
<template>
<div class="swiper-item">
<slot>slot>
div>
template>
<script>
export default {
}
script>
<template>
<swiper v-model="selected">
<div class="content">页面1div>
swiperItem>
<div class="content">页面2div>
swiperItem>
<div class="content">页面3div>
swiperItem>
swiper>
template>
<script>
import swiperItem from './components/SwiperItem'
import swiper from './components/Swiper'
export default {
data:()=>{
return {selected:'box3'}
},
components: {
swiper,
swiperItem
}
};
script>
<style lang="stylus">style>
<template>
<div class="swiper">
<div class="viewport" >
<slot>slot>
div>
div>
template>
<script>
export default {
props:{
value:{
type:String,
default:''
}
},
mounted(){
this.$children.forEach(vm => {
vm.selected = this.value
});
}
}
script>
<template>
<div class="swiper-item" v-if="name===selected?true:false" >
<slot>slot>
div>
template>
<script>
export default {
props:{
name:{
type:String,
required:true
}
},
data(){
return {selected:''}
},
mounted(){
}
}
script>
<template>
<swiper v-model="selected" >
<div class="content" style="background:red">页面1div>
swiperItem>
<div class="content" style="background:green">页面2div>
swiperItem>
<div class="content" style="background:yellow">页面3div>
swiperItem>
swiper>
template>
<script>
import swiperItem from './components/SwiperItem'
import swiper from './components/Swiper'
export default {
data:()=>{
return {selected:'box3'}
},
components: {
swiper,
swiperItem
},
mounted(){
setTimeout(() => {
this.selected='box1'
}, 1000);
}
};
script>
<style lang="stylus">
.content
width 300px
height 300px
text-align center
margin auto
style>
<template>
<div class="swiper">
<div class="viewport" >
<slot>slot>
div>
div>
template>
<script>
export default {
props:{
value:{
type:String,
default:''
}
},
methods:{
showChild(){
let currentName = this.value||this.$children[0].name
this.$children.forEach(vm => {
vm.selected = currentName
});
}
},
watch:{
value(){
this.showChild()
}
},
mounted(){
this.showChild()
}
}
script>
<style lang="stylus">
.swiper
border 5px solid black
width 300px
height 300px
margin auto
text-align center
style>
<template>
<transition>
<div class="swiper-item" v-if="isShow">
<slot>slot>
div>
transition>
template>
<script>
export default {
props: {
name: {
type: String,
required: true
}
},
data() {
return { selected: "" };
},
mounted() {},
computed: {
isShow() {
return this.name === this.selected;
}
}
};
script>
<style lang="stylus">
.v-enter-active,.v-leave-active
transition all 1s linear
position absolute
.v-leave-to
transform translate(-100%)
.v-enter
transform translate(100%)
style>
<template>
<div class="swiper">
<div class="viewport">
<slot>slot>
div>
div>
template>
<script>
export default {
props: {
value: {
type: String,
default: ""
},
autoplay:{
type:Boolean,
default:true
}
},
methods: {
showChild() {
this.currentName = this.value || this.$children[0].name;
this.$children.forEach(vm => {
vm.selected = this.currentName;
});
},
autoShow(){
if(this.autoplay){
let timer = setInterval(() => {
let index = this.names.indexOf(this.currentName)
let newIndex = ++index
if(newIndex===this.names.length){
newIndex=0
}
if(newIndex===-1)newIndex=this.names.length-1
this.$emit('input',this.names[newIndex])
},2000);
}
}
},
watch: {
value() {
this.showChild();
}
},
beforeDestroy(){
clearInterval(this.timer)
},
data(){
return {currentName:''}
},
mounted() {
this.names = this.$children.map((vm)=>vm.name)
this.showChild();
this.autoShow()
}
};
script>
<style lang="stylus">
.swiper
border 5px solid black
width 300px
height 300px
margin auto
text-align center
overflow hidden
.viewport
position relative
style>
<template>
<swiper v-model="selected" autoplay>
<swiperItem name="box1">
<div class="content" style="background:red">页面1div>
swiperItem>
<swiperItem name="box2">
<div class="content" style="background:green">页面2div>
swiperItem>
<swiperItem name="box3">
<div class="content" style="background:yellow">页面3div>
swiperItem>
swiper>
template>
<script>
import swiperItem from "./components/SwiperItem";
import swiper from "./components/Swiper";
export default {
data: () => {
return { selected: "box3" };
},
components: {
swiper,
swiperItem
},
mounted() {
}
};
script>
<style lang="stylus">
.content {
width: 300px
height: 300px
text-align: center
margin: auto
}
style>