Steps.vue 2.64 KB
Newer Older
李星剑's avatar
李星剑 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
<template>
  <view class="step">
     <view v-for="(item,index) in list" class="step-item" :key="index">
<!--       <view class="u-relative">-->
<!--         <view class="step-line" :class="[current === index ? 'active':'']"></view>-->
<!--         -->
<!--       </view>-->
       <view class="step-line" v-if="index !== list.length - 1"
             :class="[current >= index ? 'active':'']" :style="[lineStyle]"></view>
       <view class="step-circle u-m-b-24" :class="[current >= index ? 'active':'']"></view>
       <view class="step-item--title" :class="[current === index ? 'active-color':'']">{{item.title}}</view>
       <view class="step-item--desc">{{item.desc}}</view>
     </view>
  </view>
</template>

<script>
// #ifdef APP-NVUE
const dom = uni.requireNativePlugin('dom')
// #endif
export default {
  name: "steps",
  props:{
    current:{
      type: Number,
      default: 0
    },
    list:{
      type: Array,
      default: () => []
    }
  },
  data(){
    return {
      size: {
        height: 0,
        width: 0
      },
    }
  },
  computed:{
    lineStyle() {
      const style = {}
      style.width = this.size.width + 'px'
      style.left = this.size.width / 2 + 'px'
      return style
    },
  },
  mounted() {
    this.getStepsItemRect()
  },
  methods:{
    uGetRect(selector, all) {
      return new Promise((resolve) => {
        uni.createSelectorQuery()
            .in(this)[all ? 'selectAll' : 'select'](selector)
            .boundingClientRect((rect) => {
              if (all && Array.isArray(rect) && rect.length) {
                resolve(rect)
              }
              if (!all && rect) {
                resolve(rect)
              }
            })
            .exec()
      })
    },
    getStepsItemRect() {

      this.uGetRect('.step-item').then(size => {
        this.size = size
      })


      // #ifdef APP-NVUE
      dom.getComponentRect(this.$refs['u-steps-item'], res => {
        const {
          size
        } = res
        this.size = size
      })
      // #endif
    }
  }
}
</script>

<style scoped lang="scss">
.active{
  background: #1677FF !important;
}
.active-color{
  color: #1677FF !important;
}
.step{
  display: flex;
  &-item{
    flex: 1;
    justify-content: center;
    align-items: center;
    text-align: center;
    position: relative;
    &--title{
      font-size: 24rpx;
      color: #333;
    }

    &--desc{
      font-size: 24rpx;
      color: #999;
    }
  }

  &-line{
    background: #e5e5e5; height: 2rpx;
    position: absolute;
    width: 100%;
    top: 6rpx;

  }

  &-circle{
    margin: 0 auto;
    width: 16rpx; height: 16rpx;
    background: #e5e5e5; border-radius: 50%;
  }


}

</style>