中国有句古话叫:天塌下来有个高的顶着。javascript
在前端圈里,总有前仆后继的仁人志士相继挥洒着热汗(这里没有血),在咱们小白须要用到两个技术结合的时候,他们早已冲向前为咱们杀出了一条路,准备好了实现用的技术和方案。有时候我就在想,个人理想大概就是站在本身如今仰望的代码塔的顶峰,像他们同样,不断输出本身的热情和力量,产出一个能够为人所用的东西,以可以为后来人发光发热吧。css
vue-awesome-swiper就是其中一个前辈们产出的结晶。就看首尾两个单词,就知道他是vue和swiper的爱情之子了。html
先来仰望一波——>九点钟方向。vue-awesome-swiper官网是中文文档,妈妈不再用担忧我读api啦。“基于 Swiper四、适用于 Vue 的轮播组件”。在产品催着进度的紧张环境下,在四处搜寻解决方案的状况下,这句话简直发着光啊。前端
具体怎么用,官方文档写的很清楚,但我仍是想记录下来,好再普及一波。vue
1、天才第一步,这里没有纸尿裤((⊙﹏⊙) 好像暴露了年龄...)java
准备一个基于vue-cli的项目(或者你本身的脚手架,whatever),没有准备的请移驾vue-cli教程:node
http://www.javashuo.com/article/p-csfzrmml-bn.htmlgit
2、在项目目录下,往node_modules里安装插件vue-awesome-swiper(能够在项目目录内,shift+鼠标右键,选择"在此处打开命令窗口"),github
注释:我这里window电脑安装了,全部右键出现的可能和没安装的不同,实在找不到的看这一篇教程:gulp安装流程、使用方法及CMD经常使用命令导览vue-cli
而后输入下边的安装插件命令
npm i --s vue-awesome-swiper
3、第三步咱们依旧没有天才牌裤子,因此仍是本身一步一步稳稳走。
1.插件安装后就是引用插件了,main.js内部分三步走:
(1) 引用插件:mainjs注册vas
(2) 注册插件
注册后未use控制台就警告了
经过全局方法 Vue.use() 使用插件后能够了
使用后控制台无错了
(3) 插件样式(也能够本身写,不用人家的)
2.vue项目中使用:
(1) 准备-template
new a project准备一个空项目
初始化html-空结构
(2) 引入-import
(3) 注册组件-components
(4) 配置-js(具体配置和swiper的一摸同样,看swiper官网便可。我这里只说下我本次使用的配置的含义)
(5)美化- css
wan~
效果:
代码:
main.js
1
2
3
|
import
VueAwesomeSwiper from
'vue-awesome-swiper'
import
'swiper/dist/css/swiper.css'
Vue.use(VueAwesomeSwiper)
|
zujian.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
<template>
<div
class
=
"hello-world"
>
<!-- <h3>https:
//github.com/surmon-china/vue-awesome-swiper</h3> -->
<!-- <h3>http:
//www.swiper.com.cn/api/index2.html</h3> -->
<!-- http:
//www.swiper.com.cn/api/parameters/21.html -->
<transition name=
"fade"
>
<swiper id=
"swiperBox"
v-bind:options=
"swiperOption"
ref=
"mySwiper"
>
<!-- 第一页 -->
<swiper-slide
class
=
"swiper-slide1"
>
<div
class
=
"page"
>
<h3>第一页</h3>
</div>
</swiper-slide>
<!-- 第二页 -->
<swiper-slide
class
=
"swiper-slide2"
>
<div
class
=
"page"
>
<h3>第二页</h3>
</div>
</swiper-slide>
<!-- 第三页 -->
<swiper-slide
class
=
"swiper-slide3"
>
<div
class
=
"page"
>
<h3>第三页</h3>
</div>
</swiper-slide>
</swiper>
</transition>
</div>
</template>
<script>
import
{ swiper, swiperSlide } from
'vue-awesome-swiper'
export
default
{
name:
'HelloWorld'
,
components: {
swiper,
swiperSlide
},
data () {
return
{
swiperOption: {
// swiper configs 全部的配置同swiper官方api配置
notNextTick:
true
,
//notNextTick是一个组件自有属性,若是notNextTick设置为true,组件则不会经过NextTick来实例化swiper,也就意味着你能够在第一时间获取到swiper对象,假如你须要刚加载遍使用获取swiper对象来作什么事,那么这个属性必定要是true
direction:
'vertical'
,
//水平方向移动
grabCursor:
true
,
//鼠标覆盖Swiper时指针会变成手掌形状,拖动时指针会变成抓手形状
setWrapperSize:
true
,
//Swiper使用flexbox布局(display: flex),开启这个设定会在Wrapper上添加等于slides相加的宽或高,在对flexbox布局的支持不是很好的浏览器中可能须要用到。
autoHeight:
true
,
//自动高度。设置为true时,wrapper和container会随着当前slide的高度而发生变化
slidesPerView: 1,
//设置slider容器可以同时显示的slides数量(carousel模式)。能够设置为数字(可为小数,小数不可loop),或者 'auto'则自动根据slides的宽度来设定数量。loop模式下若是设置为'auto'还须要设置另一个参数loopedSlides。
mousewheel:
false
,
//开启鼠标滚轮控制Swiper切换。可设置鼠标选项,默认值false
mousewheelControl:
false
,
//同上
height: window.innerHeight,
// 高度设置,占满设备高度
resistanceRatio: 0,
//抵抗率。边缘抵抗力的大小比例。值越小抵抗越大越难将slide拖离边缘,0时彻底没法拖离。本业务须要
observeParents:
true
,
//将observe应用于Swiper的父元素。当Swiper的父元素变化时,例如window.resize,Swiper更新
// 若是自行设计了插件,那么插件的一些配置相关参数,也应该出如今这个对象中,以下debugger
debugger:
true
,
// swiper的各类回调函数也能够出如今这个对象中,和swiper官方同样
onTransitionStart(swiper){
console.log(swiper)
}
}
},
computed: {
swiper() {
return
this
.$refs.mySwiper.swiper
}
},
mounted() {
// current swiper instance
// 而后你就可使用当前上下文内的swiper对象去作你想作的事了
console.log(
'this is current swiper instance object'
,
this
.swiper)
this
.swiper.slideTo(3, 1000,
false
)
}
}
</script>
<style scoped>
.fade-enter-active, .fade-leave-active {
transition: opacity .5s
}
.fade-enter, .fade-leave-to{
opacity: 0
}
.swiper-wrapper{
height: 100px;
}
.swiper-slide1{
background: skyblue;
}
.swiper-slide2{
background: yellowgreen;
}
.swiper-slide3{
background: blanchedalmond;
}
</style>
|
我的学习理解和总结,不少不足还请指正~
声明:
请尊重博客园原创精神,转载或使用图片请注明:
博主:xing.org1^
出处:http://www.cnblogs.com/padding1015/