zoukankan      html  css  js  c++  java
  • How (not) to trigger a layout in WebKit

    As most web developers are aware, a significant amount of a script's running time may be spent performing DOM operations triggered by the script rather than executing the JS byte code itself. One such potentially costly operation is layout (aka reflow) -- the process of constructing a render tree from a DOM tree. The larger and more complex the DOM, the more expensive this operation may be.

    An important technique for keeping a page snappy is to batch methods that manipulate the DOM separately from those that query the state. For example:

    // Suboptimal, causes layout twice.
    var newWidth = aDiv.offsetWidth + 10; // Read
    aDiv.style.width = newWidth + 'px'; // Write
    var newHeight = aDiv.offsetHeight + 10; // Read
    aDiv.style.height = newHeight + 'px'; // Write

    // Better, only one layout.
    var newWidth = aDiv.offsetWidth + 10; // Read
    var newHeight = aDiv.offsetHeight + 10; // Read
    aDiv.style.width = newWidth + 'px'; // Write
    aDiv.style.height = newHeight + 'px'; // Write

    Stoyan Stefanov's tome on repaint, relayout and restyle provides an excellent explanation of the topic.

    This often leaves developers asking the question: What triggers layout? Last week Dimitri Glazkov answered this question with this codesearch link. Trying to understand it better myself, I went through and translated it into a list of properties and methods. Here they are:

    Element

    clientHeight, clientLeft, clientTop, clientWidth, focus(), getBoundingClientRect(), getClientRects(), innerText, offsetHeight, offsetLeft, offsetParent, offsetTop, offsetWidth, outerText, scrollByLines(), scrollByPages(), scrollHeight, scrollIntoView(), scrollIntoViewIfNeeded(), scrollLeft, scrollTop, scrollWidth

    Frame, Image

    height, width

    Range

    getBoundingClientRect(), getClientRects()

    SVGLocatable

    computeCTM(), getBBox()

    SVGTextContent

    getCharNumAtPosition(), getComputedTextLength(), getEndPositionOfChar(), getExtentOfChar(), getNumberOfChars(), getRotationOfChar(), getStartPositionOfChar(), getSubStringLength(), selectSubString()

    SVGUse

    instanceRoot

    window

    getComputedStyle(), scrollBy(), scrollTo(), scrollX, scrollY, webkitConvertPointFromNodeToPage(), webkitConvertPointFromPageToNode()

    This list is almost certainly not complete, but it is a good start. The best way to check for over-layout is to watch for the purple layout bars in the Timeline panel of Chrome or Safari's Inspector.

    来源:http://gent.ilcore.com/2011/03/how-not-to-trigger-layout-in-webkit.html

  • 相关阅读:
    使用循环计算斐波那契数列
    java 面向过程实现万年历
    学习js回调函数
    序列化和反序列化
    SQL语句增加字段、修改字段、修改类型、修改默认值
    使用SQL语句查询日期(当月天数,当月第一天,当月最后一天,本年最后一天,当月第一个星期) 日期转字符串
    RGB颜色表
    String 与StringBuilder有什么区别
    C# byte数组与Image的相互转换
    CentOS 7 MySQL、Tomcat、Zookeeper设置开机自启
  • 原文地址:https://www.cnblogs.com/yuzhongwusan/p/5274868.html
Copyright © 2011-2022 走看看