ajax从入门到放弃,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)

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值