zoukankan      html  css  js  c++  java
  • 分享几个很实用的CSS技巧对前端技术很有帮助

    创建剪切动画

    对于剪切动画,使用clip-path代替width/height,避免DOM重排导致性能过低。

    .animate {
       200px;
      height: 200px;
      background: #000;
      animation: 1s clip;
    }
    @keyframes clip {
      0% {
          clip-path: inset(0 0 0 0);
      }
      100% {
          clip-path: inset(0 100% 100% 0);
      }
    }
    

      

    clip-path也能用来进行其他规则/不规则图形的剪切

    .clip {
      clip-path: polygon(0 100%, 50% 0, 100% 100%, 0 30%, 100% 30%); /* 多边形 */
      clip-path: circle(30px at 35px 35px); /* 圆形 */
      clip-path: ellipse(30px 25px at 35px 35px); /* 椭圆 */
    }
    

    资源网站大全 https://55wd.com 设计导航https://www.wode007.com/favorites/sjdh

    优化动画性能

    除了使用transform3d开启gpu加速,还可以使用will-change强制gpu加速优化动画性能

    .animate {
       200px;
      height: 200px;
      background: #000;
      animation: 1s clip;
      will-change: clip-path;
    }
    @keyframes clip {
      0% {
          clip-path: inset(0 0 0 0);
      }
      100% {
          clip-path: inset(0 100% 100% 0);
      }
    }
    

      

    实现长宽比

    使用padding模拟,然后子元素使用绝对定位

    /* 1:1 */
    .container {
       200px;
    }
    .container:after {
      display: block;
      content: ' ';
      padding-top: 100%;
    }
    
    /* 16:9 */
    .container {
       200px;
    }
    .container:after {
      display: block;
      content: ' ';
      padding-top: calc(100% * 9 / 16);
    }
    

      

    垂直居中

    我们常用的方式:

    • dislay: inline-block
    • top: 50% + transform: tranlsateY(-50%)
    • display: flex

    其余还有padding上下撑高、display: table、position + margin: auto、绝对定位 + margin等等,这些属于不常用、特殊场景才能用、css3之前的hack方式,css3之后就不必使用这些来实现垂直居中,就不多说了。

    其中display: flex属于万金油,大多数场景可以直接用它,但还是有些特殊的场景不能用:

    1. 子元素需要文字截断,为了兼容4.X的Android浏览器,必须使用其他方式(一般是transform)
    2. 子元素需要多行布局,4.x的Android不支持flex-wrap,不能多行布局
  • 相关阅读:
    HTTP协议
    Python学习--装饰器、列表生成式、生成器、map filter、json处理
    Python学习--多线程&多进程
    Python学习--发送邮件
    Python学习--异常处理
    【第五节】【Python学习】【configparser模块】
    【第一节】【shell脚本】【文件里的内容与变量中的内容大小写替换】
    【Python】【多线程多进程】
    【Selenium学习】【拖动滚动条】
    【Python】【异常的获取与处理】
  • 原文地址:https://www.cnblogs.com/ypppt/p/13237323.html
Copyright © 2011-2022 走看看