数组数据,每行显示不同颜色

数组数据,每行显示不同颜色


// js
data {
    c: ['red', 'yel', 'gre'],
    s: [],
    list: ['亲子关系', '高效学习', '能力培养', '性格塑造', '安全', '教育', '科学膳食', '健康管理', '教育咨询']
},

mounted () {
  this.$nextTick(() => {
    for (let i = 0; i<this.list.length; i++) {
    // 根据你屏幕距离进行计算--请结合实际情况
      let l = this.$refs.item[i].getBoundingClientRect().left - 220
      if (l === 20) {
        this.s.push(i)
      }
     }
     console.log('s:', this.s)
    })
}


// html
<div class="test">
  <div class="test-item" ref="item" v-for="(item, ci) in list">{{item}}</div>
  <div class="test-show">
     <div v-for="(sitem, si) in s" class="line" :class="c[si]">
         <div v-for="(litem, li) in list" >
         <div class="test-item" v-if="li>=s[si] && li<s[si+1]">{{litem}}</div>
         <div class="test-item" v-if="s[s.length-1]<=li && si===s.length-1">{{litem}}</div>
     </div>
  </div>
  </div>
</div>


// css
.test {
  position: relative;
  width: 375px; height: 667px;background:pink;padding: 40px 20px;
  &-item {
    display: inline-block;
    padding: 7px 8px;
    margin-right: 25px;
    margin-bottom: 10px;
    color: transparent;
  }
  &-show {
    position: absolute;
    top: 40px;
  }
  .line {
    display: flex;
  }
  .red {
    .test-item {
      color: red;
      border: 1px solid red;
    }
  }
  .yel {
    .test-item {
      color: yellow;
      border: 1px solid yellow;
    }
  }
  .gre {
    .test-item {
      color: green;
      border: 1px solid green;
    }
  }
}

可扩展..... 鼠标点击或者滑过  改变背景颜色 等.....待续......

 

 

 

 

 

 

 

░░░░░░░░░░░░░░░░░░░░与刘大仙恋爱♡♡♡1184天啦~~~~~~~░░░░░░░░░░░░░░░░░░░░

❈❉✿❀ღ❤          一生之中一定会遇到某个人,他打破你的原则,成为你的例外,成就你全世界的幸福。       ❈❉✿❀ღ❤