CSS防IOS开关

这里写图片描述这里写图片描述

原理:
点击时,分为3步(同时进行)
1:CheckBox的背景颜色变为原谅色
2:CheckBox:after(小圆点)右移
3:CheckBox:before(小圆点旁边的白条)缩放(缩小为原来的0倍)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
        .al-toggle-button{/*默认背景为灰色*/
            appearance: none;/*去掉复选框的默认样式,将背景显示出来*/
            -webkit-appearance: none;
            position: relative;
            width: 52px;/*设置checkbox的宽高*/
            height: 32px;
            background: #dfdfdf;
            border-radius: 16px;
            border: 1px solid #dfdfdf;
            outline: 0;
            box-sizing: border-box;
        }
        .al-toggle-button:checked{/*选中时:背景变为绿色*/
            border-color: #04be02;
            background-color: #04be02;
        }
        .al-toggle-button:before, .al-toggle-button:after{/*设置0.3秒内,变换完毕*/
            content: " ";
            position: absolute;
            top: 0;
            left: 0;
            height: 30px;
            border-radius: 15px;
            transition: transform 0.3s;
            transition: -webkit-transform 0.3s;
            transition: transform 0.3s, -webkit-transform 0.3s;
            -webkit-transition: -webkit-transform 0.3s;
        }
        .al-toggle-button:before{/*小圆点右侧的白色*/
            width: 50px;
            background-color: #fdfdfd;
        }
        .al-toggle-button:after{/*小圆点*/
            width: 30px;
            background-color: white;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
        }
        .al-toggle-button:checked:before{/*选中时:小圆点右侧的白色缩小为0*/
            transform: scale(0);
            -webkit-transform: scale(0);
        }
        .al-toggle-button:checked:after{/*选中时:小圆点右移*/
            transform: translateX(20px);
            -webkit-transform: translateX(20px);
        }

        </style>
    </head>
    <body>
        <input type="checkbox" class="al-toggle-button">
    </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值