swiper 滑块视图容器

    change事件中的source字段,表示触发change事件的原因,可能值如下:

    说明
    autoplay自动播放导致的swiper切换
    touch用户划动导致的swiper切换
    ""其他原因将返回空字符串

    请使用百度APP扫码

    1. Page({
    2. data: {
    3. items: [
    4. {
    5. className: 'color-a',
    6. value: 'A'
    7. }, {
    8. className: 'color-b',
    9. value: 'B'
    10. }, {
    11. className: 'color-c',
    12. value: 'C'
    13. }
    14. ],
    15. imgUrls: [
    16. 'https://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
    17. 'https://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
    18. 'https://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
    19. 'https://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg'
    20. ],
    21. current: 0,
    22. switchIndicateStatus: true,
    23. switchAutoPlayStatus: false,
    24. autoPlayInterval: 2000
    25. },
    26. console.log('swiperChange:', e.detail);
    27. },
    28. switchIndicate() {
    29. this.setData({
    30. switchIndicateStatus: !this.getData('switchIndicateStatus')
    31. });
    32. },
    33. switchAutoPlay() {
    34. this.setData({
    35. switchAutoPlayStatus: !this.getData('switchAutoPlayStatus')
    36. });
    37. },
    38. changeSwitchDuration(e) {
    39. this.setData({
    40. switchDuration: e.detail.value
    41. });
    42. },
    43. changeAutoPlayInterval(e) {
    44. this.setData({
    45. autoPlayInterval: e.detail.value
    46. });
    47. },
    48. animationfinish() {
    49. console.log('animationfinish');
    50. }
    51. });
    • Tip:如果在 bindchange 的事件回调函数中使用 setData 改变 current 值,则会导致 setData 被重复调用,因而通常情况下请在改变 current 值前检测 source 字段来判断是否是由于用户触摸引起的。
    • Tip:其中只可放置 swiper-item 组件,否则会导致未定义的行为。
    1. data: {
    2. currentTab: 0,
    3. swiperNav(e) {
    4. console.log(e);
    5. if (this.data.currentTab === e.target.dataset.current) {
    6. return false;
    7. } else {
    8. this.setData({
    9. currentTab: e.target.dataset.current,
    10. })
    11. }
    12. },
    13. swiperChange: function (e) {
    14. console.log(e);
    15. this.setData({
    16. currentTab: e.detail.current,
    17. })
    18. }
    19. })

    在开发者工具中预览效果

    1. <view class="swiper-wrap">
    2. <swiper autoplay="auto" interval="5000" duration="500" current="{{swiperCurrent}}" bindchange="swiperChange" class="swiper">
    3. <swiper-item s-for="{{slider}}">
    4. <image src="{{item.imageUrl}}" class="img"></image>
    5. </swiper-item>
    6. </swiper>
    7. <view class="dots">
    8. <view s-for="{{slider}}" class="dot {{index == swiperCurrent ? ' active' : ''}}"></view>
    9. </view>
    10. </view>