Swiper 轮播

Scan me!

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

引入

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

代码演示

横向轮播 Goto 2PlayStop

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

渐隐轮播 Goto 2

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

每屏多内容 Goto 2

复杂结构,每屏显示一组多条内容
@before-change: from: 1, to: 2@after-change: from: 1, to: 2
  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" scoped>
  55. .md-example-child
  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, fade
default-index第一屏面板索引值Number0[0, length - 1]
has-dots控制面板指示点Booleantrue-
is-prevent是否阻止默认的事件,如页面滚动事件Booleantrueswiper-item绑定点击事件需将其设置为false
is-loop是否循环播放Booleantrue-
dragable是否禁用触摸滑动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]
  1. vm.$refs.swiper.goto(2)
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