children 封装
针对 Node.children 是 IE > 7 之后的版本才实现。
为兼容低版本浏览器,为 Node.children 封装一个的 elemChildren 方法:
function elemChildren(node){var children = { // 创建一个类数组以便将元素节点放入length: 0,push: Array.prototype.push,splice: Array.prototype.splice},len = node.childNodes.length;for(var i = 0; i < len; i++){ // 找出 MLElement.childNodes 下类型 nodeType 为元素(1:Node.ELEMENT_NODE)的节点var childrenItem = node.childeNodes[i];if(childrenItem.nodeType === 1){children.push(childrenItem); // 是元素节点时放入到类数组中}}return children;}
parentElement 封装
针对 Node.parentElement 是 IE > 9 之后的版本才实现。
并且 Node.parentNode/ Node.parentElement 只支持一层父节点,如果要祖父或以上级别需要多次调用
为兼容低版本浏览器,为 HTMLElement.parentElement 封装一个的 parentElement 方法:
function parentElement(elem, tier){var type = typeof tier;if(type === "undefined"){ //tier 为空直接输出 parentNode / parentElementreturn elem.parentElement ? elem.parentElement : elem.parentNode;}else if(tier <= 0 || type !== 'number'){return undefined;}while(tier){ // tier 自减至 0 时停止elem = elem.parentElement ? elem.parentElement : elem.parentNode;tier --;}return elem;}
insertAfter 封装
https://www.yuque.com/sylaryip/aasla4/gi2ln3#Jg2qq
function insertAfter(node, a, b){node.insertBefore(b, a.nextSibling)}
pageXOffset/pageYOffset 封装
https://www.yuque.com/sylaryip/aasla4/ukrluf#eacb2ab9
兼容IE 与 现代浏览器
function getScrollOffset(){if(window.pageXOffset){return {left: window.pageXOffset, top:window.pageYOffset};}else{return { //IE要么document.documentElement在要么在document.body获取scrollLeft / scrollTopleft: document.documentElement.srcollLeft + document.body.scrollLeft,top: document.documentElement.scrollTop + document.body.scrollTop,}}}
innerWidth/innerHeight 封装
https://www.yuque.com/sylaryip/aasla4/gl72xq
function getViewportSize(){if(window.innerWidth){return {width: window.innerWidth,height: window.innerHeight,}}else{// 不包括滚动条的宽度if(document.compatMode === 'BackCompat'){return {width: document.body.clientWidth,height: document.body.clientHeight,}}else{return {width: document.documentElement.clientWidth,height: document.documentElement.clientHeight,}}}}
offsetLeft / offsetTop 封装
https://www.yuque.com/sylaryip/aasla4/siw1wu
元素到窗口的距离
function getElemDocPosition(el){var parent = el.offsetParent,offsetLeft = el.offsetLeft,offsetTop = el.offsetTop;while(parent){ // 一直至 body , body 的 offsetParent 为 nulloffsetLeft += parent.offsetLeft;offsetTop += parent.offsetTop;parent = parent.offsetParent;}return {left: offsetLeft,top: offsetTop,}}
window.getCoumputedStyle 封装
DOM2 和 DOM3
IE不支持 getComputedStyle,有 HTMLElement.currentStyle 但不支持伪类。
function getStyles(elem, prop){if(window.getComputedStyle){return prop ? window.getComputedStyle(elem, null)[prop]: window.getComputedStyle(elem, null);}else{return prop ? elem.currentStyle[prop]: elem.currentStyle;}}
elem.attachEvent 封装
function addEvent(el, type, fn, capture) {if (el.addEventListener) {el.addEventListener(type, fn, capture);} else if (el.attachEvent) {el.attachEvent('on' + type, function () {fn.call(el);});} else {el['on' + type] = fn;}};
e.stopPropagation 封装
function cancelBubble(e) {var e = e || window.event;if (e.stopPropagation) {e.stopPropagation();} else {e.cancelBubble = true;}};
