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

CSS中的一下小技巧2之CSS3动画勾选运用

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

CSS中的一下小技巧2之CSS3动画勾选运用

使用CSS3实现动画勾选

  相信大家在项目中会经常遇到这种需求:勾选框。现在用CSS3来实现一个动画勾选,只需要一个标签即可完成:

  这次需要用到CSS中伪类 after,这个小技巧也是很容易忘记的,所以决定记录起来~

  首先给标签加宽高加背景色:


    .check{
        width: 40px;
        height: 40px;
        background: palevioletred;
        position: relative;
        margin: 50px auto;
        border-radius: 5px;
        cursor: pointer;
    }

  

  接下来利用伪类给标签添加元素,同时水平垂直居中:

  


    .check{
        width: 40px;
        height: 40px;
        background: palevioletred;
        position: relative;
        margin: 50px auto;
        border-radius: 5px;
        cursor: pointer;
    }
    .check:after{
        content: '';
        display: block;
        width: 14px;
        height: 10px;
        border: 3px solid #fff;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -5px;
        margin-left: -7px;
    }

 

 

  变成这样:

  

  接下来去掉上边框跟右边框,同时将剩下的旋转45°稍微调整上下左右的距离即可~

  


    .check{
        width: 40px;
        height: 40px;
        background: palevioletred;
        position: relative;
        margin: 50px auto;
        border-radius: 5px;
        cursor: pointer;
    }
    .check:after{
        content: '';
        display: block;
        width: 14px;
        height: 10px;
        border: 3px solid #fff;
        border-width: 0 0 3px 3px;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -8px;
        margin-left: -8px;
        transform: rotate(-45deg);
    }

  

  最终效果就出来啦~

  我们还可以添加点击事件,一开始不设置颜色跟伪类,点击后添加一个class,给这个class添加伪类以及动画效果:

  


    .check{
        width: 40px;
        height: 40px;
        position: relative;
        margin: 50px auto;
        border: 1px solid #ddd;
        border-radius: 5px;
        cursor: pointer;
        transition: background-color 0.25s;
    }
    .checkActive{
        background: palevioletred;
        border-color: palevioletred;
    }
    .checkActive:after{
        content: '';
        display: block;
        width: 14px;
        height: 10px;
        border: 3px solid #fff;
        border-width: 0 0 3px 3px;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -8px;
        margin-left: -8px;
        transform: rotate(-45deg);
    }

  这样就完成啦!


作者:市民朱先生

原文链接:https://www.cnblogs.com/zhujunye/p/10451539.html


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

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

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