欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

程序员文章站 2022-07-22 07:56:33
说到h5的翻页,很定第一时间想到的是swiper。但是我当时想到的却是,vue里边怎么用swiper?! vue-awesome-swiper就是其中一个前辈们产出的结晶...

说到h5的翻页,很定第一时间想到的是swiper。但是我当时想到的却是,vue里边怎么用swiper?!

vue-awesome-swiper就是其中一个前辈们产出的结晶。就看首尾两个单词,就知道他是vue和swiper的爱情之子了。

官网是中文文档,妈妈再也不用担心我读api啦。“基于 swiper4、适用于 vue 的轮播组件”。在产品催着进度的紧张环境下,在四处搜寻解决方案的情况下,这句话简直发着光啊。

具体怎么用,官方文档写的很清楚,但我还是想记录下来,好再普及一波。

一、天才第一步,这里没有纸尿裤((⊙﹏⊙) 好像暴露了年龄...)

准备一个基于vue-cli的项目(或者你自己的脚手架,whatever),没有准备的请移驾vue-cli教程:

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

二、在项目目录下,往node_modules里安装插件vue-awesome-swiper(可以在项目目录内,shift+鼠标右键,选择"在此处打开命令窗口"),

 vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

注释:我这里window电脑安装了,所有右键出现的可能和没安装的不一样,实在找不到的看这一篇教程:gulp安装流程、使用方法及cmd常用命令导览

然后输入下边的安装插件命令

npm i --s vue-awesome-swiper

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

三、第三步我们依旧没有天才牌裤子,所以还是自己一步一步稳稳走。

1.插件安装后就是引用插件了,main.js内部分三步走:

(1) 引用插件:mainjs注册vas

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

(2) 注册插件

注册后未use控制台就警告了

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

通过全局方法 vue.use() 使用插件后可以了

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

使用后控制台无错了

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

(3) 插件样式(也可以自己写,不用人家的)

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

2.vue项目中使用:

(1) 准备-template

new a project准备一个空项目

 vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

初始化html-空结构

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

(2) 引入-import

 vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

(3) 注册组件-components

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

(4) 配置-js(具体配置和swiper的一摸一样,看swiper官网即可。我这里只说下我本次使用的配置的含义)

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

(5)美化- css

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

wan~

效果:

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】

代码:

main.js

import vueawesomeswiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
vue.use(vueawesomeswiper)

zujian.vue

<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>

总结

以上所述是小编给大家介绍的vue-awesome-swiper 基于vue实现h5滑动翻页效果【推荐】,希望对大家有所帮助