自定义单选按钮(radio)和自定义复选框(checkbox)

Write By Monkeyfly

以下内容均为原创,如需转载请注明出处。

前提

在项目中经常会遇到样式美观的radio或者checkbox,经过我的观察,发现有两种实现方法:

  • 利用点击时切换不同的图片代替radio或者checkbox,来实现选中或者取消时的样式变换效果。
  • 利用label标签的特性以及i元素搭配伪元素(:before或者:after)进行模拟。

下面主要说第二种实现方式:

实现原理

    <!-- 结构:一个 label 标签,其中包含 input 元素和 i 元素 -->
    <label>
        <input type="radio"><i><i></label>
    <!-- 基本原理: -->
    <!-- 1.首先将 input 元素 “隐藏” 起来;-->
    (1)可以使用  visibility: hidden; opacity: 0;来实现
    (2)也可以使用 display:none;来实现
    (3)或者使用 position:absolute; left:-9999px;来实现,都是可以的
    <!-- 2.然后利用 label 标签的特性,在点击时将 input 元素选中或取消选中。 -->
    <!-- 3.i 元素结合伪类(:before或:after)模拟单选框 radio 和多选框 checkbox 的外观。 -->

具体操作

一、美化单选按钮(radio)

1.具体代码如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css3美化单选按钮</title>
    <style>
        /*隐藏input*/
        label.bui-radios-label input[type="radio"]{
            position: absolute;
            opacity: 0;
            visibility: hidden;
        }
        /*自定义radio*/
        label.bui-radios-label .bui-radios{
            display: inline-block;
            position: relative;
            width: 13px;
            height: 13px;
            background-color: #fff;
            border: 1px solid #979797;
            border-radius: 50%;
            vertical-align: -2px;
            margin-right: 5px;
        }
        /*单选框选中后,自定义radio的样式*/
        label.bui-radios-label input[type="radio"]:checked + .bui-radios:after{
            position: absolute;
            content: "";
            width: 7px;
            height: 7px;
            background-color: #fff;
            border-radius: 50%;
            top: 3px;
            left: 3px;
        }
        label.bui-radios-label input[type="radio"]:checked + .bui-radios{
            background-color: #00B066;
            border:1px solid #00B066;
        }
        label.bui-radios-label input[type="radio"]:disabled + .bui-radios{
            background-color: #e8e8e8;
            border:1px solid #979797;
        }
        label.bui-radios-label input[type="radio"]:disabled:checked + .bui-radios:after{
            background-color: #c1c1c1;
        }
        /*自定义radio的过渡效果*/
        label.bui-radios-label.bui-radios-anim .bui-radios{
            -webkit-transition:background-color ease-out .5s;
            transition:background-color ease-out .5s;
        }
    </style>
</head>
<body>
    <h4>有简单的背景动画:</h4>
    <label class="bui-radios-label bui-radios-anim">
        <input type="radio" name="sex"><i class="bui-radios"></i></label>
    <label class="bui-radios-label bui-radios-anim">
        <input type="radio" name="sex"><i class="bui-radios"></i></label>
    <h4>无背景动画:</h4>
    <label class="bui-radios-label">
        <input type="radio" name="sex"><i class="bui-radios"></i></label>
    <label class="bui-radios-label">
        <input type="radio" name="sex"><i class="bui-radios"></i></label>
    <h4>禁用状态(disabled):</h4>
    <label class="bui-radios-label">
        <input type="radio" name="sex" disabled="disabled"><i class="bui-radios"></i></label>
    <label class="bui-radios-label">
        <input type="radio" name="sex" disabled="" checked=""><i class="bui-radios"></i></label>
</body>
</html>

2.效果展示

这里写图片描述

二、美化多选框(checkbox)

1.具体代码如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css3美化checkbox</title>
    <style>
        label.bui-checkbox-label input[type="checkbox"]{
            visibility: hidden;
            opacity: 0;
        }
        label.bui-checkbox-label .bui-checkbox{
            display: inline-block;
            position: relative;
            width: 13px;
            height: 13px;
            border: 1px solid #ddd;
            background-color: #fff;
            vertical-align: -2px;
            margin-right: 5px;
        }
        label.bui-checkbox-label input[type="checkbox"]:checked + .bui-checkbox:after{
            position: absolute;
            content: "";
            width:8px;
            height: 4px;
            border-left:2px solid #fff;
            border-bottom: 2px solid #fff;
            transform: rotate(-45deg) translate(-50%,-50%);
            /*top:50%;*/
            left:50%;
        }
        label.bui-checkbox-label input[type="checkbox"]:checked + .bui-checkbox {
            background-color: #00B066;
        }
        label.bui-checkbox-label input[type="checkbox"]:disabled + .bui-checkbox{
            background-color: #e8e8e8;
            border:1px solid #979797;
        }
        label.bui-checkbox-label input[type="checkbox"]:disabled:checked + .bui-checkbox:after{
            border-color: #000;
        }
        label.bui-checkbox-label.bui-checkbox-anim .bui-checkbox{
            -webkit-transition:background-color ease-in-out .3s;
            transition:background-color ease-in-out .3s;
        }
    </style>
</head>
<body>
    <h4>有简单的背景动画:</h4>
    <label class="bui-checkbox-label bui-checkbox-anim">
        <input type="checkbox" name="sex"><i class="bui-checkbox"></i></label>
    <label class="bui-checkbox-label bui-checkbox-anim">
        <input type="checkbox" name="sex"><i class="bui-checkbox"></i></label>
    <h4>无背景动画:</h4>
    <label class="bui-checkbox-label">
        <input type="checkbox" name="sex"><i class="bui-checkbox"></i></label>
    <label class="bui-checkbox-label">
        <input type="checkbox" name="sex"><i class="bui-checkbox"></i></label>
    <h4>禁用状态(disabled):</h4>
    <label class="bui-checkbox-label">
        <input type="checkbox" name="sex" disabled="disabled"><i class="bui-checkbox"></i></label>
    <label class="bui-checkbox-label">
        <input type="checkbox" name="sex" disabled="" checked=""><i class="bui-checkbox"></i></label>
</body>
</html>

2.效果展示
这里写图片描述

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用单选按钮复选框时需要注意以下几点: 1. 为每个单选按钮复选框指定唯一的`name`属性,以便在提交表单时可以正确地识别用户的选择。 2. 对于单选按钮,必须同时指定每个按钮的`value`属性和`checked`属性,以便指定默认选中的按钮和每个按钮所对应的值。 3. 对于复选框,可以使用`checked`属性指定默认选中的选项。 4. 使用`<label>`标签为每个单选按钮复选框提供描述文本,以便用户明确选项含义。 5. 对于复选框,可以在`<label>`标签内嵌套`<input>`标签来实现点击 label 标签也可以选中对应的复选框,需要设置`for`属性和`id`属性保持一致。 例如,下面是一个单选按钮复选框的示例: ```html <form> <p>请选择你喜欢的颜色:</p> <input type="radio" id="red" name="color" value="red" checked /> <label for="red">红色</label> <input type="radio" id="green" name="color" value="green" /> <label for="green">绿色</label> <input type="radio" id="blue" name="color" value="blue" /> <label for="blue">蓝色</label> <p>请选择你喜欢的水果:</p> <input type="checkbox" id="apple" name="fruit" value="apple" checked /> <label for="apple">苹果</label> <input type="checkbox" id="orange" name="fruit" value="orange" /> <label for="orange">橙子</label> <input type="checkbox" id="banana" name="fruit" value="banana" /> <label for="banana">香蕉</label> </form> ``` 在这个示例中,我们使用了单选按钮复选框来让用户选择颜色和水果。其中,每个单选按钮复选框都有一个唯一的`name`属性,以便在提交表单时可以正确地识别用户的选择。对于单选按钮,我们还指定了每个按钮的`value`属性和`checked`属性,以便指定默认选中的按钮和每个按钮所对应的值。同时,我们还使用了`<label>`标签为每个单选按钮复选框提供描述文本,让用户明确选项含义。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值