vue - 自定义Checkbox 样式组件

<template>
    <div class="checkbox_div">
        <div :class="`checkbox_div_one_${item.status}`" v-for="(item, index) in statusList" :key="index" @click="clickCheckbox(item)">
            <input type="checkbox" v-model="item.checked"  :class="`input_check_${item.status}`" :disabled="disabled" :checked="item.checked"
                />
            <span>{{item.status | convertCase}}</span>
        </div>
    </div>

</template>
<script>
export default {
/*
    dataList: [{
        * status: String,
        * checked: Boolean,
    }]
*/
  props: {
      dataList: {
          type: Array,
          default: []
      },
      checked:{
        type:Boolean,
        default:false
      },
      disabled:{
        type:Boolean,
        default:false
      }
  },
  data(){
    return{
    //   checked: false
     statusList: [],
     checkedList: []
    }
  },
  mounted() {
      this.statusList = JSON.parse(JSON.stringify(this.dataList))
  },
  methods:{
    clearCheckedList() {
        console.log('llllll')
        this.checkedList = []
        this.statusList.forEach(ele => {
            ele.checked = false
            document.querySelector(`.input_check_${ele.status}`).checked = false
        })
    },
    clickCheckbox(item) {
        let i = this.checkedList.findIndex(ele => ele.status == item.status)
        if(i == -1) {
            item.checked = true
            this.checkedList.push(item)
            document.querySelector(`.input_check_${item.status}`).checked = true
        } else {
            item.checked = false
            this.checkedList.splice(i, 1)
            document.querySelector(`.input_check_${item.status}`).checked = false
        }
        
        this.$emit('select-type', item)
        this.$emit('filterType', this.checkedList)
    }
  },
}
</script>
<style lang="scss" scoped>

// $statusArr: 'passed','failed','error', 'unfinished', 'tbc','skip', 'running', 'ready';
// $color_passed: '#73B05E';
// $color_failed: '#CC516B';
// $color_error: '#F9AB00';
// $color_unfinished: '#959799';
// $color_tbc: '#5470c6';
// $color_skip: '#cbced4';
// $color_running: '#F9AB00';
// $color_ready: '#73B05E';
// $color: $color_passed, $color_failed, $color_error, $color_unfinished, $color_tbc, $color_skip, $color_running, $color_ready;

// document.getElementsByTagName("body")[0].style.setProperty("--test1", "#FFF9EE");

@mixin sty_bg_border($color){
    border: 1px solid $color;
    background-color: $color;
}
@mixin input_sty($color) {
    input[type=checkbox]:before{
        @include sty_bg_border($color)
    }
    input[type=checkbox]:checked:before{
        @include sty_bg_border($color)
    }
    // input[type=checkbox]:disabled:before{}
}

@mixin input_passed{
    @include input_sty(#73B05E)
}
@mixin input_failed{
    @include input_sty(#CC516B)
}
@mixin input_error{
    @include input_sty(#F9AB00)
}
@mixin input_unfinished{
    @include input_sty(#959799)
}
@mixin input_tbc{
    @include input_sty(#5470c6)
}
@mixin input_skip{
    @include input_sty(#cbced4)
}
@mixin input_running{
    @include input_sty(#F9AB00)
}
@mixin input_ready{
    @include input_sty(#73B05E)
}



@mixin input_default{
    input[type=checkbox] {
        width: 15px;
        height: 15px;
        -webkit-appearance: none;
        background-color: transparent;
        border: 0;
        outline: 0 !important;
        color: #d8d8d8;
        position: relative;
        cursor: pointer;
    } 
    input[type=checkbox]:before{
        content: "";
        display:block;
        width: 15px;
        height: 15px;
        box-sizing:border-box;  
        border-radius: 3px;
        position: absolute;
    }

    input[type=checkbox]:disabled:before{
        content: "";
        display:block;
        width: 15px;
        height: 15px;
        box-sizing:border-box;  
        border-radius: 3px;
        position: absolute;
    }
    input[type=checkbox]:checked:before{
        content: "";
        display:block;
        width: 15px;
        height: 15px;
        box-sizing:border-box;  
        border-radius: 3px;
        position: absolute;
    }
    input[type=checkbox]:checked:after{
        content: "";
        display:block;
        width: 6px;
        height: 9px;
        border-left: 2px solid #fff;
        border-top: 2px solid #fff;
        border-radius:  2px;
        box-sizing:border-box; 
        position: absolute;
        transform: rotate(-135deg) translate(-70%, 20%);
        }
}
.checkbox_div{
    display: flex;
    justify-content: flex-start;
    cursor: pointer;
}
@mixin checkbox_div_one{
    margin-right: 15px;
    display: flex;
    align-items: center;
    span{
        margin-left: 5px;
    }
}
@mixin checkbox_default_input{
        @include checkbox_div_one;
        @include input_default;
}


.checkbox_div_one_passed{
    @include checkbox_default_input;
    @include input_passed
}
.checkbox_div_one_failed{
    @include checkbox_default_input;
    @include input_failed
}
.checkbox_div_one_error{
    @include checkbox_default_input;
    @include input_error
}
.checkbox_div_one_tbc{
    @include checkbox_default_input;
    @include input_tbc
}
.checkbox_div_one_unfinished{
    @include checkbox_default_input;
    @include input_unfinished
}
.checkbox_div_one_skip{
    @include checkbox_default_input;
    @include input_skip
}
.checkbox_div_one_running{
    @include checkbox_default_input;
    @include input_running
}
.checkbox_div_one_ready{
    @include checkbox_default_input;
    @include input_ready
}

// @for $i from 1 through length($statusArr) {
//     .checkbox_div_one_#{nth($statusArr, $i)}{
//         @include checkbox_default_input;
//         @include input_sty(#{nth($color, $i)})
//     }
// }

</style>

 使用Sass 定义自定义复选框

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想在 Vue 小程序中使用 `checkbox-group`,并去掉 checkbox 的边框并自定义样式,可以使用以下的代码示例: ```html <template> <div class="checkbox-group"> <label v-for="(option, index) in options" :key="index" class="checkbox-wrapper"> <input type="checkbox" :value="option.value" v-model="selectedOptions" class="checkbox"> <span class="label">{{ option.label }}</span> </label> </div> </template> <style> .checkbox-group { display: flex; flex-direction: column; } .checkbox-wrapper { display: flex; align-items: center; margin-bottom: 10px; } .checkbox { position: absolute; opacity: 0; cursor: pointer; } .label { position: relative; padding-left: 30px; font-size: 14px; line-height: 1.5; color: #333; } .label:before { content: ""; position: absolute; left: 0; top: 0; width: 20px; height: 20px; border: 1px solid #ccc; box-sizing: border-box; border-radius: 2px; } .checkbox:checked + .label:before { background-color: #007aff; border-color: #007aff; } .checkbox:checked + .label:before:after { content: ""; position: absolute; left: 6px; top: 2px; width: 5px; height: 10px; border: solid white; border-width: 0 2px 2px 0; transform: rotate(45deg); } </style> <script> export default { data() { return { options: [ { value: 'option1', label: '选项1' }, { value: 'option2', label: '选项2' }, { value: 'option3', label: '选项3' }, ], selectedOptions: [], } }, } </script> ``` 在这个示例中,我们使用了 `checkbox-group` 组件来管理多个 checkbox 的选中状态。我们使用 CSS 样式自定义 checkbox 的外观,包括去掉边框、自定义背景颜色、自定义选中时的背景颜色等。在每个 checkbox 的 label 中,我们使用了 `before` 伪元素来绘制 checkbox 的外观。同时,我们使用了 Vue 的数据绑定功能来管理 checkbox 的选中状态。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值