swiper和自定义指令

本文介绍了如何使用第三方库Swiper实现移动端和PC端的滑动操作,包括静态和动态数据的实例化。同时,探讨了在Vue中优化Swiper的方法,如设置判断条件和异步队列。此外,文章还详细讲解了Vue的自定义指令功能,包括常见内置指令以及如何创建自定义指令,并阐述了自定义指令的各个钩子函数及其参数,为开发者提供了实现特定交互逻辑的指导。

swiper

作用: 用来实现移动端,pc端滑动操作

swiper是一个第三方的库

学习 swiper 官网文档 https://www.swiper.com.cn/

  <div class="swiper-container">
        <div class="swiper-wrapper">
            <div class="swiper-slide">Slide 1</div>
            <div class="swiper-slide">Slide 2</div>
            <div class="swiper-slide">Slide 3</div>
        </div>
        <!-- 如果需要分页器 -->
        <div class="swiper-pagination"></div>
        
        <!-- 如果需要导航按钮 -->
        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>
        
        <!-- 如果需要滚动条 -->
        <div class="swiper-scrollbar"></div>
    </div>
    // 第三库实例化
  var mySwiper = new Swiper ('.swiper-container', {
      // direction: 'vertical', // 垂直切换选项
      loop: true, // 循环模式选项
      
      // 如果需要分页器
      pagination: {
        el: '.swiper-pagination',
      },
      
      // 如果需要前进后退按钮
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
      
      // 如果需要滚动条
      scrollbar: {
        el: '.swiper-scrollbar',
      },
      autoplay: true
    })        
  • vue中swiper实现

    1. 静态数据的第三方库实例化,我们写在mounted中
    2. 动态数据的第三方库实例化,我们写在updated中
    3. 优化:
      1. 做判断条件
      2. 异步队列
      1. setTimout
      2. nextTick

有的开发者,也会使用 vue-swiper 这个第三方的库来实现轮播

<div id="app">
      <Banner></Banner>
  </div>


  <template id = "banner">
    <div>

      <input type="text" v-model = "num">
      <div class="swiper-container">
          <div class="swiper-wrapper">
              <div class="swiper-slide" 
                v-for = "banner in banners"
                :key = "banner.id"
              >
                {{ banner.text}}
              </div>
          </div>
          <!-- 如果需要分页器 -->
          <div class="swiper-pagination"></div>
          
          <!-- 如果需要导航按钮 -->
          <div class="swiper-button-prev"></div>
          <div class="swiper-button-next"></div>
          
          <!-- 如果需要滚动条 -->
          <div class="swiper-scrollbar"></div>
      </div>
    </div>
    
  </template>
    /* 
    第三方库的实例化要求,必须真实dom已经存在

    业务: 动态数据请求,然后进行第三方库实例化的优化
      理由: 将动态数据的第三方库实例化放在updated中是有弊端的,弊端是第三方库会重复实例化
    解决: 
      1. 加判断条件   if( this.mySwiper ) return false
      2. 将实例化放到异步队列中去
          - 将实例化放在created的数据请求中,然后放在setTimeout中
          - vue提供了一个它认为是最好的方案: nextTick    
            使用: Vue.nextTick    this.$nextTick
            nextTick表示组件渲染结束之后这个方法才会调用

      最优解决方案:  nextTick
   */
  Vue.component('Banner',{
    template: '#banner',
    data () {
      return {
        banners: null,
        num:100
      }
    },
    beforeCreate () {
      
    },
    created () {
      //数据请求一般写在这里
      fetch('./data.json')
        .then( res => res.json())
        .then( data => {
          this.banners = data
          //   setTimeout(()=>{
          //     this.mySwiper = new Swiper ('.swiper-container', {
          //       // direction: 'vertical', // 垂直切换选项
          //       loop: true, // 循环模式选项
                
          //       // 如果需要分页器
          //       pagination: {
          //         el: '.swiper-pagination',
          //       },
                
          //       // 如果需要前进后退按钮
          //       navigation: {
          //         nextEl: '.swiper-button-next',
          //         prevEl: '.swiper-button-prev',
          //       },
                
          //       // 如果需要滚动条
          //       scrollbar: {
          //         el: '.swiper-scrollbar',
          //     },
          //     autoplay: true
          //   })       

          // },0)



          this.$nextTick( () => {
            this.swiper = new Swiper('.swiper-container',{
              loop: true,
              autoplay: true,
              pagination: {
                el: '.swiper-pagination',
              },
              
              // 如果需要前进后退按钮
              navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
              },
              
              // 如果需要滚动条
              scrollbar: {
                el: '.swiper-scrollbar',
              }
            })
          })

        })
        .catch( error => console.log( error ))

    },
    beforeMount () {
      
    },
    mounted () {
      //静态数据的第三方实例化
      // 第三库实例化
  
    },
    updated () {
      console.log( 'updated')
      if( this.mySwiper ) return false
      
    }
  })
  new Vue({
    el: '#app'
  })

自定义指令 ( 白银 )

v-html

v-text

v-for

v-if

v-else-if

v-else

v-show

v-on

v-bind

v-model

以上指令可以不够用户使用,所以vue给开发者提供了自定义指令的方式

举例: 比如说 我想要有一个指令,来实现一个自动获得input焦点,或是一个轮播 。。。

有两种自定义指令的使用形式:

  1. 全局定义 ---- vue.directive()

  2. 局部定义

    directives: {}

  3. 指令的钩子函数

    • bind:只调用一次,指令第一次绑定到元素时调用。在这里可以进行一次性的初始化设置。

    • inserted:被绑定元素插入父节点时调用 (仅保证父节点存在,但不一定已被插入文档中)。

    • update:所在组件的 VNode 更新时调用,但是可能发生在其子 VNode 更新之前。指令的值可能发生了改变,也可能没有。但是你可以通过比较更新前后的值来忽略不必要的模板更新 (详细的钩子函数参数见下)。

    • componentUpdated:指令所在组件的 VNode 及其子 VNode 全部更新后调用。

    • unbind:只调用一次,指令与元素解绑时调用 ( 指令绑定的元素被删除 )。

  4. 自定义指令钩子的参数

    • el:指令所绑定的元素,可以用来直接操作 DOM 。

    • binding

      :一个对象,包含以下属性:

      • name:指令名,不包括 v- 前缀。
      • value:指令的绑定值,例如:v-my-directive="1 + 1" 中,绑定值为 2
      • oldValue:指令绑定的前一个值,仅在 updatecomponentUpdated 钩子中可用。无论值是否改变都可用。
      • expression:字符串形式的指令表达式。例如 v-my-directive="1 + 1" 中,表达式为 "1 + 1"
      • arg:传给指令的参数,可选。例如 v-my-directive:foo 中,参数为 "foo"
      • modifiers:一个包含修饰符的对象。例如:v-my-directive.foo.bar 中,修饰符对象为 { foo: true, bar: true }
    • vnode:Vue 编译生成的虚拟节点。移步 VNode API 来了解更多详情。

    • oldVnode:上一个虚拟节点,仅在 updatecomponentUpdated 钩子中可用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值