<<High Performance JavaScript>>读书笔记-3.DOM Scripting

HTML collections are array-like objects containing DOM node references. 

• document.getElementsByName()

• document.getElementsByClassName()

• document.getElementsByTagName()

• document.images

• document.links

• document.forms

• document.forms[0].elements

HTML collections 的值受当前页面的影响,使用的时候最好先存到数组中。

function toArray(coll) {
    for (var i = 0, a = [], len = coll.length;i < len; i++) {
        a[i] = coll[i];
    }
    return a;
}


 

A DOM tree

    A representation of the page structure

A render tree

    A representation of how the DOM nodes willbe displayed

 

Reflow is needed whenever layout and geometry change. This happens when:

• Visible DOM elements are added or removed

• Elements change position

• Elements change size (because of a change inmargin, padding, border thickness,width, height, etc.)

• Content is changed, e.g., text changes or animage is replaced with one of a differentsize

• Page renders initially

• Browser window is resized

 

Queuing and Flushing Render Tree Changes

• offsetTop, offsetLeft, offsetWidth, offsetHeight

• scrollTop, scrollLeft, scrollWidth, scrollHeight

• clientTop, clientLeft, clientWidth, clientHeight

• getComputedStyle() (currentStyle in IE)

 

Minimizing Repaints and Reflows

//bad,reflow three times

var el = document.getElementById('mydiv');

el.style.borderLeft = '1px';

el.style.borderRight = '2px';

el.style.padding = '5px';

 

//good,reflow one time

var el = document.getElementById('mydiv');

el.style.cssText = 'border-left: 1px; border-right: 2px; padding:5px;';

 

Batching DOM changes

1. Take the element off of the document flow.

2. Apply multiple changes.

3. Bring the element back to the document.

There are three basic ways to modify the DOM off the document:

• Hide the element, apply changes, and show it again.

var ul = document.getElementById('mylist');

ul.style.display = 'none';

appendDataToElement(ul, data);

ul.style.display = 'block';

• Use a document fragment to build a subtree outside of the live DOM and then copy it to the document.

var fragment = document.createDocumentFragment();

appendDataToElement(fragment, data);

document.getElementById('mylist').appendChild(fragment);

• Copy the original element into an off-document node, modify the copy, and then replace the original element once you're done.

var old = document.getElementById('mylist');

var clone = old.cloneNode(true);

appendDataToElement(clone, data);

old.parentNode.replaceChild(clone, old);

 

Caching Layout Information

 

Take Elements Out of the Flow for Animations

1. Use absolute positioning for the element you want to animate on the page, taking it out of the layout flow of the page.

2. Animate the element. When it expands, it will temporarily cover part of the page. This is a repaint, but only of a small part of the page instead of a reflow and repaint of a big page chunk.

3. When the animation is done, restore the positioning, thereby pushing down the rest of the document only once.

 

Event Delegation

 

Summary

• Minimize DOM access, and try to work as much as possible in JavaScript.

• Use local variables to store DOM references you’ll access repeatedly.

• Be careful when dealing with HTML collections because they represent the live,underlying document. Cache the collection length into a variable and use it when iterating, and make a copy of the collectioninto an array for heavy work oncollections.

• Use faster APIs when available, such as querySelectorAll() and firstElementChild.

• Be mindful of repaints and reflows; batchstyle changes, manipulate the DOM tree“offline,” and cache and minimize access to layout information.

• Position absolutely during animations, and use drag and drop proxies.

• Use event delegation to minimize the number of event handlers.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值