vue+swiper实现侧滑菜单效果

2020-06-16 06:06:44易采站长站整理

本文实例为大家分享了vue swiper实现侧滑菜单效果的具体代码,供大家参考,具体内容如下

先上效果图:

这个左右滑动以及上下滑动主要使用了Swiper的轮播功能,首先是该自定义组件的代码:


<template>
<div class="s-slider">
<swiper :options="horizontalSwiperOptions" ref="horizontalSwiper">
<swiper-slide class="left" ref="left" v-bind:style="{'background':getRandomColor()}">
<slot name="left"></slot>
</swiper-slide>
<swiper-slide class="content">
<swiper :options="verticalSwiperOptions" ref="verticalSwiper">
<swiper-slide class="top" ref="top" v-bind:style="{'background':getRandomColor()}">
<slot name="top"></slot>
</swiper-slide>
<swiper-slide class="content" ref="content" v-bind:style="{'background':getRandomColor()}">
<slot name="content"></slot>
</swiper-slide>
<swiper-slide class="bottom" ref="bottom" v-bind:style="{'background':getRandomColor()}">
<slot name="bottom"></slot>
</swiper-slide>
</swiper>
</swiper-slide>
<swiper-slide class="right" ref="right" v-bind:style="{'background':getRandomColor()}">
<slot name="right"></slot>
</swiper-slide>
</swiper>
</div>
</template>
<script>
import {swiper, swiperSlide, swiperWraper} from 'vue-awesome-swiper'
export default {
name: "s-slider",
props: ['leftWidth','rightWidth','topHeight','bottomHeight'],
data() {
return {
horizontalSwiperOptions: {
slidesPerView: 'auto',
initialSlide: 0,
direction: 'horizontal'
},
verticalSwiperOptions:{
slidesPerView: 'auto',
initialSlide: 0,
direction: 'vertical'
}
}
},
mounted() {
setTimeout(() => {
this._initMenuWidth();
}, 20);

},
methods: {
_initMenuWidth() {
this.$refs.left.$el.style.width = this.leftWidth;
this.$refs.right.$el.style.width = this.rightWidth;
this.$refs.top.$el.style.height = this.topHeight;
this.$refs.bottom.$el.style.height = this.bottomHeight;
this.horizontalSwiper.updateSlides();
this.horizontalSwiper.slideTo(1, 1000, false);
this.verticalSwiper.updateSlides();
this.verticalSwiper.slideTo(1, 1000, false);
},
/*获取随机颜色*/
getRandomColor() {
return "#" + ("00000" + ((Math.random() * 16777215 + 0.5) >> 0).toString(16)).slice(-6);
}
},
computed: {
horizontalSwiper() {
return this.$refs.horizontalSwiper.swiper;