Swiper 轮播

Scan me!

走马灯,用于一组图片或卡片轮播

引入

  1. import { Swiper, SwiperItem } from 'mand-mobile'
  2. Vue.component(Swiper.name, Swiper)
  3. Vue.component(SwiperItem.name, SwiperItem)

代码演示

横向轮播 Goto 2PlayStop

10秒后异步加载更多项,20秒后重置为初始数量
@before-change: from: 0, to: 1@after-change: from: 0, to: 1

Swiper 轮播 - 图2

  1. <template>
  2. <div class="md-example-child md-example-child-swiper md-example-child-swiper-0">
  3. <md-swiper
  4. @before-change="beforeChange"
  5. @after-change="afterChange"
  6. ref="swiper"
  7. :is-prevent="false"
  8. :useNative-driver="false"
  9. >
  10. <md-swiper-item :key="$index" v-for="(item, $index) in simple">
  11. <div
  12. class="banner-item"
  13. :style="{'background': `${item.color}`}">{{item.text}}</div>
  14. </md-swiper-item>
  15. </md-swiper>
  16. </div>
  17. </template>
  18. <script>
  19. import {Swiper, SwiperItem} from 'mand-mobile'
  20. import simple from 'mand-mobile/components/swiper/demo/data/simple'
  21. export default {
  22. name: 'swiper-demo',
  23. components: {
  24. [Swiper.name]: Swiper,
  25. [SwiperItem.name]: SwiperItem,
  26. },
  27. data() {
  28. return {
  29. simple,
  30. }
  31. },
  32. mounted() {
  33. // Simulation of asynchronous
  34. setTimeout(() => {
  35. this.simple = simple.concat(simple)
  36. }, 10000)
  37. // Simulation of asynchronous
  38. setTimeout(() => {
  39. this.simple = simple
  40. }, 24500)
  41. window.triggerSwiper0 = () => {
  42. this.goto()
  43. }
  44. window.triggerSwiper1 = () => {
  45. this.play()
  46. }
  47. window.triggerSwiper2 = () => {
  48. this.stop()
  49. }
  50. },
  51. methods: {
  52. setValue(id, value) {
  53. document.querySelector(id) && (document.querySelector(id).innerHTML = value)
  54. },
  55. beforeChange(from, to) {
  56. this.setValue('#valueSwiper0', from)
  57. this.setValue('#valueSwiper1', to)
  58. },
  59. afterChange(from, to) {
  60. this.setValue('#valueSwiper2', from)
  61. this.setValue('#valueSwiper3', to)
  62. },
  63. fn(index) {
  64. this.setValue('#valueSwiper4', index)
  65. },
  66. goto() {
  67. this.$refs.swiper.goto(2)
  68. },
  69. play() {
  70. this.$refs.swiper.play()
  71. },
  72. stop() {
  73. this.$refs.swiper.stop()
  74. },
  75. },
  76. }
  77. </script>
  78. <style lang="stylus">
  79. .md-example-child-swiper-0
  80. height 250px
  81. .banner-item
  82. float left
  83. width 100%
  84. height 100%
  85. line-height 250px
  86. text-align center
  87. font-size 28px
  88. color #FFF
  89. box-align center
  90. align-items center
  91. box-pack center
  92. justify-content center
  93. text-decoration-line none
  94. </style>
  95.  

渐隐轮播 Goto 2

@before-change: from: 0, to: 0@after-change: from: 0, to: 0

Swiper 轮播 - 图3

  1. <template>
  2. <div class="md-example-child md-example-child-swiper md-example-child-swiper-2">
  3. <md-swiper
  4. @before-change="beforeChange"
  5. @after-change="afterChange"
  6. :autoplay="5000"
  7. transition="fade"
  8. ref="swiper">
  9. <md-swiper-item :key="$index" v-for="(item, $index) in simple">
  10. <a href="javascript:void(0)"
  11. class="banner-item"
  12. :style="{'background': `${item.color}`}">{{item.text}}</a>
  13. </md-swiper-item>
  14. </md-swiper>
  15. </div>
  16. </template>
  17. <script>
  18. import {Swiper, SwiperItem} from 'mand-mobile'
  19. import simple from 'mand-mobile/components/swiper/demo/data/simple'
  20. export default {
  21. name: 'swiper-demo',
  22. components: {
  23. [Swiper.name]: Swiper,
  24. [SwiperItem.name]: SwiperItem,
  25. },
  26. data() {
  27. return {
  28. simple,
  29. }
  30. },
  31. mounted() {
  32. window.triggerSwiper3 = () => {
  33. this.goto()
  34. }
  35. },
  36. methods: {
  37. setValue(id, value) {
  38. document.querySelector(id) && (document.querySelector(id).innerHTML = value)
  39. },
  40. beforeChange(from, to) {
  41. this.setValue('#valueSwiper10', from)
  42. this.setValue('#valueSwiper11', to)
  43. },
  44. afterChange(from, to) {
  45. this.setValue('#valueSwiper12', from)
  46. this.setValue('#valueSwiper13', to)
  47. },
  48. goto() {
  49. this.$refs.swiper.goto(2)
  50. },
  51. },
  52. }
  53. </script>
  54. <style lang="stylus">
  55. .md-example-child-swiper-2
  56. height 250px
  57. .banner-item
  58. float left
  59. width 100%
  60. height 100%
  61. line-height 250px
  62. text-align center
  63. font-size 28px
  64. color #FFF
  65. box-align center
  66. align-items center
  67. box-pack center
  68. justify-content center
  69. text-decoration-line none
  70. </style>
  71.  

纵向轮播

纵向,默认显示第2屏(index 1),不显示导航点,不可拖动,滚动间隔为5秒
@before-change: from: 0, to: 0@after-change: from: 0, to: 0

Swiper 轮播 - 图4

  1. <template>
  2. <div class="md-example-child md-example-child-swiper md-example-child-swiper-1">
  3. <md-swiper
  4. @before-change="beforeChange"
  5. @after-change="afterChange"
  6. :default-index="1"
  7. :dragable="false"
  8. :autoplay="5000"
  9. transition="slideY">
  10. <md-swiper-item :key="$index" v-for="(item, $index) in simple">
  11. <a href="javascript:void(0)"
  12. class="banner-item"
  13. :style="{'background': `${item.color}`}">{{item.text}}</a>
  14. </md-swiper-item>
  15. </md-swiper>
  16. </div>
  17. </template>
  18. <script>
  19. import {Swiper, SwiperItem} from 'mand-mobile'
  20. import simple from 'mand-mobile/components/swiper/demo/data/simple'
  21. export default {
  22. name: 'swiper-demo',
  23. components: {
  24. [Swiper.name]: Swiper,
  25. [SwiperItem.name]: SwiperItem,
  26. },
  27. data() {
  28. return {
  29. simple,
  30. }
  31. },
  32. methods: {
  33. setValue(id, value) {
  34. document.querySelector(id) && (document.querySelector(id).innerHTML = value)
  35. },
  36. beforeChange(from, to) {
  37. this.setValue('#valueSwiper5', from)
  38. this.setValue('#valueSwiper6', to)
  39. },
  40. afterChange(from, to) {
  41. this.setValue('#valueSwiper7', from)
  42. this.setValue('#valueSwiper8', to)
  43. },
  44. },
  45. }
  46. </script>
  47. <style lang="stylus">
  48. .md-example-child-swiper-1
  49. height 250px
  50. .banner-item
  51. float left
  52. width 100%
  53. height 100%
  54. line-height 250px
  55. text-align center
  56. font-size 28px
  57. color #FFF
  58. box-align center
  59. align-items center
  60. box-pack center
  61. justify-content center
  62. text-decoration-line none
  63. </style>
  64.  

每屏多内容 Goto 2

复杂结构,每屏显示一组多条内容
@before-change: from: 0, to: 1@after-change: from: 0, to: 1

Swiper 轮播 - 图5

  1. <template>
  2. <div class="md-example-child md-example-child-swiper md-example-child-swiper-3">
  3. <md-swiper
  4. @before-change="beforeChange"
  5. @after-change="afterChange"
  6. ref="swiper">
  7. <md-swiper-item :key="$index" v-for="(item, $index) in mulit">
  8. <ul>
  9. <li :key="$index1" v-for="(sub, $index1) in item">
  10. <a href="javascript:void(0)" class="banner-item" :style="{'background': `${sub.color}`}">{{sub.text}}</a>
  11. </li>
  12. </ul>
  13. </md-swiper-item>
  14. </md-swiper>
  15. </div>
  16. </template>
  17. <script>
  18. import {Swiper, SwiperItem} from 'mand-mobile'
  19. import mulit from 'mand-mobile/components/swiper/demo/data/mulit-item'
  20. export default {
  21. name: 'swiper-demo',
  22. components: {
  23. [Swiper.name]: Swiper,
  24. [SwiperItem.name]: SwiperItem,
  25. },
  26. data() {
  27. return {
  28. mulit,
  29. }
  30. },
  31. mounted() {
  32. window.triggerSwiper4 = () => {
  33. this.goto()
  34. }
  35. },
  36. methods: {
  37. setValue(id, value) {
  38. document.querySelector(id) && (document.querySelector(id).innerHTML = value)
  39. },
  40. beforeChange(from, to) {
  41. this.setValue('#valueSwiper15', from)
  42. this.setValue('#valueSwiper16', to)
  43. },
  44. afterChange(from, to) {
  45. this.setValue('#valueSwiper17', from)
  46. this.setValue('#valueSwiper18', to)
  47. },
  48. goto() {
  49. this.$refs.swiper.goto(2)
  50. },
  51. },
  52. }
  53. </script>
  54. <style lang="stylus">
  55. .md-example-child-swiper-3
  56. height 250px
  57. li
  58. list-style none
  59. float left
  60. width 33%
  61. height 125px
  62. .banner-item
  63. float left
  64. width 100%
  65. height 100%
  66. line-height 125px
  67. text-align center
  68. font-size 28px
  69. color #FFF
  70. box-align center
  71. align-items center
  72. box-pack center
  73. justify-content center
  74. text-decoration-line none
  75. </style>
  76.  

API

Swiper Props

属性说明类型默认值可选值/备注
autoplay自动切换间隔时长(毫秒), 禁用可设置为0Number30000, [500, +Int.Max)
transition面板切换动画效果Stringslideslide, slideY, fade
transition-duration面板切换动画时长Number250单位ms
default-index第一屏面板索引值Number0[0, length - 1]
has-dots控制面板指示点Booleantrue-
is-prevent阻止默认的事件,如页面滚动事件Booleantrueswiper-item绑定点击事件需将其设置为false
is-loop循环播放Booleantrue-
dragable禁用触摸滑动Booleantrue-
use-native-driver开启3D加速Booleantrue-

Swiper Methods

play(autoplay)

打开自动切换

参数说明类型默认值可选值
autoplay自动切换间隔时长(毫秒)Number3000[500, +Int.Max)
  1. vm.$refs.swiper.play(5000)
stop()

停止自动切换

  1. vm.$refs.swiper.stop()
prev()

前一个item

  1. vm.$refs.swiper.prev()
next()

后一个item

  1. vm.$refs.swiper.next()
goto(index)

切换到某一个index

参数说明类型默认值可选值
index面板索引值Number0[0, length - 1]
js</td><td></td><td></td><td></td><td></td></tr><tr><td>vm.$refs.swiper.goto(2)</td><td></td><td></td><td></td><td></td></tr><tr><td>
getIndex()

获取当前显示的index

参数说明类型
index当前显示的indexNumber
  1. var index = vm.$refs.swiper.getIndex()

Swiper Events

@beforeChange(from, to)

轮播器将要切换前的事件

参数说明类型
from轮播器当前展示的索引值Number
to轮播器下一屏展示的索引值Number
@afterChange(from, to)

轮播器切换完成时的事件

参数说明类型
from轮播器当前展示的索引值Number
to轮播器下一屏展示的索引值Number