vue列表展开收起

1,174 阅读1分钟
  <div class="container">
    <div class="box_onfo">
      <div class="info_item">
        <span>当前地区:</span>
        <ul>
          <li>深圳市</li>
          <li>其他地区</li>
        </ul>
      </div>
      <div class="info_item">
        <span>其他地区:</span>
        <ul>
          <li v-for="(item, index) in testList" :key="index" :class="{'activeClass':activeIndex==index}" @click="getIndex(index)">{{ item }}</li>
        </ul>
        <div style="width:50px" v-if="location.length > 6" @click="showList = !showList">
          {{ showList ? "收起" : "更多" }}
        </div>
      </div>
    </div>
  </div>

export default {

  data() {
    return {
      showList: false,
      location: [
        "宝安区",
        "罗湖区",
        "龙岗区",
        "福田",
        "南山",
        "龙华",
        "坪山",
        "宝安区1",
        "罗湖区1",
        "龙岗区1",
        "福田1",
        "南山1",
        "龙华1",
        "坪山1",
        "宝安区2",
        "罗湖区2",
        "龙岗区2",
        "福田2",
        "南山2",
        "龙华2",
        "坪山2",
      ],
      activeIndex:0
    };
  },
  computed: {
    testList(){
      if(this.showList == false){
         let testList = []
        if(this.location.length >6){
          for(var i=0;i<6;i++){
            testList.push(this.location[i])
          }
        }else{
          testList = this.location
        }
        return testList
     
    }else{
      return this.location
    }
    
  },
  },
  methods: {
    getIndex(index){
      this.activeIndex = index
    },
  },
};

.box_onfo {
  width: 60%;
  margin: 50px auto;
  padding: 20px 30px;
  background: #e5e5e5;
  border-radius: 14px;
  .info_item {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    span {
      display: block;
      font-weight: 600;
      width: 100px;
    }
    ul {
      display: flex;
      flex-wrap: wrap;
      margin: 0;
      padding: 0;
      width: 80%;
    }
    li {
      list-style: none;
      margin: 5px 15px;
      padding: 1px 10px;
      font-size: 14px;
      min-width: 50px;
      text-align: center;
    }
  }
  .activeClass{
    background: chocolate;
    color: #fff;
    border-radius: 20px;
  }
}

最终效果图: