代码之家  ›  专栏  ›  技术社区  ›  SmallTown NE

为什么带转换的转换会导致整个页面重新绘制?

  •  5
  • SmallTown NE  · 技术社区  · 8 年前

    首先,打开chrome devtools,触发器

    然后将鼠标悬停在第一个div上,您会发现如果删除 transition 属性,只重新绘制第一个div,但如果添加 过渡 属性 整体 页面将被重新绘制,是否有人可以解释这一点?

    div {   
      width: 100px;   
      height: 100px;
    }
    
    div:first-child {   
      margin-bottom: 100px; 
    }
    
    div:first-child:hover {   
      transform: scale(1.4);   
      transition: all .3s;   
      background-color: red; 
    }
    <div> 11111111111111111 </div>
    <div> 222222222222222222 </div>

    JSFIDLE演示: https://jsfiddle.net/heaven_xz/4q2kgr2g/5/

    2 回复  |  直到 8 年前
        1
  •  7
  •   vals    8 年前

    要提高性能,您应该做两件事。

    第一个是只声明转换到您感兴趣更改的属性。

    但重新粉刷问题的根源在于,Chrome现在使用的新样式将会改变。如果您充分声明,那么现在重新绘制将是您所期望的。

    关于为什么Chrome团队决定停止尝试自动优化,并依赖开发人员来声明,我真的不知道。

    参考 here

    dev google page

    div {   
      width: 100px;   
      height: 100px;
    }
    
    div:first-child {   
      margin-bottom: 100px; 
      will-change: transform, background-color;
    }
    
    div:first-child:hover {   
      transform: scale(1.4);   
      transition: transform .3s, background-color .3s;   
      background-color: red; 
    }
    <div> 11111111111111111 </div>
    <div> 222222222222222222 </div>
        2
  •  0
  •   Parth Desai    8 年前

    此链接可以帮助解决此问题。它给出了转换属性的详细说明。

    Here's the link