Skip to content

Instantly share code, notes, and snippets.

View hehongwei44's full-sized avatar
🎯
Focusing

kevin hehongwei44

🎯
Focusing
View GitHub Profile
@hehongwei44
hehongwei44 / setPos.js
Created July 27, 2014 05:49
一些元素位置设置的通用方法
/*获取元素的CSS位置的辅助函数*/
/**
* 查找元素的左端位置,
* 代码依赖:getStyle来自 https://gist.github.com/hehongwei44/9abf63536accd0f2eeb7
* */
function posX(elem) {
return parseInt(getStyle(elem, "left"));
}
/**
@hehongwei44
hehongwei44 / offsetparent.js
Last active August 29, 2015 14:04
元素相对于整个父亲节点的left和top的辅助函数
/**
* 元素elem相对于父亲元素的左端和顶端的位置
* 依赖脚本:https://gist.github.com/hehongwei44/8d33a6e35ee045722e75
* */
/**
* 获取元素相对于父亲元素的水平位置
* */
function parentX(elem) {
/**
@hehongwei44
hehongwei44 / offsetRoot.js
Created July 27, 2014 04:06
元素相对于整个文档的left和top的辅助函数
/*元素elem相对于整个文档的左端和顶端的位置*/
/**
* 获取元素的水平位置
* */
function pageX(elem) {
/**
* 参看我们是否位于根元素
* 如果我们能继续得到上一个元素,增加当前偏移量并继续向下递归.
* 否则,获取当前的偏移量.
@hehongwei44
hehongwei44 / cssUtil.js
Created July 27, 2014 03:04
样式相关的通用函数
/**
* 获取指定元素(elem)的样式属性(name)
* */
function getStyle(elem, name) {
//如果存在于style[]中,那么它已被设置了(并且是当前的)
if (elem.style[name]) {
return elem.style[name];
}
//否则,测试IE的方法
@hehongwei44
hehongwei44 / events.js
Created July 25, 2014 08:38
事件模型的封装
/**
*
* @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
*
* */
@hehongwei44
hehongwei44 / eventUtil.js
Last active August 29, 2015 14:04
阻止事件冒泡和默认行为的通用函数
/**
* 阻止事件冒泡的通用函数
* */
function stopBubble(e) {
if (e && e.stopPropagation) {
e.stopPropagation();
} else {
window.event.cancelBubble = true;
}
}
@hehongwei44
hehongwei44 / DOMUtil.js
Created July 24, 2014 02:39
DOM操作的增强版功能函数
/**
* 将一个DOM节点、HTML字符串混合型参数
* 转化为原生的DOM节点数组
*
* */
function checkElem(a) {
var r = [];
if (a.constructor != Array) {
//如果不是参数数组,则强行转换
@hehongwei44
hehongwei44 / DOMHelper.js
Last active August 29, 2015 14:04
创建DOM元素的通用函数
/*创建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;
}
@hehongwei44
hehongwei44 / hasAttribute.js
Created July 22, 2014 02:27
检查元素是否有一个指定的特性
/*检查元素是否有一个指定的特性*/
function hasAttribute(elem, name) {
// !!表达式快速的求bool值
return !!elem.getAttribute(name);
}
@hehongwei44
hehongwei44 / text.js
Created July 18, 2014 15:07
获取元素文本内容的通用函数
/*获取元素文本内容的通用函数*/
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);