自己模拟的复选框

效果图:
这里写图片描述
gif:
这里写图片描述
单个复选框

<template>
    <span class="home" @click="change">
        <span class="home_checkbox"
            :class="{active:checked}"></span>
        <label>{{_label}}</label>
    </span>
</template>

<script>
export default {
  props: ['_label', 'isAllCehcked'],
  data () {
    return {
      checked: false
    }
  },
  watch: {
    isAllCehcked (val) {
      if (val !== undefined) {
        this.checked = val
      }
    }
  },
  methods: {
    change () {
      this.checked = !this.checked
      this.$emit('change', {checked: this.checked, label: this._label})
    },
    toChecked () {
      this.checked = true
    },
    toUnChecked () {
      this.checked = false
    }
  }
}
</script>

<style scoped lang="scss">
.home{
    &_checkbox{ display: inline-block;
        width: 6px;
        height: 6px;
        border-radius: 50%;
        border: 1px solid #FFFFFF;
        box-sizing: border-box;
        cursor: pointer;
    }
    label{
        font-size: 13px;
        color: #fff;
        cursor: pointer;
    }
    .active{
        background: #0DAACE;
        border: 1px solid #0DAACE;
    }
}
</style>

组合应用

<template>
<div class="home">
    <!-- 复选框 -->
    <div class="home_selections">
        <!-- 左边全选 -->
        <div class="home_selections_left">
            <checkBox :_label="all" @change="changeAll" :isAllCehcked.sync="isAllCehcked"></checkBox>
        </div>
        <!-- 右边每个选项框 -->
        <div class="home_selections_right">
            <div v-for="(item,index) in items" :key="index">
                <checkBox :_label="item" @change="changeItem" :ref="'checkbox'+index"></checkBox>
            </div>
        </div>
    </div>
    <!-- 排名 -->

</div>
</template>

<script>
/* eslint-disable */ 
import checkBox from './checkBox'
export default {
  components: {
    checkBox
  },
  data () {
    return {
      all: '全部',
      items: ['运检', '营销', '建设', '通信', '后勤', '集体企业'],
      isAllCehcked: false,
    }
  },
  computed: {
    // newIsAllCehcked () {
    //   const newIsAllCehcked = this.isAllCehcked
    //   return newIsAllCehcked
    // }
  },
  methods: {
    changeAll (item) {
      var e
      if (item.checked) {
        this.isAllCehcked = true
        for (e in this.$refs) {
          this.$refs[e][0].toChecked()
        }
      } else {
          this.isAllCehcked = false
        for (e in this.$refs) {
          this.$refs[e][0].toUnChecked()
        }
      }
    },
    changeItem (item) {
      var e
      var flag
      if (!item.checked) {
        this.isAllCehcked = false
      } else {
         flag =0
        for (e in this.$refs) {
          if (!this.$refs[e][0].checked) {
            flag += 1
          }
        }
        if(flag===0){
            this.isAllCehcked = true
        }
      }
    }
  }
}
</script>

<style scoped lang="scss">
@mixin size($width,$height){
    width: $width;
    height: $height;
}
.home{
    @include size(100%,170px);
    &_selections{
        height: 50px;
        padding:5px 10px;
        box-sizing: border-box;
        &_left{
            float: left;
        }
        &_right{
            height: 100%;
            display: flex;
            flex-wrap: wrap;
            justify-content: flex-end;
            div{
                min-width: 70px;
            }
        }
    }
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值