This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*获取元素的CSS位置的辅助函数*/ | |
/** | |
* 查找元素的左端位置, | |
* 代码依赖:getStyle来自 https://gist.github.com/hehongwei44/9abf63536accd0f2eeb7 | |
* */ | |
function posX(elem) { | |
return parseInt(getStyle(elem, "left")); | |
} | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 元素elem相对于父亲元素的左端和顶端的位置 | |
* 依赖脚本:https://gist.github.com/hehongwei44/8d33a6e35ee045722e75 | |
* */ | |
/** | |
* 获取元素相对于父亲元素的水平位置 | |
* */ | |
function parentX(elem) { | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*元素elem相对于整个文档的左端和顶端的位置*/ | |
/** | |
* 获取元素的水平位置 | |
* */ | |
function pageX(elem) { | |
/** | |
* 参看我们是否位于根元素 | |
* 如果我们能继续得到上一个元素,增加当前偏移量并继续向下递归. | |
* 否则,获取当前的偏移量. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 获取指定元素(elem)的样式属性(name) | |
* */ | |
function getStyle(elem, name) { | |
//如果存在于style[]中,那么它已被设置了(并且是当前的) | |
if (elem.style[name]) { | |
return elem.style[name]; | |
} | |
//否则,测试IE的方法 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @author Dean Edwards | |
* @date 2005/10 | |
* @link http://dean.edwards.name/weblog/2005/10/add-event/ | |
* @transform https://github.com/hehongwei44 | |
* @compatibility IE6+ ,FF, chrome | |
* | |
* */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 阻止事件冒泡的通用函数 | |
* */ | |
function stopBubble(e) { | |
if (e && e.stopPropagation) { | |
e.stopPropagation(); | |
} else { | |
window.event.cancelBubble = true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 将一个DOM节点、HTML字符串混合型参数 | |
* 转化为原生的DOM节点数组 | |
* | |
* */ | |
function checkElem(a) { | |
var r = []; | |
if (a.constructor != Array) { | |
//如果不是参数数组,则强行转换 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*创建DOM元素的通用函数*/ | |
function create (elem) { | |
return document.createElementNS ? document.createElementNS('http://' + | |
'www.w3.org/1999/xhtml', elem) : document.createElement(elem); | |
} | |
//如果提供的是字符串,则把它转化成文本节点. | |
function checkElem(elem) { | |
return elem && elem.constructor == "String" ? document.createTextNode(elem) : elem; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*检查元素是否有一个指定的特性*/ | |
function hasAttribute(elem, name) { | |
// !!表达式快速的求bool值 | |
return !!elem.getAttribute(name); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*获取元素文本内容的通用函数*/ | |
function text(e) { | |
var str = ""; | |
//判断元素是否包含子元素 | |
e = e.childNodes || e; | |
//遍历子元素,获取其文本内容 | |
for (var i = 0; i < e.length; i++) { | |
//如果子元素下面还包含子元素,则递归执行 | |
str += e[i].nodeType != 1 ? (e[i].nodeValue) : text(e[i].childNodes); |