JS 工具函数
2021-05-05 20:28
标签:com turn spl inner emc atm parent ret ++ JS 工具函数 标签:com turn spl inner emc atm parent ret ++ 原文地址:https://www.cnblogs.com/ycherry/p/12106189.html// var tools = function() {
function addEvent(el, type, fn) {
if (el.addEventListener) { //
el.addEventListener(type, fn, false);
} else if (el.attachEvent) {
el.attachEvent(‘on‘ + type, fn);
} else {
el[‘on‘ + type] = fn;
}
}
function getStyles(elem, prop) {
if (window.getComputedStyle) {
if (prop) {
return parseInt(window.getComputedStyle(elem)[prop]);
} else {
return window.getComputedStyle;
}
} else if (elem.currentStyle) {
if (prop) {
return elem.currentStyle[prop]
} else {
return elem.currentStyle;
}
}
}
function getEleDocPosition(ele) {
var parent = ele.offsetParent,
offsetLeft = ele.offsetLeft,
offsetTop = ele.offsetTop;
while (parent) {
offsetLeft += parent.offsetLeft;
offsetTop += parent.offsetTop;
parent = parent.offsetParent;
}
return {
left: offsetLeft,
top: offsetTop
}
}
function getScrollSize() {
if (document.body.scrollHeight) {
return {
width: document.body.scrollWidth,
height: document.body.scrollHeight
};
} else {
return {
width: document.documentElement.scrollWidth,
height: document.documentElement.scrollHeight
}
}
}
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 if (document.compatMode === ‘CSS1Compat‘) {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight
}
}
}
}
function getScrollOffset() {
if (window.pageXOffset) { // w3c 规范
return {
left: window.pageXOffset,
top: window.pageYOffset
}
} else {
return {
left: document.body.scrollLeft + document.documentElement.scrollLeft, //document.body.scrollLeft 和 document.documentElement.scrollLeft都是数字类型,不存在即为0
top: document.body.scrollTop + document.documentElement.scrollTop,
}
}
}
function elemChildren(node) {
var temp = {
‘length‘: 0,
‘push‘: Array.prototype.push,
‘splice‘: Array.prototype.splice
},
children = node.childNodes;
for (var i = 0; i ) {
var childItem = children[i];
if (childItem.nodeType === 1) {
// temp[temp[‘length‘]] = childItem;
// temp[‘length‘]++;
temp.push(childItem);
}
}
return temp;
}
function cancelBuble(e) {
if (e.stopPropagation) {
e.stopPropagation();
} else {
e.cancelBuble = true;
}
}
// return {
// addEvent: addEvent,
// getStyles: getStyles,
// getEleDocPosition: getEleDocPosition,
// getScrollWidth: getScrollWidth,
// getViewportSize: getViewportSize,
// getScrollOffset: getScrollOffset,
// elemChildren: elemChildren,
// cancelBuble: cancelBuble
// }
// };
下一篇:PHP工厂模式