栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > Html/CSS > CSS教程

通过css3实现checkbox选择样式

CSS教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过css3实现checkbox选择样式

前端开发过程中,由于不同浏览器间的不兼容性,相同的标签在不同的浏览器中显示不同的效果。尤其在移动端开发中,安卓与iOS表单元素显示各不相共,所以在前端页面开发过程中,需要统一相关样式,前端er需要自己开发相应组件去实现对应标签功能。
知识点: css3中before与after选择器,兄弟选择器,checked选择器
本示例通过css3实现浏览器下checkbox选中与未选中效果。
实现效果示例图片描述

1、html结构

    
    

结构比较简单,通过一个div,包含一个ckeckbox选择框,一个label

2、给这三个标签添加样式

.checkbox {
    position: relative;
    height: 30px;
}

checkbox选择框设置opacity为0,及用户不能直接感受到它的存在

.checkbox input[type='checkbox'] {
    position: absolute;
    left: 0;
    top: 0;
    width: 20px;
    height: 20px;
    opacity: 0;
}
.checkbox label {
    position: absolute;
    left: 30px;
    top: 0;
    height: 20px;
    line-height: 20px;
}

通过label的before与after实现圆点未选中效果,设置before为圆圈,after实现选中后对勾的效果,设置after为长方形,顺时针旋转45deg,设置右边框与下边框,色值为白色。

.checkbox label:before {
    content: '';
    position: absolute;
    left: -30px;
    top: 0;
    width: 20px;
    height: 20px;
    border: 1px solid #ddd;
    border-radius: 50%;
    transition: all 0.3s ease;
    -webkit-transition: all 0.3s ease;
    -moz-transition: all 0.3s ease;
}

.checkbox label:after {
    content: '';
    position: absolute;
    left: -22px;
    top: 3px;
    width: 6px;
    height: 12px;
    border: 0;
    border-right: 1px solid #fff;
    border-bottom: 1px solid #fff;
    background: #fff;
    transform: rotate(45deg);
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transition: all 0.3s ease;
    -webkit-transition: all 0.3s ease;
    -moz-transition: all 0.3s ease;
}

添加input选中时的效果,设置before与after的背景色相同,由于after元素有背景色为白色的边框,即可实现相关效果

.checkbox input[type='checkbox']:checked + label:before {
    background: #4cd764;
    border-color: #4cd764;
}
.checkbox input[type='checkbox']:checked + label:after {
    background: #4cd764;
}

完整实例




    
    
    Examples
    
    
    
    



.checkbox {
    position: relative;
    height: 30px;
}
.checkbox input[type='checkbox'] {
    position: absolute;
    left: 0;
    top: 0;
    width: 20px;
    height: 20px;
    opacity: 0;
}
.checkbox label {
    position: absolute;
    left: 30px;
    top: 0;
    height: 20px;
    line-height: 20px;
}
.checkbox label:before {
    content: '';
    position: absolute;
    left: -30px;
    top: 0;
    width: 20px;
    height: 20px;
    border: 1px solid #ddd;
    border-radius: 50%;
    transition: all 0.3s ease;
    -webkit-transition: all 0.3s ease;
    -moz-transition: all 0.3s ease;
}

.checkbox label:after {
    content: '';
    position: absolute;
    left: -22px;
    top: 3px;
    width: 6px;
    height: 12px;
    border: 0;
    border-right: 1px solid #fff;
    border-bottom: 1px solid #fff;
    background: #fff;
    transform: rotate(45deg);
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transition: all 0.3s ease;
    -webkit-transition: all 0.3s ease;
    -moz-transition: all 0.3s ease;
}

.checkbox input[type='checkbox']:checked + label:before {
    background: #4cd764;
    border-color: #4cd764;
}
.checkbox input[type='checkbox']:checked + label:after {
    background: #4cd764;
}



    
    


    
    


    
    



作者: 朽梨
链接:https://www.imooc.com/article/15815
来源:慕课网

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/243049.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号