swiper的使用 在vue中

14 篇文章 0 订阅
2 篇文章 0 订阅

swiper的安装与使用:
https://github.com/surmon-china/vue-awesome-swiper

以下代码转自:https://www.cnblogs.com/scale/p/9294516.html
(链接里面有详细解释步骤)
代码:

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">第一页</swiper-slide>
          <!-- 第二页 -->
          <swiper-slide class="swiper-slide2">第二页</swiper-slide>
          <!-- 第三页 -->
          <swiper-slide class="swiper-slide3">第三页</swiper-slide>
          <!-- 下一页 -->
		<div class="swiper-button-next" slot="button-next"></div>
      </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更新
        loop: true,//页面可循环,尾页之后下一页是首页
       
        //以下两个未用到,仅供参考
        pagination: {
              el: '.swiper-pagination',
              clickable: true // 允许点击小圆点跳转
            },
       autoplay: {
         delay: 1000,
         disableOnInteraction: false // 手动切换之后继续自动轮播
       },
       
 
        // 如果自行设计了插件,那么插件的一些配置相关参数,也应该出现在这个对象中,如下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>

ps:如果
direction: “horizontal”,//控制垂直滚动的方向
需设置:(否则只显示一半)

.swiper-container-autoheight, .swiper-container-autoheight .swiper-slide{
    height: 100vh;
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值