zoukankan      html  css  js  c++  java
  • will-change

    目的:

     让GPU分担CPU的工作,从而优化和分配内存,告知浏览器做好动画的准备。

    背景:

    注意事项:

    1,will-change虽然可以加速,但是,一定一定要适度使用;

    2,使用伪元素,独立渲染:

      不要这样直接写在默认状态中,因为will-change会一直挂着: 

    .a {
      will-change: transform;
      transition: transform 0.3s;
    }
    .a:hover {
      transform: scale(1.5);
    }

      解决方案:

      可以让父元素hover的时候,声明will-change,这样,移出的时候就会自动remove,触发的范围基本上是有效元素范围。

    .a-parent:hover .a {
      will-change: transform;
    }
    .a {
      transition: transform 0.3s;
    }
    .a:hover {
      transform: scale(1.5);
    }

    3,如果使用JS添加will-change, 事件或动画完毕,一定要及时remove. 比方说点击某个按钮,其他某个元素进行动画。点击按钮(click),要先按下(mousedown)再抬起才出发。因此,可以mousedown时候打声招呼, 动画结束自带回调,于是:

    dom.onmousedown = function() {
        target.style.willChange = 'transform';
    };
    dom.onclick = function() {
        // target动画哔哩哔哩...
    };
    target.onanimationend = function() {
        // 动画结束回调,移除will-change
        this.style.willChange = 'auto';
    };

     官方:https://developer.mozilla.org/en/docs/Web/CSS/will-change ,给出的示例:

    var el = document.getElementById('element');
    
    // Set will-change when the element is hovered
    el.addEventListener('mouseenter', hintBrowser);
    el.addEventListener('animationEnd', removeHint);
    
    function hintBrowser() {
      // The optimizable properties that are going to change
      // in the animation's keyframes block
      this.style.willChange = 'transform, opacity';
    }
    
    function removeHint() {
      this.style.willChange = 'auto';
    }
  • 相关阅读:
    springmvc入门&参数&注解
    springmvc_ssm Spring、SpringMVC、MyBatis集成
    spring-dbutils&注解
    如何快速进去 注册表
    数据库的导出 与导入 速度相当快的方法
    常见的问题:https://localhost:1158/em 无法打开
    卸载windows服务
    用语句创建 表空间、临时表空间、用户 等规则
    游标 根据目录号 操作用户 查看 对应得影像数
    根据 目录号 案卷号 用户名 查询 page 中 的条数
  • 原文地址:https://www.cnblogs.com/wang715100018066/p/7064918.html
Copyright © 2011-2022 走看看