VUE下纯css实现checkbox

在网上找了半天很多都不是很全,这玩意虽然不难,但是自己写也很麻烦,我整了个全的效果如下:

下面上代码:

<template>中代码如下:

            <div class="checkbox-btn-group">
            <div class="checkbox">
              <label for="seCh0">
                <input type="checkbox" id="seCh0" name="selfCheck" value="seCh0" v-model="selfCheck"  />
                <span :class="showBtn?'enable-checkbox-style':'disable-checkbox-style'">苹果</span>
              </label>
            </div>
            <div class="checkbox">
              <label for="seCh1">
                <input type="checkbox" id="seCh1" name="selfCheck" value="seCh1" v-model="selfCheck"  />
                <span :class="showBtn?'enable-checkbox-style':'disable-checkbox-style'">橘子</span>
              </label>
            </div>
            <div class="checkbox">
              <label for="seCh2">
                <input type="checkbox" id="seCh2" name="selfCheck" value="seCh2" v-model="selfCheck"  />
                <span :class="showBtn?'enable-checkbox-style':'disable-checkbox-style'">香蕉</span>
              </label>
            </div>
            <div class="checkbox">
              <label for="seCh4">
                <input type="checkbox" id="seCh4" name="selfCheck" value="seCh4" v-model="selfCheck"  />
                <span :class="showBtn?'enable-checkbox-style':'disable-checkbox-style'">牛奶</span>
              </label>
            </div>
          </div>

data中一定要设置默认值:

data(){
    return{selfCheck:['seCh1'],}
}

css如下:

.checkbox-btn-group {
          width: 100%;
          .checkbox {
            word-wrap: break-word;
            word-break: normal;
            height: auto;
            padding: 10px 0;
            text-align: left;
            // height: 17px;
            line-height: 17px;
            font-size: 13px;
            color: rgba(51,51,51,1);
            display: flex;
            align-items: center;
            input[type="checkbox"] {
              width: 0;
              height: 0;
              opacity: 0;
            }
            span::before {
              content: "";
              display: inline-block;
              width: 14px;
              height: 14px;
              // border-radius: 50%;
              border: 1px solid rgba(190,190,190,1);
              box-sizing: border-box;
              margin-right: 6px;
              position: relative;
              top: 2px;
            }
            input[type="checkbox"]:checked + .enable-checkbox-style::before {
              padding: 2px;
              background-color: rgba(68,83,152,1);
              background-clip: content-box;
              border-color: rgba(68,83,152,1);
            }
            input[type="checkbox"]:checked + .disable-checkbox-style::before {
              padding: 2px;
              background-color: rgba(153,153,153,1);
              background-clip: content-box;
              border-color: rgba(153,153,153,1);
            }
            .other {
              width: 190px;
              height: 24px;
              border-radius: 2px;
              border: 1px solid rgba(68,83,152,1);
              font-size: 13px;
              margin: 0 0 0 8px;
            }
          }
        }
      }

radio就不赘述了,把type换成radio,默认值改成一个string类型就可以了。

很简单,但是有现成可以抄的还是比较爽的,哈哈哈

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值