glTab.js 1.61 KB
// components/glTab/glTab.js
import util from '../../utils/util.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tabList: {
      type: Array,
      value: []
    },
    activeColor:{
      type:String,
      value:'#05BAA0'
    },
    activeDotColor:{
      type:String,
      value:'#05BAA0'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    tabIndex:0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    change(e) {
      let {
        index
      } = e.currentTarget.dataset
      this.setTab(index)
      this.triggerEvent('change',index)
    },
    setTab(index = 0) {
      let {
        tabIndex,
        activeDotColor
      } = this.data
      util.getViewInfo(`#tab-${tabIndex}`, old => {
        let oldLeft = old.left
        let oldWidth = old.width
        util.getViewInfo(`#tab-${index}`, res => {
          let {
            left,
            width
          } = res
          if (tabIndex != index) {
            let readyLeft = oldLeft > left ? left : oldLeft
            let readyWidth = oldLeft > left ? oldLeft - left : left - oldLeft
            this.setData({
              activeStyle: `width:${readyWidth - oldWidth / 2 + width / 2}px;background-color:${activeDotColor};border-radius:100%;left:${readyLeft + oldWidth / 2}px;transition: all .1s;`
            })
          }
          setTimeout(() => {
            this.setData({
              activeStyle: `width:${width}px;left:${left}px;background-color:${activeDotColor};`,
              tabIndex: index
            })
          }, 100)
        },this)
      },this)
    },
    
  },
  ready(){
    this.setTab()
  }
})