开始写这个功能,不得不吐槽原始的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. MySQL的共享锁与排它锁编码演示

    一.行锁之MySQL  使用SELECT ... FOR UPDATE 做事务写入前的确认 以MySQL 的InnoDB 为例,预设的Tansaction isolation level 为REPEA ...

  2. Mybatis一级缓存和二级缓存 Redis缓存

    一级缓存 Mybatis的一级缓存存放在SqlSession的生命周期,在同一个SqlSession中查询时,Mybatis会把执行的方法和参数通过算法生成缓存的键值,将键值和查询结果存入一个Map对 ...

  3. [HTML知识体系]meta标签的常见用法

    1.meta是什么 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. 标签位于文档的头部,不包含任何内容. 标签的属性定义了与文档相关联的名称 ...

  4. leetcode-166周赛-5280-用户分组

    题目描述: 自己的提交: class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: ...

  5. PHP排序函数:sort()、rsort()、asort()、arsort()、ksort()、krsort()

    sort()函数以升序对数组排序.rsort() 函数以降序对数组排序.asort() 函数对数组从低到高进行排序并保持索引关系.arsort() 函数对数组从高到低进行排序并保持索引关系.ksort ...

  6. POJ 2387 Til the Cows Come Home (dijkstra模板题)

    Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...

  7. POJ 3468 A Simple Problem with Integers(线段树区间修改及查询)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  8. vue基础九

    1.使用组件 1.1注册 要注册一个全局组件,你可以使用 Vue.component(tagName, options). 例如: Vue.component('my-component', { // ...

  9. java pair配对的概念

    今天在项目中遇到了Pair,之前没有使用过,百度了下,记录. 使用场景 当我们在写一个方法需要返回两个字段值时,我之前的方法是新建一个类或使用集合.目前来看使用Pair方便很多. 配对(Pair).配 ...

  10. Webx.0-Web2.0:Web2.0

    ylbtech-Webx.0-Web2.0:Web2.0 Web2.0 是相对于Web1.0 的新的时代.指的是一个利用Web的平台,由用户主导而生成的内容互联网产品模式,为了区别传统由网站雇员主导生 ...