• 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';
    }
  • 相关阅读:
    Python安装的库列表导出到文件和批量安装库文件
    Selenium之浏览器驱动下载和配置使用
    测试面试计算题--python
    软件质量模型
    用例要素和设计方法
    python的层级
    day 14:深浅copy,数据结构 ,函数,set集合,变量作用域、返回值
    day 8:open文件和with的使用
    day 1:计算机发展史和组成部分
    day 2:计算机的基础知识,编程语言分类
  • 原文地址:https://www.cnblogs.com/wang715100018066/p/7064918.html
Copyright © 2020-2023  润新知