动态切换样式

前端实现标签默认全选与点击标签选中切换


整体展示效果图

html代码

	 <div class="filterBox">
        <div class="filterTitle">
          请选择
        </div>
        <div class="filterContent">
          <span v-for="(item,index) in company" :key="index" :class="selectCompanyId.indexOf(item.id)!==-1? 'defaultSty':' cancelSty'" @click="selectCompany(item)">{{ item.name }}</span>
        </div>
      </div>

JavaScript代码

<script>
export default {
  data() {
    return {
      selectCompanyId: [1, 2, 3, 4],
      company: [
        {
          id: 1,
          name: '小米'
        }, {
          id: 2,
          name: '华为'
        }, {
          id: 3,
          name: '腾讯'
        }, {
          id: 4,
          name: '阿里巴巴'
        }
      ]
    }
  },
  methods: {
    // 选择
    selectCompany(data) {
    //最开始默认选中所有数据  第一次点击时取消所有数据选中状态  只保留点击标签的选中状态
      if (this.selectCompanyId.length === this.company.length) {
        this.selectCompanyId = []
      }
      if (this.selectCompanyId.indexOf(data.id) === -1) {
        this.selectCompanyId.push(data.id)
      } else {
      //再次点击选中的标签时   取消选中状态
        this.selectCompanyId.splice(this.selectCompanyId.indexOf(data.id), 1)
      }
      this.selectContent.push(data)
    },
  }
}
</script>

样式代码

使用的less编写

// 样式
  .filterBox{
    width: 100%;
    padding-left: 0.1rem;
    .filterTitle{
      width: 100%;
      margin: 0.2rem 0;
      text-align: left;
      color: #333333;
      font-size: 0.4rem;
    }
    .filterContent{
      width: 100%;
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      flex-wrap: wrap;
      align-items: center;
      span{
        display: inline-block;
        width: 44%;
        padding: 0.3rem;
        font-size: 0.36rem;
        text-align: center;
      }
      .defaultSty{
        color: #596DFF;
        background: rgba(152,164,249, 0.2);
        border:1px solid #596DFF;
      }
      .cancelSty{
        color: #333;
        background: #F2F2F2;
        border:1px solid #F2F2F2;
      }
      :nth-child(3){
        margin: 0.3rem 0;
      }
      :nth-child(4){
        margin: 0.3rem 0;
      }
    }
  }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值