鍍金池/ 問答/Java  HTML/ 用better-scroll做的輪播圖,為什么不能無縫循環(huán)?

用better-scroll做的輪播圖,為什么不能無縫循環(huán)?

一.實(shí)錘:
1.1 html:

  <div class="slider" ref="slider">
    <div class="slider-group" ref="sliderGroup">
      <div v-for="item in recommends">
        <a :href="item.linkUrl">
          <img class="needsclick" @load="loadImage" :src="item.picUrl">
        </a>
    </div>
    <div class="dots">
      <span class="dot" :class="{active: currentPageIndex === index }" v-for="(item, index) in dots"></span>
    </div>
  </div>

1.2 script:


<script>
import BScroll from 'better-scroll'
import {addClass} from 'common/js/dom'
export default{
  props: {
    loop: {
      type: Boolean,
      default: true
    },
    autoPlay: {
      type: Boolean,
      default: true
    },
    interval: {
      type: Number,
      default: 4000
    }
  },
  data() {
    return {
      dots: [],
      currentPageIndex: 0
    }
  },
  mounted() {
    setTimeout(() => {
      this._setSliderWidth()
      this._initSlider()
      this._initDots()

      if (this.autoPlay) {
        this._play()
      }
    }, 20)

    window.addEventListener('resize', () => {
      if (!this.slider) {
        return
      }
      this._setSliderWidth(true)
      this.slider.refresh()
    })
  },
  methods: {
    _setSliderWidth(isResize) {
      // 取輪播組的子元素
      this.children = this.$refs.sliderGroup.children

      let width = 0
      // 取輪播組件寬度(即屏幕寬度)
      let sliderWidth = this.$refs.slider.clientWidth
      for (var i = 0; i < this.children.length; i++) {
        let child = this.children[i]
        addClass(child, 'slider-item')

        // 設(shè)置輪播子圖寬度為屏幕寬度
        child.style.width = sliderWidth + 'px'
        // 將輪播子圖累加
        width += sliderWidth
      }

      // ???
      if (this.loop && !isResize) {
        width += 2 * sliderWidth
      }
      this.$refs.sliderGroup.style.width = width + 'px'
    },
    _initSlider() {
      this.slider = new BScroll(this.$refs.slider, {
        scrollX: true,
        scrollY: false,
        momentum: false,
        snap: true,
        snapLoop: this.loop, // 循環(huán)
        snapThreshold: 0.3,
        snapSpeed: 400,
        click: true
      })

      this.slider.on('scrollEnd', () => {
        let pageIndex = this.slider.getCurrentPage().pageX
        if (this.loop) {
          pageIndex -= 1
        }
        this.currentPageIndex = pageIndex

        if (this.autoPlay) {
          clearTimeout(this.timer)
          this._play()
        }
      })
    },
    _initDots() {
      this.dots = new Array(this.children.length)
    },
    _play() {
      let pageIndex = this.currentPageIndex + 1
      if (this.loop) {
        pageIndex += 1
      }
      this.timer = setTimeout(() => {
        this.slider.goToPage(pageIndex, 0, 400)
      }, this.interval)
    }
  }
}
</script>

二.線索:

  1. 可以輪播,但不能循環(huán)(不能從第一張往前翻,不能從最后一張往后翻)
  2. 按教程里給slider-group增加了2個(gè)slider的寬度(是為無縫循環(huán)準(zhǔn)備的?),但他們出現(xiàn)在輪播圖片的末尾(2個(gè)空白)
  3. 第一張圖沒有對(duì)應(yīng)dot,第二張圖對(duì)應(yīng)第一個(gè)dot,以此類推
  4. 對(duì)比了教程里的代碼,幾乎沒有區(qū)別,數(shù)據(jù)來源也是一樣,替換了代碼也是這樣的表現(xiàn),so百思不得其解

完整代碼:github

找到原因了,是版本的問題,在0.1.15版本這么寫是沒問題的,snap的各個(gè)配置項(xiàng)并列寫,但在新版本要寫在一個(gè)snap對(duì)象內(nèi),就像采納的答案寫的那樣,另外還有幾個(gè)小修改,不知是個(gè)人原因還是版本差異,總之先貼出來給大家參考吧
`

  1. 降版本后dots會(huì)多兩個(gè):
    _initDots() {
      // this.dots = new Array(this.children.length) // 原寫法
      this.dots = new Array(this.children.length - 2)
    },

2.升版本,修改創(chuàng)建better-scroll的配置時(shí),dot與圖片不對(duì)應(yīng):

  // bs對(duì)象配置方法中(配置后):
  this.slider.on('scrollEnd', () => {
        let pageIndex = this.slider.getCurrentPage().pageX
        // 老版本有,新版去掉:
        // if (this.loop) {
        //   pageIndex -= 1
        // }
        this.currentPageIndex = pageIndex

        if (this.autoPlay) {
          clearTimeout(this.timer)
          this._play()
        }
      })
回答
編輯回答
神曲

better-scroll的版本從0.1.15已經(jīng)到1.x的版本了,把源碼的BS更新到最新版本也是這個(gè)情況,但..我也是才發(fā)現(xiàn)的不知道怎么解決

2017年6月22日 03:36
編輯回答
老梗

我也剛剛遇到這個(gè)情況,請(qǐng)問這個(gè)你找到答案了嗎?

2018年4月23日 00:00
編輯回答
溫衫

新版本已經(jīng)更新了,我也是找了好久

_initSlider() {

let slider = new BScroll(this.slider, {
  click: true,
  scrollX: true,
  scrollY: false,
  momentum: false,
  snap: {
    loop: true,
    threshold: 0.3,
    speed: 400
  },
})

這么寫就可以了
2017年7月4日 20:09