vue中checkbox 样式自定义重写;循环遍历checkbox,拿到不同的v-model绑定值;及获取当前checked 状态,全选和全不选等功能。
开始写这个功能,不得不吐槽原始的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绑定值一定要是数组
{{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 状态,全选和全不选等功能。的更多相关文章
- Vue学习笔记七:Vue中的样式
目录 两种样式 class样式 内联样式 两种样式 Vue中使用样式方式有两种,一种是class样式,一种是内联样式也就是style class样式 class样式使用的方式有5种,HTML如下 &l ...
- 3-5 Vue中的样式绑定
Vue中的样式绑定: 本案例,简单设计一个<div>的点击绑定事件来改变div的样式效果 方法一:[class] ①(class和对象的绑定) //如上,运用class和一个对象的形式来解 ...
- VUE中CSS样式穿透
VUE中CSS样式穿透 1. 问题由来 在做两款H5的APP项目,前期采用微信官方推荐的weui组件库.后来因呈现的效果不理想,组件不丰富,最终项目完成后全部升级采用了有赞开发的vant组件库.同时将 ...
- vue.js(7)--vue中的样式绑定
vue中class样式与内联样式的绑定 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- 深入理解 vue 中 scoped 样式作用域的规则
哈喽!大家好!我是木瓜太香,今天我们来聊一个 vue 的样式作用域的问题,通常我们开发项目的时候是要在 style 上加上 scoped 来起到规定组件作用域的效果的,所以了解他们的规则也是很有必要的 ...
- 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 ...
- vue组件,vue补充和总结,JS循环遍历和加减运算、类型转换补充
目录 一.vue中的组件 1. 组件的概念 2. 组件分类 3. 组件的特点 4. 组件的定义 5. 组件化 (1)用法和注意 (2)数据组件化实例 6. 组件传参--父传子 (1)用法和注意 (2) ...
- vue中动态样式不起作用? scoped了解一下
vue中style标签使用属性scoped的注意事项 style上添加属性scoped可以实现样式私有化,但是在使用动态样式时,样式会不起作用.可以先去掉scoped
- javascript中常见的几种循环遍历
项目开发中,不管是建立在哪个框架基础上,对数据的处理都是必须的,而处理数据离不开各种遍历循环.javascript中循环遍历有很多种方式,记录下几种常见的js循环遍历. 一.for循环 for循环应该 ...
随机推荐
- C++中的面向对象(一)
1,本节课开始进入 C++ 中的面向对象,面向对象是 C++ 中最核心也是体现 C++ 价 值的一个部分: 2,日常生活当中我们都习惯对事物进行分类,那么这种分类的思想是否可以引入到 程序设计中? ...
- Python3下安装Scrapy
在windows下安装Scrapy的错误挺多的, 我将我安装成功的步骤发出来,供更多的人参考. 首先,直接进入Scrapy网站的文档Installation guide下的 Installing Sc ...
- 44.Linked List Cycle II(环的入口节点)
Level: Medium 题目描述: Given a linked list, return the node where the cycle begins. If there is no cy ...
- Hadoop伪分布式环境安装
一.环境准备 阿里云ECS(Centos7).已预装JDK8 Hadoop安装包 hadoop-2.7.7.tar.gz 二. 安装步骤 1.确认JDK环境的安装位置 命令 echo $JAVA_HO ...
- USB hub串口绑定
方式一 1.查看串口信息 udevadm info /dev/ttyUSB0 2.创建配置文件 sudo vi /etc/udev/rules.d/com_port.rules ACTION==&qu ...
- Linux安装篇超详细
在此篇文章中主要介绍Linux系统的安装,以及学习大数据过程中Linux中常用的命令有哪些. 一.Linux(CentOs6.8)的安装 1.安装VMware虚拟机 虚拟机下载地址:https://p ...
- 16.ThreadGroup线程组
public class ThreadGroupDemo implements Runnable{ public static void main(String[] args){ //建立一个名为&q ...
- 调用opencv的接口实现人脸检测(简单)
import cv2 import matplotlib.pyplot as plt %matplotlib inline # 提取预训练的人脸检测模型,提前下载好的模型 face_cascade = ...
- 信号量的使用 ManualResetEvent
线程的等待时可以用这个,不论是线程池还是线程都可以用这个做等待. ManualResetEvent md=new ManualResetEvent(false);//这个false 设置线程等待, t ...
- Window01
1 <script src="~/jquery-easyui-1.5.5.2/jquery.min.js"></script> <link href= ...