JavaScript从入门到放弃
var GLOBAL = {};
GLOBAL.namespace = function(str) {
var arr = str.split('.'),
o = GLOBAL;
for (var i = (arr[0] == 'GLOBAL') ? 1 : 0; i < arr.length; i++) {
o[arr[i]] = o[arr[i]] || {};
o = o[arr[i]];
}
}
GLOBAL.namespace('Myspace'); //自定义属性名
GLOBAL.Myspace.$ = function(id) {
return typeof id === "object" ? id : document.getElementById(id)
};
GLOBAL.Myspace.$$ = function(tagName, oParent) {
return (oParent || document).getElementsByTagName(tagName)
};
GLOBAL.Myspace.$$$ = function(className, tagName, oParent) {
var reg = new RegExp("(^|\\s)" + className + "(\\s|$)"),
aEl = GLOBAL.Myspace.$$(tagName || "*", oParent)
len = aEl.length,
aClass = [],
i = 0;
for (; i < len; i++) reg.test(aEl[i].className) && aClass.push(aEl[i]);
return aClass
};
GLOBAL.Myspace.index = function(element) {
var aChild = element.parentNode.children;
for (var i = aChild.length; i--;)
if (element == aChild[i]) return i
};
GLOBAL.Myspace.css = function(element, attr, value) {
if (arguments.length == 2) {
var style = element.style,
currentStyle = element.currentStyle;
if (typeof attr === "string")
return parseFloat(currentStyle ? currentStyle[attr] : getComputedStyle(element, null)[attr])
for (var property in attr)
property == "opacity" ? (style.filter = "alpha(opacity=" + attr[property] + ")", style.opacity = attr[property] / 100) : style[property] = attr[property]
} else if (arguments.length == 3) {
switch (attr) {
case "width":
case "height":
case "paddingTop":
case "paddingRight":
case "paddingBottom":
case "paddingLeft":
value = Math.max(value, 0);
case "top":
case "right":
case "bottom":
case "left":
case "marginTop":
case "marginRigth":
case "marginBottom":
case "marginLeft":
element.style[attr] = value + "px";
break;
case "opacity":
element.style.filter = "alpha(opacity=" + value + ")";
element.style.opacity = value / 100;
break;
default:
element.style[attr] = value
}
}
};
GLOBAL.Myspace.attr = function(element, attr, value) {
if (arguments.length == 2) {
return element.attributes[attr] ? element.attributes[attr].nodeValue : undefined
} else if (arguments.length == 3) {
element.setAttribute(attr, value)
}
};
GLOBAL.Myspace.contains = function(element, oParent) {
if (oParent.contains) {
return oParent.contains(element)
} else if (oParent.compareDocumentPosition) {
return !!(oParent.compareDocumentPosition(element) & 16)
}
};
GLOBAL.Myspace.isParent = function(element, tagName) {
while (element != undefined && element != null && element.tagName.toUpperCase() !== "BODY") {
if (element.tagName.toUpperCase() == tagName.toUpperCase())
return true;
element = element.parentNode;
}
return false
};
GLOBAL.Myspace.extend = function(destination, source) {
for (var property in source) destination[property] = source[property];
return destination
};
GLOBAL.Myspace.ajax = function(config) {
var oAjax = null,
config = GLOBAL.Myspace.extend({
cache: !0,
param: "",
type: "GET",
success: function() {}
},
config);
config.url += config.param && "?" + config.param;
if (config.cache === !1) {
var timestamp = (new Date).getTime(),
re = config.url.replace(/([?&])_=[^&]*/, "$1_=" + timestamp);
config.url = re + (config.url === re ? (/\?/.test(config.url) ? "&" : "?") + "_=" + timestamp : "")
}
oAjax = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
oAjax.onreadystatechange = function() {
oAjax.readyState === 4 && oAjax.status === 200 && config.success(oAjax.responseText)
};
oAjax.open(config.type, config.url, !0);
config.type.toUpperCase() === "POST" && oAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
oAjax.send(config.type.toUpperCase() === "POST" && config.param || null)
};
GLOBAL.Myspace.animate = function(obj, json, opt) {
clearInterval(obj.timer);
obj.iSpeed = 0;
opt = GLOBAL.Myspace.extend({
type: "buffer",
callback: function() {}
}, opt);
obj.timer = setInterval(function() {
var iCur = 0,
complete = !0,
property = null,
maxSpeed = 30;
for (property in json) {
iCur = GLOBAL.Myspace.css(obj, property);
property == "opacity" && (iCur = parseInt(iCur.toFixed(2) * 100));
switch (opt.type) {
case "buffer":
obj.iSpeed = (json[property] - iCur) / 5;
obj.iSpeed = obj.iSpeed > 0 ? Math.ceil(obj.iSpeed) : Math.floor(obj.iSpeed);
json[property] == iCur || (complete = !1, GLOBAL.Myspace.css(obj, property, property == "zIndex" ? iCur + obj.iSpeed || iCur * -1 : iCur + obj.iSpeed));
break;
case "flex":
obj.iSpeed += (json[property] - iCur) / 5;
obj.iSpeed *= 0.7;
obj.iSpeed = Math.abs(obj.iSpeed) > maxSpeed ? obj.iSpeed > 0 ? maxSpeed : -maxSpeed : obj.iSpeed;
Math.abs(json[property] - iCur) <= 1 && Math.abs(obj.iSpeed) <= 1 || (complete = !1, GLOBAL.Myspace.css(obj, property, iCur + obj.iSpeed));
break;
}
}
if (complete) {
clearInterval(obj.timer);
if (opt.type == "flex")
for (property in json) GLOBAL.Myspace.css(obj, property, json[property]);
opt.callback.apply(obj, arguments);
}
}, 30)
};
点击查看更多内容
2人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦