技术好文共享:非常好的一个JS代码(CJL.0.1.js)

简介: 技术好文共享:非常好的一个JS代码(CJL.0.1.js)

/!


Cloudgamer JavaScript Library v0.1


Copyright (c) 2009 cloudgamer


Blog:


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 = //代码效果参考:http://www.lyjsj.net.cn/wx/art_23909.html

{

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;

相关文章
|
2天前
|
Web App开发 移动开发 前端开发
技术经验分享:canvas+howler.js解决同页面视频、音频同时播放问题
技术经验分享:canvas+howler.js解决同页面视频、音频同时播放问题
|
1天前
|
JavaScript vr&ar 数据库
技术笔记:Js获取当前日期时间及其它操作
技术笔记:Js获取当前日期时间及其它操作
|
1天前
|
前端开发 JavaScript 容器
程序技术好文:纯原生javascript下拉框表单美化实例教程
程序技术好文:纯原生javascript下拉框表单美化实例教程
|
1天前
|
前端开发 JavaScript 容器
技术经验解读:个人练习:使用HTML+CSS3制作图片轮播功能(不使用JavaScript)
技术经验解读:个人练习:使用HTML+CSS3制作图片轮播功能(不使用JavaScript)
|
2天前
|
JavaScript 前端开发
程序技术好文:第一百三十八节,JavaScript,封装库
程序技术好文:第一百三十八节,JavaScript,封装库
|
2天前
|
JavaScript
必知的技术知识:js保留二位小数
必知的技术知识:js保留二位小数
|
2天前
|
Web App开发 JavaScript iOS开发
技术笔记:js数组定义和方法(包含ES5新增数组方法)
技术笔记:js数组定义和方法(包含ES5新增数组方法)
|
Web App开发 JavaScript 数据安全/隐私保护
|
Web App开发 JavaScript .NET
javascript客户端检测技术
原文:javascript客户端检测技术 javascript客户端检测技术  1. Firefox    Gecko是firefox的呈现引擎。当初的Gecko是作为通用Mozilla浏览器一部分开发的,而第一个采用Gecko引擎的浏览器是Netscape6; 我们可以使用用户代理检测下:如下JS代码: var ua = navigator.
1129 0
|
3天前
|
XML 缓存 JavaScript
一篇文章讲明白JS模板引擎之JST模板
一篇文章讲明白JS模板引擎之JST模板