vue+swiper实现侧滑菜单效果


Posted in Javascript onDecember 28, 2017

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

先上效果图:

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; 
  }, 
  verticalSwiper(){ 
  return this.$refs.verticalSwiper.swiper; 
  } 
 } 
 } 
</script> 
 
<style scoped lang="scss"> 
 @import "src/base/css/public/variable.scss"; 
 @import "swiper/dist/css/swiper.css"; 
 
 .s-slider { 
 height: 100%; 
 color: white; 
 .swiper-container { 
  @include fill-with-parent 
 } 
 } 
</style>

 该组件自定义了四个属性,分别是左右侧滑菜单的宽度,上下滑动菜单的高度,leftWdith、rightWidth、topHeight、bottomHeight,然后用了一个横向的轮播用来存放左滑菜单,中间内容,右滑菜单,然后在中间内容又放了一个纵向的轮播用来放置上滑菜单,内容以及下滑菜单,具体思路就是这样。在组件挂载的时候,需要根据父组件传入的数值去初始化四个菜单的宽高,初始化完毕宽高之后,还要调用swiper本身的updateSlides更新所有的slides,不然滑动的时候,还是按照没设置之前的宽高进行滑动。在父组件中调用:

<s-slider leftWidth="200px" rightWidth="300px" topHeight="100px" bottomHeight="150px"> 
  <div slot="left"> 
  left 
  </div> 
  <div slot="content"> 
  Content 
  </div> 
  <div slot="right"> 
  right 
  </div> 
  <div slot="top"> 
  top 
  </div> 
  <div slot="bottom"> 
  bottom 
  </div> 
 </s-slider>

不要忘了在父组件中还要引入这个vue组件。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
js substr、substring和slice使用说明小记
Sep 15 Javascript
JQuery1.6 使用方法三
Nov 23 Javascript
jquery next nextAll nextUntil siblings的区别介绍
Oct 05 Javascript
Jquery解析json字符串及json数组的方法
May 29 Javascript
Vue + Webpack + Vue-loader学习教程之相关配置篇
Mar 14 Javascript
详解angularjs利用ui-route异步加载组件
May 21 Javascript
JS实现无缝循环marquee滚动效果
May 22 Javascript
vue修改vue项目运行端口号的方法
Aug 04 Javascript
vue中进入详情页记住滚动位置的方法(keep-alive)
Sep 21 Javascript
JS实现的类似微信聊天效果示例
Jan 29 Javascript
vxe-table vue table 表格组件功能
May 26 Javascript
JavaScript如何操作css
Oct 24 Javascript
swiper插件自定义切换箭头按钮
Dec 28 #Javascript
swiper移动端轮播插件(触碰图片之后停止轮播)
Dec 28 #Javascript
Vue组件通信之Bus的具体使用
Dec 28 #Javascript
基于Swiper实现移动端页面图片轮播效果
Dec 28 #Javascript
Swiper自定义分页器使用详解
Dec 28 #Javascript
swiper自定义分页器使用方法详解
Sep 14 #Javascript
JavaScript实现数值自动增加动画
Dec 28 #Javascript
You might like
php pcntl_fork和pcntl_fork 的用法
2009/04/13 PHP
gd库图片下载类实现下载网页所有图片的php代码
2012/08/20 PHP
JS代码格式化和语法着色V2
2006/10/14 Javascript
javascript 建设银行登陆键盘
2008/06/10 Javascript
js AppendChild与insertBefore用法详细对比
2013/12/16 Javascript
Express作者TJ告别Node.js奔向Go
2014/07/14 Javascript
HTML5实现留言和回复页面样式
2015/07/22 Javascript
贴近用户体验的Jquery日期、时间选择插件
2015/08/19 Javascript
基于jquery实现即时检查格式是否正确的表单
2016/05/06 Javascript
关于JSON与JSONP简单总结
2016/08/16 Javascript
jQuery插件HighCharts实现的2D堆条状图效果示例【附demo源码下载】
2017/03/14 Javascript
vue.js中Vue-router 2.0基础实践教程
2017/05/08 Javascript
react系列从零开始_简单谈谈react
2017/07/06 Javascript
JS中使用textPath实现线条上的文字
2017/12/25 Javascript
如何从零开始利用js手写一个Promise库详解
2018/04/19 Javascript
php结合js实现多条件组合查询
2019/05/28 Javascript
详解React中共享组件逻辑的三种方式
2021/02/02 Javascript
[02:43]2014DOTA2国际邀请赛 官方Alliance战队纪录片
2014/07/14 DOTA
Python psutil模块简单使用实例
2015/04/28 Python
Python获取央视节目单的实现代码
2015/07/25 Python
Python实现爬虫抓取与读写、追加到excel文件操作示例
2018/06/27 Python
Python读取txt文件数据的方法(用于接口自动化参数化数据)
2018/06/27 Python
python 把列表转化为字符串的方法
2018/10/23 Python
11个Python3字典内置方法大全与示例汇总
2019/05/13 Python
实例详解Python模块decimal
2019/06/26 Python
python关于矩阵重复赋值覆盖问题的解决方法
2019/07/19 Python
python的scipy实现插值的示例代码
2019/11/12 Python
如何更换python默认编辑器的背景色
2020/08/10 Python
html2canvas把div保存图片高清图的方法示例
2018/03/05 HTML / CSS
结婚周年感言
2014/02/24 职场文书
目标责任书范文
2014/04/14 职场文书
团日活动总结模板
2014/06/25 职场文书
党干部专题民主生活会对照检查材料思想汇报
2014/10/06 职场文书
入党积极分子自我批评思想汇报
2014/10/10 职场文书
四风问题查摆剖析材料
2014/10/11 职场文书
毕业生登记表班级意见
2015/06/05 职场文书