博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从青铜到王者,10个css3伪类使用技巧和运用
阅读量:2430 次
发布时间:2019-05-10

本文共 5662 字,大约阅读时间需要 18 分钟。

伪类经常与伪元素混淆,伪元素的效果类似于通过添加一个实际的元素才能达到,而伪类的效果类似于通过添加一个实际的类来达到。实际上css3为了区分两者,已经明确规定了伪类用一个冒号来表示,而伪元素则用两个冒号来表示。伪类与伪元素的本质区别就是是否抽象创造了新元素。具体的伪类和伪元素相关知识本文就不深入,下面介绍一下从青铜到王者10个css3伪类使用技巧和运用。

青铜-1、伪类实现盒子阴影

众所周知,Animate/transition box-shadow 可以使用 box-shadow属性 来实现盒子阴影效果,但repaint消耗较多,于是这里提出 通过修改伪元素的透明度来实现盒子阴影

实现原理:

**通过改变透明度,这样从一个非默认值更新它的值,就不需要承担任何重绘

这里设置一个空的伪元素设置阴影透明度为0隐藏,再通过鼠标悬停恢复它的透明度,下面是传统和伪类实现的代码对比

    

Before

    

Animate/transition box-shadow 可以使用box-shadow属性来实现盒子阴影效果,但重绘消耗较多

 
    

After

    

通过修改伪元素的透明度来实现同样的效果,没有重绘消耗

.before {    padding: 1em;    background-color: #fff;    -webkit-transition: 0.2s;    transition: 0.2s;}.before:hover {    box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.3);}.after {    position: relative;    padding: 1em;    background-color: #fff;}.after:before {    content: "";    position: absolute;      top: 0;    right: 0;    bottom: 0;      left: 0;    z-index: -1;    box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.3);    opacity: 0;    will-change: opacity;    -webkit-transition: 0.2s;    transition: 0.2s;}.after:hover:before {    opacity: 1;}
1240

青铜-2、伪元素:before实现的面包屑导航栏

    
  • Home    
  •     
  • Pictures    
  •     
  • Summer 15    
  •     
  • Italy
  • ul.breadcrumb {    padding: 8px 16px;    list-style: none;    background-color: #eee;}ul.breadcrumb li {    display: inline;}ul.breadcrumb li+li:before {    padding: 8px;    color: black;    content: "/\00a0";}ul.breadcrumb li a {    color: green;}

    效果:

    1240

    青铜-3、伪元素实现悬停时按钮填充和边界浮动动画

    1240

    青铜-4、伪类after实现的三角箭头

    实现原理:三边设置边框,箭头指向的那个方向的border不用设置,位于箭头两边的边框颜色为透明(transparent),对边为主体边框颜色(较大的)/主体背景颜色(较小的),因为我们要有边框颜色的三角箭头,当第一个箭头(较大的)被第二个箭头(较小的)通过准确覆盖之后剩下没被覆盖的边缘就是合成三角箭头的边框了,其颜色就是较大的那个三角箭头的颜色,可调。而较小的那个三角箭头的颜色要设置成主体颜色,进行负值定位偏移时要把主体边框盖住,从而与主体合在一起了

        
        
        
        
    .arrow-left:before {    z-index: 9999;    content: "";    display: block;    width: 0;    height: 0;    border-top: 20px solid transparent;    border-bottom: 20px solid transparent;    border-right: 20px solid #E9E9E9;    position: absolute;    left: -20px;    top: 80px;}
    1240

    青铜-5、伪类after实现的图片箭头

    1240

    青铜-6、伪元素实现带角度的底部边界(倾斜的边界)

    原理:修改webkit-transform: skewY属性来修改倾斜度(旋转也是一样的道理)

    .edge--bottom {    position: relative;    z-index: 1;}.edge--bottom:after {    background: inherit;    content: '';    display: block;    height: 50%;    left: 0;    position: absolute;    right: 0;    z-index: -1;}.edge--bottom:after {    bottom: 0;    -webkit-transform: skewY(-1.5deg);    -ms-transform: skewY(-1.5deg);    transform: skewY(-1.5deg);    -webkit-transform-origin: 100%;    -ms-transform-origin: 100%;    transform-origin: 100%;}
    1240

    王者-1、伪元素和平移(translate)变换实现的提示框

     
            
                
    TOOLTIP TOP            
    Lorem ipsum dolor sit amet            
    .tooltip .tooltip-content::after {    background: #05a8ff;    content: "";    height: 10px;    position: absolute;    -webkit-transform: rotate(45deg);    transform: rotate(45deg);    width: 10px;}.tooltip.top .tooltip-content {    bottom: calc(100% + 1.5em);    left: 50%;    -webkit-transform: translateX(-50%);    transform: translateX(-50%);}.tooltip.top .tooltip-content::after {    bottom: -5px;    left: 50%;    margin-left: -5px;}
    1240

    王者-2、使用CSS3伪元素实现的自动打字动画

    原理:Typing Animation with Pseudo-Elements 看起来是打字,其实是使用伪元素覆盖在字符串上,然后逐渐减少伪元素覆盖宽度来实现的视觉效果

        

    Typing Animation

        
            
    webdesign    

    .tagline-skill_inner:after {    content: "";    position: absolute;    top: -1px;    right: 0;    bottom: -2px;    left: 0;    border-left: 1px solid #fff;    background-color: #2a2a28;    -webkit-animation: animatetoright 1s steps(10) infinite alternate;    animation: animatetoright 1s steps(10) infinite alternate;}
    1240

    王者-3、CSS3 伪元素构建的文章水印背景

    h1 {    position: relative;    margin: 0;    font-weight: bold;    letter-spacing: -0.05rem;    line-height: 1;    text-transform: uppercase;    z-index: 10;}h1:before {    content: "2018/08";    font-family: monospace;    font-size: 10rem;    position: absolute;    top: 2rem;    left: -2rem;    z-index: 0;    line-height: 1;    color: rgba(50, 25, 0, 0.1);}
    1240

    王者-4、CSS3 用伪元素做页码摘要

    a {    display: -webkit-box;    display: -webkit-flex;    display: -ms-flexbox;    display: flex;    -webkit-flex-flow: row nowrap;    -ms-flex-flow: row nowrap;    flex-flow: row nowrap;    -webkit-box-align: baseline;    -webkit-align-items: baseline;    -ms-flex-align: baseline;    align-items: baseline;    text-decoration: none;    -webkit-transition: color .2s ease-in-out;    transition: color .2s ease-in-out;}a::before {    height: .1em;    -webkit-box-flex: 1;    -webkit-flex: 1 1 auto;    -ms-flex: 1 1 auto;    flex: 1 1 auto;    -webkit-box-ordinal-group: 2;    -webkit-order: 1;    -ms-flex-order: 1;    order: 1;    background: left bottom/contain repeat-x url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA3IDIiPjxjaXJjbGUgZmlsbD0iI2ZmZiIgY3g9IjMuNSIgY3k9IjEiIHI9IjEiLz48L3N2Zz4=);    content: '';}a::after {    -webkit-box-ordinal-group: 3;    -webkit-order: 2;    -ms-flex-order: 2;    order: 2;    content: "p." attr(data-page);}

    王者-5、伪类兼容性了解一下

    1、IE8不支持CSS3中很多特性,比如伪元素nth-child,可以使用+号(代表相邻元素)来实现相同功能

    2、Google的IE9.js是解决IE5.5到IE8 CSS3特性兼容性问题的JS库

    最后

    CSS的世界很美好,每个知识点都可以值得深入研究和实践,对于伪类、伪元素也有很多土味特效可以写出来,比如说图片遮罩、图片背景模糊,更多高级的鼠标经过事件特效等等

    来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/69901074/viewspace-2641718/,如需转载,请注明出处,否则将追究法律责任。

    转载于:http://blog.itpub.net/69901074/viewspace-2641718/

    你可能感兴趣的文章
    java学习6:方法
    查看>>
    python-命名空间和作用域
    查看>>
    支付宝签名与验签,return_url和通知页notify_url
    查看>>
    JAVA三个默认类加载器及相互关系
    查看>>
    JAVA 实例对象创建后的初始化过程
    查看>>
    HashMap使用及完整测试用例
    查看>>
    OBJDUMP用法
    查看>>
    c/cplusplus通用makefile
    查看>>
    JavaScript-密码强度
    查看>>
    【SSH】1366-InCorrect string value:'\xE9\x99\x88\xE6\x96\xB0...'for column 'name' at row 1
    查看>>
    SpringCloud前身之微服务
    查看>>
    纵览全局——SSH
    查看>>
    纵览全局——Mybatis
    查看>>
    PC端-中文转拼音后续问题
    查看>>
    第七章-面向对象技术
    查看>>
    Mybatis-略识之无
    查看>>
    ionic 前端 - 汉字转拼音
    查看>>
    Ionic-与时间有关的故事-localecompare()
    查看>>
    ionic -- 实现根据拼音字母搜索人员
    查看>>
    Jenkins部署Git项目
    查看>>