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循环应该 ...
随机推荐
- Vue.config.silent = true
Vue.config vue的全局配置文件 silent默认值是false Vue.config.silent = true 取消Vue所有的日志和警告
- 设置cookie,获取cookie
封装cookie获取方法一 function getCookie(key) { var key = encodeURIComponent(key); var result; var pairs = d ...
- 修改默认runlevel
CentOS直接修改文件 /etc/inittab 就好了 # Default runlevel. The runlevels used are: # - halt (Do NOT set init ...
- 安装gmpy2
本来只想做个RSA的题,结果环境就搭了好久 首先想用 pip install gmpy2 发现缺东西,要安装gmp,mpfr,mpc 安装: mkdir -p $HOME/src mkdir -p $ ...
- 十、hibernate的延迟加载和抓取策略
延迟加载:控制sql语句发送时机 抓取策略:控制sql语句格式,子查询.连接查询.普通sql 延迟加载 延迟加载(lazy),也叫做懒加载:执行到该行代码时,不发送sql进行查询,只有在真正使用到这个 ...
- java 高性能Server —— Reactor模型单线程版
NIO模型 NIO模型示例如下: Acceptor注册Selector,监听accept事件 当客户端连接后,触发accept事件 服务器构建对应的Channel,并在其上注册Selector,监听读 ...
- unittest接口自动化测试报告
unittest接口自动化测试报告 展示: 代码: __author__ = "Wai Yip Tung, Findyou" __version__ = "0.8.2.1 ...
- cnblogs添加打赏
上图上真相 1.进入后台设置---文件 2.上传你的支付宝和微信收款码(注意图片格式为bmp格式) 2.还是上图的位置,选择设置选项,找到博客侧边栏公告(支持HTML代码)(支持JS代码) 3.将如下 ...
- python print 连续输出变量加字符串
a=1 b=2 print(a,'+',b,'=',a+b) 输出:1+2=3
- Spring 讲解(四)
Spring 中使用注解注入 注解:就是一个类,使用 @ 注解名称. 实际开发中:使用注解取代 xml 配置文件. 1.常用注解释义 @component 取代 <bean class=&quo ...