非常好的一个JS代码(CJL.0.1.js)
/*!
* Cloudgamer JavaScript Library v0.1
* Copyright (c) 2009 cloudgamer
* Blog: http://cloudgamer.cnblogs.com/
* Date: 2009-10-15
*/ var $$, $$B, $$A, $$F, $$D, $$E, $$CE, $$S;
function wr(text, ap) {
if (ap) {
document.getElementById("WriteDiv").innerHTML += text;
} else {
document.getElementById("WriteDiv").innerHTML = text;
}
}
(function (undefined) { var O, B, A, F, D, E, CE, S; /*Object*/ O = function (id) {
return "string" == typeof id ? document.getElementById(id) : id;
}; //保存了一个空的function,主要用来代替空function节省资源。
O.emptyFunction = function () { }; //扩展对象(override:是否继承属性值)destination格式:var a={name:'1',age:12'}或某个对象:new class();
O.extend = function (destination, source, override) {
if (override === undefined) override = true;
for (var property in source) {
if (override || !(property in destination)) {
destination[property] = source[property];
}
}
return destination;
}; //深度扩展,这里的深度跟深度复制里面的意思差不多,参考的是jQuery的extend。
O.deepextend = function (destination, source) {
for (var property in source) {
var copy = source[property];
if (destination === copy) continue;
if (typeof copy === "object") {
destination[property] = arguments.callee(destination[property] || {}, copy);
} else {
destination[property] = copy;
}
}
return destination;
}; /*from youa*///就复杂一点,主要用来做继承,主要参考有啊的$extends(跟prototype的Class.create也类似)。
O.wrapper = function (me, parent) {
var ins = function () { me.apply(this, arguments); };
var subclass = function () { };
subclass.prototype = parent.prototype;
ins.prototype = new subclass;
return ins;
}; /*Browser*/ /*from youa*/
B = (function (ua) {
var b = {
msie: /msie/.test(ua) && !/opera/.test(ua),
opera: /opera/.test(ua),
safari: /webkit/.test(ua) && !/chrome/.test(ua),
firefox: /firefox/.test(ua),
chrome: /chrome/.test(ua)
};
var vMark = "";
for (var i in b) {
if (b[i]) { vMark = "safari" == i ? "version" : i; break; }
}
b.version = vMark && RegExp("(?:" + vMark + ")[\\/: ]([\\d.]+)").test(ua) ? RegExp.$1 : "0"; b.ie = b.msie;
b.ie6 = b.msie && parseInt(b.version, 10) == 6;
b.ie7 = b.msie && parseInt(b.version, 10) == 7;
b.ie8 = b.msie && parseInt(b.version, 10) == 8; return b;
})(window.navigator.userAgent.toLowerCase()); /*Array*/ A = function () { var ret = {
isArray: function (obj) {
return Object.prototype.toString.call(obj) === "[object Array]";
},
indexOf: function (array, elt, from) {
if (array.indexOf) {
return (from) ? array.indexOf(elt) : array.indexOf(elt, from);
} else {
var len = array.length;
from = isNaN(from) ? 0
: from < 0 ? Math.ceil(from) + len : Math.floor(from); for (; from < len; from++) { if (array[from] === elt) return from; }
return -1;
}
},
lastIndexOf: function (array, elt, from) {
if (array.lastIndexOf) {
return isNaN(from) ? array.lastIndexOf(elt) : array.lastIndexOf(elt, from);
} else {
var len = array.length;
from = isNaN(from) || from >= len - 1 ? len - 1
: from < 0 ? Math.ceil(from) + len : Math.floor(from); for (; from > -1; from--) { if (array[from] === elt) return from; }
return -1;
}
}
}; //定义一个基本的迭代函数eac
//object:要迭代的对象,callback:回调函数:用于获取迭代对象的值(属性值属性名称,迭代对象,)
function each(object, callback) {
if (undefined === object.length) {
for (var name in object) {
// alert(name + ":" + object[name]);
if (false === callback(object[name], name, object)) break;
}
} else {
for (var i = 0, len = object.length; i < len; i++) {
if (i in object) { if (false === callback(object[i], i, object)) break; }
}
}
}; //调用each方法
each(
//object参数
{
forEach: function (object, callback, thisp) {
each(object, function () { callback.apply(thisp, arguments); });
},
map: function (object, callback, thisp) {
var ret = [];
each(object, function () { ret.push(callback.apply(thisp, arguments)); });
return ret;
},
filter: function (object, callback, thisp) {
var ret = [];
each(object, function (item) {
callback.apply(thisp, arguments) && ret.push(item);
});
return ret;
},
every: function (object, callback, thisp) {
var ret = true;
each(object, function () {
if (!callback.apply(thisp, arguments)) { ret = false; return false; };
});
return ret;
},
some: function (object, callback, thisp) {
var ret = false;
each(object, function () {
if (callback.apply(thisp, arguments)) { ret = true; return false; };
});
return ret;
}
},
//callback参数
function (method, name) {
ret[name] = function (object, callback, thisp) {
if (object[name]) {
return object[name](callback, thisp);
} else {
return method(object, callback, thisp);
}
}
}); return ret;
}(); /*Function*/ F = (function () {
var slice = Array.prototype.slice;
return {
bind: function (fun, thisp) {
//将arguments转换成真正的数组
var args = slice.call(arguments, 2);
return function () {
return fun.apply(thisp, args.concat(slice.call(arguments)));//连起来
}
},
bindAsEventListener: function (fun, thisp) {
var args = slice.call(arguments, 2);
return function (event) {
return fun.apply(thisp, [E.fixEvent(event)].concat(args));
}
}
};
})(); /*Dom*/ D = {
getScrollTop: function (node) {
var doc = node ? node.ownerDocument : document;
return doc.documentElement.scrollTop || doc.body.scrollTop;
},
getScrollLeft: function (node) {
var doc = node ? node.ownerDocument : document;
return doc.documentElement.scrollLeft || doc.body.scrollLeft;
},
contains: document.defaultView
? function (a, b) { return !!(a.compareDocumentPosition(b) & 16); }
: function (a, b) { return a != b && a.contains(b); },
rect: function (node) {
var left = 0, top = 0, right = 0, bottom = 0;
//ie8的getBoundingClientRect获取不准确
if (!node.getBoundingClientRect || B.ie8) {
var n = node;
while (n) { left += n.offsetLeft, top += n.offsetTop; n = n.offsetParent; };
right = left + node.offsetWidth; bottom = top + node.offsetHeight;
} else {
var rect = node.getBoundingClientRect();
left = right = D.getScrollLeft(node); top = bottom = D.getScrollTop(node);
left += rect.left; right += rect.right;
top += rect.top; bottom += rect.bottom;
};
return { "left": left, "top": top, "right": right, "bottom": bottom };
},
clientRect: function (node) {
var rect = D.rect(node), sLeft = D.getScrollLeft(node), sTop = D.getScrollTop(node);
rect.left -= sLeft; rect.right -= sLeft;
rect.top -= sTop; rect.bottom -= sTop;
return rect;
},
curStyle: document.defaultView
? function (elem) { return document.defaultView.getComputedStyle(elem, null); }
: function (elem) { return elem.currentStyle; },
getStyle: document.defaultView
? function (elem, name) {
var style = document.defaultView.getComputedStyle(elem, null);
return name in style ? style[name] : style.getPropertyValue(name);
}
: function (elem, name) {
var style = elem.style, curStyle = elem.currentStyle;
//透明度 from youa
if (name == "opacity") {
if (/alpha\(opacity=(.*)\)/i.test(curStyle.filter)) {
var opacity = parseFloat(RegExp.$1);
return opacity ? opacity / 100 : 0;
}
return 1;
}
if (name == "float") { name = "styleFloat"; }
var ret = curStyle[name] || curStyle[S.camelize(name)];
//单位转换 from jqury
if (!/^-?\d+(?:px)?$/i.test(ret) && /^\-?\d/.test(ret)) {
var left = style.left, rtStyle = elem.runtimeStyle, rsLeft = rtStyle.left; rtStyle.left = curStyle.left;
style.left = ret || 0;
ret = style.pixelLeft + "px"; style.left = left;
rtStyle.left = rsLeft;
}
return ret;
},
setStyle: function (elems, style, value) {
if (!elems.length) { elems = [elems]; }
if (typeof style == "string") { var s = style; style = {}; style[s] = value; }
A.forEach(elems, function (elem) {
for (var name in style) {
var value = style[name];
if (name == "opacity" && B.ie) {
//ie透明度设置 from jquery
elem.style.filter = (elem.currentStyle && elem.currentStyle.filter || "").replace(/alpha\([^)]*\)/, "") + " alpha(opacity=" + (value * 100 | 0) + ")";
} else if (name == "float") {
elem.style[B.ie ? "styleFloat" : "cssFloat"] = value;
} else {
elem.style[S.camelize(name)] = value;
}
};
});
},
getSize: function (elem) {
var width = elem.offsetWidth, height = elem.offsetHeight;
if (!width && !height) {
var repair = !D.contains(document.body, elem), parent;
if (repair) {//如果元素不在body上
parent = elem.parentNode;
document.body.insertBefore(elem, document.body.childNodes[0]);
}
var style = elem.style,
cssShow = { position: "absolute", visibility: "hidden", display: "block", left: "-9999px", top: "-9999px" },
cssBack = { position: style.position, visibility: style.visibility, display: style.display, left: style.left, top: style.top };
D.setStyle(elem, cssShow);
width = elem.offsetWidth; height = elem.offsetHeight;
D.setStyle(elem, cssBack);
if (repair) {
parent ? parent.appendChild(elem) : document.body.removeChild(elem);
}
}
return { "width": width, "height": height };
}
}; /*Event*/
E = (function () {
/*from dean edwards*/
var addEvent, removeEvent, guid = 1,
storage = function (element, type, handler) {
if (!handler.$$guid) handler.$$guid = guid++;
if (!element.events) element.events = {};
var handlers = element.events[type];
if (!handlers) {
handlers = element.events[type] = {};
if (element["on" + type]) {
handlers[0] = element["on" + type];
}
}
};
if (window.addEventListener) {
var fix = { "mouseenter": "mouseover", "mouseleave": "mouseout" };
addEvent = function (element, type, handler) {
if (type in fix) {
storage(element, type, handler);
var fixhandler = element.events[type][handler.$$guid] = function (event) {
var related = event.relatedTarget;
if (!related || (element != related && !(element.compareDocumentPosition(related) & 16))) {
handler.call(this, event);
}
};
element.addEventListener(fix[type], fixhandler, false);
} else {
element.addEventListener(type, handler, false);
};
};
removeEvent = function (element, type, handler) {
if (type in fix) {
if (element.events && element.events[type]) {
element.removeEventListener(fix[type], element.events[type][handler.$$guid], false);
delete element.events[type][handler.$$guid];
}
} else {
element.removeEventListener(type, handler, false);
};
};
} else {
addEvent = function (element, type, handler) {
storage(element, type, handler);
element.events[type][handler.$$guid] = handler;
element["on" + type] = handleEvent;
};
removeEvent = function (element, type, handler) {
if (element.events && element.events[type]) {
delete element.events[type][handler.$$guid];
}
};
function handleEvent() {
var returnValue = true, event = fixEvent();
var handlers = this.events[event.type];
for (var i in handlers) {
this.$$handleEvent = handlers[i];
if (this.$$handleEvent(event) === false) {
returnValue = false;
}
}
return returnValue;
};
} function fixEvent(event) {
if (event) return event;
event = window.event;
event.pageX = event.clientX + D.getScrollLeft(event.srcElement);
event.pageY = event.clientY + D.getScrollTop(event.srcElement);
event.target = event.srcElement;
event.stopPropagation = stopPropagation;
event.preventDefault = preventDefault;
var relatedTarget = {
"mouseout": event.toElement, "mouseover": event.fromElement
}[event.type];
if (relatedTarget) { event.relatedTarget = relatedTarget; } return event;
};
function stopPropagation() { this.cancelBubble = true; };
function preventDefault() { this.returnValue = false; }; return {
"addEvent": addEvent,
"removeEvent": removeEvent,
"fixEvent": fixEvent
};
})(); /*CustomEvent*/ CE = (function () {
var guid = 1;
return {
addEvent: function (object, type, handler) {
if (!handler.$$$guid) handler.$$$guid = guid++;
if (!object.cusevents) object.cusevents = {};
if (!object.cusevents[type]) object.cusevents[type] = {};
object.cusevents[type][handler.$$$guid] = handler;
},
removeEvent: function (object, type, handler) {
if (object.cusevents && object.cusevents[type]) {
delete object.cusevents[type][handler.$$$guid];
}
},
fireEvent: function (object, type) {
if (!object.cusevents) return;
var args = Array.prototype.slice.call(arguments, 2),
handlers = object.cusevents[type];
for (var i in handlers) {
handlers[i].apply(object, args);
}
},
clearEvent: function (object) {
if (!object.cusevents) return;
for (var type in object.cusevents) {
var handlers = object.cusevents[type];
for (var i in handlers) {
handlers[i] = null;
}
object.cusevents[type] = null;
}
object.cusevents = null;
}
};
})(); /*String*/ S = {
camelize: function (s) {
return s.replace(/-([a-z])/ig, function (all, letter) { return letter.toUpperCase(); });
}
}; /*System*/ // remove css image flicker
if (B.ie6) {
try {
document.execCommand("BackgroundImageCache", false, true);
} catch (e) { }
}; /*define*/ $$ = O; $$B = B; $$A = A; $$F = F; $$D = D; $$E = E; $$CE = CE; $$S = S; })();
原文来自:
http://www.cnblogs.com/cloudgamer/archive/2009/10/29/Cloudgamer_JavaScript_Library.html
http://www.cnblogs.com/cloudgamer/archive/2009/08/10/FixedMenu.html
非常好的一个JS代码(CJL.0.1.js)的更多相关文章
- JS代码高亮编辑器 ace.js
JS代码高亮编辑器 ace.js 字数254 阅读2 评论0 喜欢0 瞎扯 ace 是 js 实现的代码编辑器 编译打包之后的 ACE 代码 官网,未提供编译好的文件 ACE 拥有的特点 语法高亮超过 ...
- 站长常用的200个js代码 站长常用js代码大全 站长常用js代码集合
站长常用的200个js代码 1.文本框焦点问题 onBlur:当失去输入焦点后产生该事件 onFocus:当输入获得焦点后,产生该文件 Onchange:当文字值改变时,产生该事件 Onselect: ...
- 如何查找元素对应事件的js代码,检测定位js事件
比如一张图片当鼠标放到上面时,图片改变.想找到这个事件对应的js代码,假设另存为html之后,文件夹中有.js文件. 如果你会调试,可以用打开浏览器的调试功能,以chrome为例,按F12打开调试窗口 ...
- CJL.0.1.js
/*! * Cloudgamer JavaScript Library v0.1 * Copyright (c) 2009 cloudgamer * Blog: http://cloudgamer.c ...
- js代码中引入其他js文件
/***引入 js 文件 @example: import('js/aui.picker.js') @example: import(['js/aui.picker.js', 'css/aui.pic ...
- 非常好的一个JS代码(FixedMenu.htm)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- selenium 执行js代码
获取一个input输入框的值: JavascriptExecutor js =(JavascriptExecutor) driver; merchatName=js.executeScript(&qu ...
- jquery-2.0.3.js和jquery-2.0.3.min.js的区别
两个文件的作用是完全一样的. jquery-2.0.3.js里的代码是没有进行处理的原代码,适合于人们阅读与研究. jquery-2.0.3.min.js里的代码进行过特殊的处理, 如变量的名称基本都 ...
- 在dcef3当中执行js代码并获得返回值
1.如何在dcef3当中执行js代码 procedure TForm1.btnWriteZMClick(Sender: TObject);var js: string;begin js := 'd ...
随机推荐
- Spring前置方法无法阻止原方法执行(转)
在前置方法用return的方法无法阻止原方法的执行. 该处的return 只是结束了目标方法执行前的检验方法而已,而不是结束目标方法. 使用抛异常的方式程序终止. 环绕通知可以阻止原方法的执行, pu ...
- HDU 6043 - KazaQ's Socks | 2017 Multi-University Training Contest 1
/* HDU 6043 - KazaQ's Socks [ 找规律 ] | 2017 Multi-University Training Contest 1 题意: 一共 n 双袜子编号 1~n,每天 ...
- K8S中的Job和CronJob
Job Job负责批量处理短暂的一次性任务 (short lived one-off tasks),即仅执行一次的任务,它保证批处理任务的一个或多个Pod成功结束. Kubernetes支持以下几种J ...
- CUDA-F-2-2-核函数计时
Abstract: 本文介绍CUDA核函数计时方法 Keywords: gettimeofday,nvprof 开篇废话 继续更新CUDA,同时概率和数学分析也在更新,欢迎大家访问www.face2a ...
- TensorFlow使用记录 (十二): ℓ1 and ℓ2 Regularization
实现方式 以 ℓ2 Regularization 为例,主要有两种实现方式 1. 手动累加 with tf.name_scope('loss'): loss = tf.losses.softmax_c ...
- docker的数据管理
容器中管理数据主要有两种方式: 1.数据卷:容器内数据直接映射到本地宿主机. 2.数据卷容器:使用特定容器维护数据卷 数据卷: 数据卷是一个可供容器使用的特殊目录,他将主机操作系统目录直接映射进容器. ...
- beautifulsoup 安装
pip install beautifulsoup4
- vuex 讲解
vuex 状态的管理状态,它采用集中式存储管理应用的所有组件的状态,尤其是在中大型项目,则是很好的开发利器 vuex 的流程图 vuex 的优势: 1. vuex 的存储状态,响应式的 2. 他是所有 ...
- mysql之索引 应用于事物 内连接、左(外)连接、右(外)连接
什么是索引 索引就像是一本书的目录一样,能够快速找到所需要的内容 索引的作用 加快查询速率,降低IO成本加快表与表之间的连接,减少分组和排序时间 索引类型 普通索引:没有唯一性的基本索引 唯一索引:有 ...
- cocos creator发布win10全屏缩放问题
当前2.2.1版本cocos creator发布的win32版本不是全屏的, https://forum.cocos.org/t/win-exe/80429 https://blog.csdn.net ...