开始写这个功能,不得不吐槽原始的checkbox,灰色小方块的丑陋,虽说eleUI,mintUI,等各种框架的单复选框已经对其优化,但还是不想要这种。那我们就来研究一下怎么处理它。

 <section class="box">
<label :for="item3" @click="chooseType($event,index3)" v-for="(item3,index3) in type" class="labelName">
<input type="checkbox" :value="item3" :id="item3" v-model="checkedValue" class="checkboxList"> // for属性一定要与id一致,原因请看上图
<div class="name">{{item3}}</div> // label的值 // checkbox的v-model绑定值一定要是数组
&nbsp;&nbsp;
{{checkedValue}} // 查看值
</label>
<button @click="chooseQu">全选</button>
<button @click="chooseNo">全不选</button> </section>

data:

data(){
return{
checkedValue: [],
type:['a','b','c','d'] // 测试数据,可在mounted钩子函数中将后台数据赋值
}
},

methods:

methods:{
chooseType(e,val){
console.log(e.target.checked) // 可获取当前的checked 状态
console.log(val) // 可获取到当前的下标。 },
chooseQu(){
// document.querySelectorAll('.checkboxList').setAttribute("checked","true");
this.checkedValue = this.type ; //将原数据全部赋值给checkedValue,全部置为true.
},
chooseNo(){
this.checkedValue = [] ; // checkedValue 为空即将checkedValue全部置为false,
} }

样式的调整:

// 样式可根据自己的需要,通过控制外层div自行配置,
.box{
/*display: flex;*/
/*justify-content: start;*/
/*align-items: center;*/
}
.labelName{
width: 25%;
display: flex;
justify-content: center;
margin-bottom: 20px;
}
/*------新-----*/
input[type=checkbox] {
width: 20px; // 可根据自己的需要进行配置input的大小。
height: 20px;
border-radius: 10px;
-webkit-appearance: none;
background-color: transparent;
border: 0;
outline: 0 !important;
color: #d8d8d8;
position: relative;
}
input[type=checkbox]:before{
content: "";
display:block;
width: 20px;
height: 20px;
border-radius: 10px;
border: 1px solid #ddd;
background-color: #fff;
box-sizing:border-box;
position: absolute;
} input[type=checkbox]:disabled:before{
content: "";
display:block;
width: 20px;
height: 20px;
border-radius: 10px;
border: 1px solid #333;
background-color: #333;
box-sizing:border-box; // 可控制调整背景色。
position: absolute;
}
input[type=checkbox]:checked:before{
content: "";
display:block;
width: 20px;
height: 20px;
border-radius: 10px;
border: 1px solid #D2A47E;
background-color: #D2A47E; //可控制checked背景色
box-sizing:border-box;
position: absolute;
}
input[type=checkbox]:checked:after{
content: "";
display:block;
/*width: .15rem;*/
/*height: .3rem;*/
/*border-radius: .06rem;*/
width: 7px; // 此处是控制获取checked=true 后的颜色,请注意宽高比约1:2 。 原理是通过伪类去控制样式。
height: 15px;
/*border-radius:3px;*/
border-left: .07rem solid #fff;
border-top: .07rem solid #fff;
box-sizing:border-box;
position: absolute;
transform: rotate(-135deg) translate(-70%, 25%);
}

vue中checkbox 样式自定义重写;循环遍历checkbox,拿到不同的v-model绑定值;及获取当前checked 状态,全选和全不选等功能。的更多相关文章

  1. Vue学习笔记七:Vue中的样式

    目录 两种样式 class样式 内联样式 两种样式 Vue中使用样式方式有两种,一种是class样式,一种是内联样式也就是style class样式 class样式使用的方式有5种,HTML如下 &l ...

  2. 3-5 Vue中的样式绑定

    Vue中的样式绑定: 本案例,简单设计一个<div>的点击绑定事件来改变div的样式效果 方法一:[class] ①(class和对象的绑定) //如上,运用class和一个对象的形式来解 ...

  3. VUE中CSS样式穿透

    VUE中CSS样式穿透 1. 问题由来 在做两款H5的APP项目,前期采用微信官方推荐的weui组件库.后来因呈现的效果不理想,组件不丰富,最终项目完成后全部升级采用了有赞开发的vant组件库.同时将 ...

  4. vue.js(7)--vue中的样式绑定

    vue中class样式与内联样式的绑定 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  5. 深入理解 vue 中 scoped 样式作用域的规则

    哈喽!大家好!我是木瓜太香,今天我们来聊一个 vue 的样式作用域的问题,通常我们开发项目的时候是要在 style 上加上 scoped 来起到规定组件作用域的效果的,所以了解他们的规则也是很有必要的 ...

  6. JavaScript 中的常用12种循环遍历(数组或对象)的方法

    1.for 循环 let arr = [1,2,3]; for (let i=0; i<arr.length; i++){ console.log(i,arr[i]) } // 0 1 // 1 ...

  7. vue组件,vue补充和总结,JS循环遍历和加减运算、类型转换补充

    目录 一.vue中的组件 1. 组件的概念 2. 组件分类 3. 组件的特点 4. 组件的定义 5. 组件化 (1)用法和注意 (2)数据组件化实例 6. 组件传参--父传子 (1)用法和注意 (2) ...

  8. vue中动态样式不起作用? scoped了解一下

    vue中style标签使用属性scoped的注意事项 style上添加属性scoped可以实现样式私有化,但是在使用动态样式时,样式会不起作用.可以先去掉scoped

  9. javascript中常见的几种循环遍历

    项目开发中,不管是建立在哪个框架基础上,对数据的处理都是必须的,而处理数据离不开各种遍历循环.javascript中循环遍历有很多种方式,记录下几种常见的js循环遍历. 一.for循环 for循环应该 ...

随机推荐

  1. element UI的使用

    npm install --save element-ui main.js里面添加 import ElementUI from 'element-ui' import 'element-ui/lib/ ...

  2. AJAX 步骤分析

    HTML 步骤: 1. 创建异步对象 2. 设置请求行 3. 设置请求头(get请求可以省略) 4. 注册状态改变事件 4.1. 判断状态&请求是否成功并使用数据   5. 发送请求   PH ...

  3. shell位置参数处理举例

  4. 命令分析nginx访问日志的用法

    awk分析日志常用高级使用命令方法 分析访问日志(Nginx为例) 日志格式: '$remote_addr - $remote_user [$time_local] "$request&qu ...

  5. Codeforces 354C 暴力 数论

    题意:给你一个数组,你可以把数组中的数减少最多k,问数组中的所有数的GCD最大是多少? 思路:容易发现,GCD的上限是数组中最小的那个数,而因为最多可以减少k,及可以凑出来的余数最大是k,那么GCD的 ...

  6. BZOJ 2238: Mst DFS序+KDtree

    明明可以用二维数点来做啊,网上为什么都是树剖+线段树呢 ? code: #include <cstdio> #include <cstring> #include <al ...

  7. maven 使用idea运行按钮install

    根据需要配置 例: clean install 离线 设置vm 跳过测试 -Xms1024m -Xmx1024m -XX:MaxPermSize=1024m 附(离线+跳过测试): mvn clean ...

  8. Python 序列类型拆包 %s 和'{}'.format 的功能差异之一

    >>> 1, 2, 3 #这样写成一行相当于一个元组(1, 2, 3)>>> x = 1, 2, 3>>> x(1, 2, 3)>>& ...

  9. PHP如何使用AES加密和解密

    AES加密在php5的版本中使用的mcrypt_decrypt 函数,该函数已经在php7.1后弃用了,取而代之的是openssl的openssl_encrypt和openssl_decrypt,并且 ...

  10. STM32的结构和启动模式

    一.STM32F10x功能模块 32位的Cortex-M3微处理器: 可嵌套的向量中断控制器(NVIC)和60个可屏蔽中断且有16个可编程优先级: 内嵌内存: FLASH:最大512K字节 STAM: ...