/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =================================================================================== 程序名称: JavaScript 封装库 BETA 3.0 版 迭代版本: BETA 2.0 插件总数: 8 个 库方法数: 20 个 功能总数: 48 个 新增总数: 7 个 删除总数: 0 个 追加功能: 1. 新增“插件库”支持, 可自由扩展网页特效插件 2. 新增“元素节点选择器”, 元素节点操纵简便性大大提升, 可读性加强 3. 新增“HTML DOM 加载”, 节省外部文件加载耗时 4. 库方法: 新增“CSS 处理”, 分离调用代码可读性加强 5. 库方法: 新增“class 处理”, 分离调用代码可读性加强 ... 优化功能: 1. 优化节点定位并删除了 name 方式定位, 遵循 W3C 原则 2. 优化节点事件绑定, 集中至“nodeEvent”方法同一调用, 可读性、扩展性、维护性大大加强 3. 库方法: 优化“清除空白节点”, 采用倒序遍历, 删除更加彻底简便 4. 库方法: 优化“清除注释节点”, 采用倒序遍历, 删除更加彻底简便 .... 删除功能: 无 */ // 实例化基础库 $ = function () { return arguments.length == 0 ? new Base() : new Base(arguments); }; // 基础库构造方法 var Base = function (nodes) { this.info = '无节点'; this.elements = []; if (typeof nodes == 'object') { var elements = [], node = null; for (var i = 0; i < nodes.length; i ++) { node = nodes[i]; if (typeof node == 'string') { // 元素节点选择器 // 全局或局部 if (this.elements.length == 0) this.elements.push(document); var patternSpace = /[\s\t ]+/; if (patternSpace.test(node)) { // 多层 node = node.split(patternSpace); for (var j = 0; j < node.length; j ++) { if (j == (node.length - 1)) { elements.push(this.selector(node[j]).getNodes()); } else { this.selector(node[j]); } } } else { // 单层 elements.push(this.selector(node).getNodes()); } this.elements = []; } else if (typeof node == 'object' && (node == window || typeof node.nodeType != 'undefined')) { // 节点对象 this.elements.push(node); } else if (typeof node == 'function') { // HTML DOM 加载 this.htmlLoaded(node); } } if (typeof elements != 'undefined') { for (var k = 0; k < elements.length; k ++) { if (elements[k] instanceof Array) { // 集群 for (var l = 0; l < elements[k].length; l ++) { if (elements[k][l] != this.info) this.elements.push(elements[k][l]); } } else { // 单一 if (elements[k] != this.info) this.elements.push(elements[k]); } } } } }; // 获取采集的所有节点 Base.prototype.getNodes = function () { if (this.elements.length == 0) return this.info; if (this.elements.length == 1) return this.firstNode(); return this.elements; }; // 获取采集的首位节点 Base.prototype.firstNode = function () { if (this.elements.length == 0) return this.info; return this.elements[0]; }; // 获取采集的末尾节点 Base.prototype.lastNode = function () { if (this.elements.length == 0) return this.info; return this.elements[this.elements.length - 1]; }; // 获取 id 元素节点 Base.prototype.getId = function (ids, positioner) { if (ids instanceof Array) { // 集群 for (var i = 0; i < ids.length; i ++) { this.getId(ids[i], positioner); } } else { // 单一 var selector = null, node = null; if (typeof positioner != 'undefined') { // 局部 if (typeof positioner == 'object' && typeof positioner.nodeType != 'undefined') { selector = positioner; } else { if (!(selector = Base.positioner(positioner))) return this; } } else { // 全局 selector = document; } if (selector instanceof Array) { // 集群 for (var j = 0; j < selector.length; j ++) { node = selector[j].getElementsByTagName('*'); for (var k = 0; k < node.length; k ++) { if (node[k].id == ids) { for (var i = 0; i < this.elements.length; i ++) { if (this.elements[i] == node[k]) return this; } this.elements.push(node[k]); } } } } else { // 单一 // 容错处理 if (typeof selector.getElementById != 'undefined') { node = selector.getElementById(ids); if (node === null) return this; for (var i = 0; i < this.elements.length; i ++) { if (this.elements[i] == node) return this; } this.elements.push(node); } else { node = selector.getElementsByTagName('*'); for (var j = 0; j < node.length; j ++) { if (node[j].id == ids) { for (var i = 0; i < this.elements.length; i ++) { if (this.elements[i] == node[j]) return this; } this.elements.push(node[j]); } } } } } return this; }; // 获取 tagName 元素节点 Base.prototype.getTagName = function (tagName, positioner) { var selector = null, nodes = []; if (typeof positioner != 'undefined') { // 局部 if (typeof positioner == 'object' && typeof positioner.nodeType != 'undefined') { selector = positioner; } else { if (!(selector = Base.positioner(positioner))) return this; } } else { // 全局 selector = document; } if (selector instanceof Array) { // 集群 for (var j = 0; j < selector.length; j ++) { nodes = selector[j].getElementsByTagName(tagName); for (var i = 0; i < nodes.length; i ++) { this.elements.push(nodes[i]); } } } else { // 单一 nodes = selector.getElementsByTagName(tagName); for (var i = 0; i < nodes.length; i ++) { this.elements.push(nodes[i]); } } return this; }; // 获取 class 元素节点 Base.prototype.getClass = function (className, positioner) { var selector = null, nodes = []; if (typeof positioner != 'undefined') { // 局部 if (typeof positioner == 'object' && typeof positioner.nodeType != 'undefined') { selector = positioner; } else { if (!(selector = Base.positioner(positioner))) return this; } } else {// 全局 selector = document; } if (selector instanceof Array) { // 集群 for (var j = 0; j < selector.length; j ++) { nodes = selector[j].getElementsByTagName('*'); for (var i = 0; i < nodes.length; i ++) { if (Base.hasClass(className, nodes[i])) this.elements.push(nodes[i]); } } } else { // 单一 nodes = selector.getElementsByTagName('*'); for (var i = 0; i < nodes.length; i ++) { if (Base.hasClass(className, nodes[i])) this.elements.push(nodes[i]); } } return this; }; // 获取 name 元素节点 Base.prototype.getName = function (name, positioner) { var selector = null, nodes = []; if (typeof positioner != 'undefined') { // 局部 if (typeof positioner == 'object' && typeof positioner.nodeType != 'undefined') { selector = positioner; } else { if (!(selector = Base.positioner(positioner))) return this; } } else { // 全局 selector = document; } if (selector instanceof Array) { // 集群 for (var j = 0; j < selector.length; j ++) { nodes = selector[j].getElementsByTagName('*'); for (var i = 0; i < nodes.length; i ++) { if (nodes[i].name == name) this.elements.push(nodes[i]); } } } else { // 单一 // 容错处理 nodes = typeof selector.getElementsByName != 'undefined' ? selector.getElementsByName(name) : selector.getElementsByTagName('*'); for (var i = 0; i < nodes.length; i ++) { if (nodes[i].name == name) this.elements.push(nodes[i]); } } return this; }; // 元素节点选择器 Base.prototype.selector = function (node) { if (typeof node == 'string') { var nodes = [], elements = []; var patternId = /([a-z]+)#([\w\-]+)/i; var patternClass = /([a-z]+)\.([\w\-]+)/i; var tagName = '', id = '', className = ''; for (var i = 0; i < this.elements.length; i ++) { switch (node.charAt(0)) { case '#' : // id if ((nodes = $().getId(node.substring(1, node.length), this.elements[i]).getNodes()) != this.info) { elements.push(nodes); } break; case '.' : // class if ((nodes = $().getClass(node.substring(1, node.length), this.elements[i]).getNodes()) != this.info) { if (nodes instanceof Array) { // 集群 for (var j = 0; j < nodes.length; j ++) { elements.push(nodes[j]); } } else { // 单一 elements.push(nodes); } } break; default : if (patternId.test(node)) { // tagName + id tagName = patternId.exec(node)[1]; id = patternId.exec(node)[2]; if ((nodes = $().getTagName(tagName, this.elements[i]).getNodes()) != this.info) { if (nodes instanceof Array) { // 集群 for (var j = 0; j < nodes.length; j ++) { if (nodes[j].id == id) elements.push(nodes[j]); } } else { // 单一 if (nodes.id == id) elements.push(nodes); } } } else if (patternClass.test(node)) { // tagName + class tagName = patternClass.exec(node)[1]; className = patternClass.exec(node)[2]; if ((nodes = $().getTagName(tagName, this.elements[i]).getNodes()) != this.info) { if (nodes instanceof Array) { // 集群 for (var j = 0; j < nodes.length; j ++) { if (Base.hasClass(className, nodes[j])) elements.push(nodes[j]); } } else { // 单一 if (Base.hasClass(className, nodes)) elements.push(nodes); } } } else { // tagName if ((nodes = $().getTagName(node, this.elements[i]).getNodes()) != this.info) { if (nodes instanceof Array) { // 集群 for (var j = 0; j < nodes.length; j ++) { elements.push(nodes[j]); } } else { // 单一 elements.push(nodes); } } } } } this.elements = elements; } return this; }; // 获取与设置元素内容 Base.prototype.html = function (text) { var html = []; for (var i = 0; i < this.elements.length; i ++) { if (typeof text != 'undefined') { // 设置 this.elements[i].innerHTML = text; } else { // 获取 html.push(this.elements[i].innerHTML); } } switch (html.length) { case 0 : return this; break; case 1 : return html[0]; break; default : return html; } }; // 获取与设置表单元素内容 Base.prototype.value = function (text) { var value = []; for (var i = 0; i < this.elements.length; i ++) { if (typeof text != 'undefined') { // 设置 if (typeof this.elements[i].value != 'undefined') this.elements[i].value = text; } else { // 获取 if (typeof this.elements[i].value != 'undefined') value.push(this.elements[i].value); } } switch (value.length) { case 0 : return this; break; case 1 : return value[0]; break; default : return value; } }; // 获取与设置 CSS Base.prototype.css = function (cssKey, cssValue) { if (typeof cssValue != 'undefined' || cssKey instanceof Array) { // 设置 if (cssKey instanceof Array) { // 集群 var patternStyle = /^([a-z]+)[\s\t ]*=[\s\t ]*([\w\(=,\s\.#\/\\\'\"\-\)]+)$/i; var key = '', value = ''; for (var i = 0; i < cssKey.length; i ++) { if (patternStyle.test(cssKey[i])) { key = patternStyle.exec(cssKey[i])[1]; value = patternStyle.exec(cssKey[i])[2]; this.css(key, value); } } } else { // 单一 for (var i = 0; i < this.elements.length; i ++) { if (typeof this.elements[i].style[cssKey] != 'undefined') { this.elements[i].style[cssKey] = cssValue; } } } } else { // 获取 var css = [], style = ''; for (var i = 0; i < this.elements.length; i ++) { if (typeof (style = Tool.getStyle(cssKey, this.elements[i])) != 'undefined') css.push(style); } switch (css.length) { case 0 : return this; break; case 1 : return css[0]; break; default : return css; break; } } return this; }; // 添加 class 选择器 Base.prototype.addClass = function (className) { if (className instanceof Array) { // 集群 for (var i = 0; i < className.length; i ++) { this.addClass(className[i]); } } else { // 单一 var space = ''; for (var i = 0; i < this.elements.length; i ++) { space = this.elements[i].className != '' ? ' ' : space; if (!Base.hasClass(className, this.elements[i])) this.elements[i].className += space + className; } } return this; }; // 移除 class 选择器 Base.prototype.removeClass = function (className) { if (className instanceof Array) { // 集群 for (var i = 0; i < className.length; i ++) { this.removeClass(className[i]); } } else { // 单一 var node = null; for (var i = 0; i < this.elements.length; i ++) { node = this.elements[i]; if (Base.hasClass(className, node)) { node.className = node.className.replace(new RegExp('(^|\\s+)' + className + '(\\s+|$)'), ' '); node.className = Base.trim(node.className); } } } return this; }; // 添加样式规则 Base.prototype.addRule = function (ruleName, ruleText, rulePosition, sheetIndex) { if (ruleName instanceof Array) { // 集群 if (!(ruleText instanceof Array) || ruleName.length != ruleText.length) return this; for (var i = 0; i < ruleName.length; i ++) { if (ruleName[i] && ruleText[i]) this.addRule(ruleName[i], ruleText[i], rulePosition, sheetIndex); } } else { // 单一 var rule = Base.checkRule(rulePosition, sheetIndex); if (typeof rule.sheet != 'undefined') { var nameTotal = Tool.ruleNameTotal(rule.sheet); for (var i = 0; i < nameTotal.length; i ++) { if (nameTotal[i] == ruleName) return this; } Tool.addRule(rule.sheet, ruleName, ruleText, rule.position); } } return this; }; // 移除样式规则 Base.prototype.removeRule = function (rulePosition, ruleNumber, sheetIndex) { var rule = Base.checkRule(rulePosition, sheetIndex, ruleNumber); if (typeof rule.sheet != 'undefined') { for (var i = 0; i < rule.number; i ++) { Tool.removeRule(rule.sheet, rule.position); } } return this; }; // 节点事件绑定 Base.prototype.nodeEvent = function (eventName, method, mode) { if (typeof eventName != 'string' || typeof method != 'function') return this; eventName = eventName.toLowerCase(); if (eventName.indexOf('on') == 0) eventName = eventName.substring(2, eventName.length); if (typeof mode == 'undefined') mode = true; for (var i = 0; i < this.elements.length; i ++) { switch (eventName) { case 'mousewheel' : Tool.mousewheel(this.elements[i], method, mode); break; default : mode ? Tool.loginEvent(this.elements[i], eventName, method) : Tool.logoutEvent(this.elements[i], eventName, method); } } return this; }; // HTML DOM 加载 Base.prototype.htmlLoaded = function (method) { if (typeof method == 'function') Tool.htmlLoaded(method); return this; }; // 加载特效插件 Base.prototype.plugins = function (pluginName, pluginMethod) { if (pluginName instanceof Array) { // 集群 if (!(pluginMethod instanceof Array) || pluginName.length != pluginMethod.length) return false; for (var i = 0; i < pluginName.length; i ++) { this.plugins(pluginName[i], pluginMethod[i]); } } else { // 单一 if (typeof pluginName == 'string' && typeof pluginMethod == 'function') { Base.prototype[pluginName] = pluginMethod; } } return this; }; /* 库方法集合 */ // 库方法: 清除两边空格 Base.trim = function (string) { if (typeof string != 'string') return string; var patternLeft = /^[\s\t ]+/; var patternRight = /[\s\t ]+$/; if (patternLeft.test(string)) string = string.replace(patternLeft, ''); if (patternRight.test(string)) string = string.replace(patternRight, ''); return string; }; // 库方法: 检测样式规则 Base.checkRule = function (rulePosition, sheetIndex, ruleNumber) { if (typeof rulePosition != 'number') rulePosition = 0; if (typeof sheetIndex != 'number') sheetIndex = 0; if (typeof ruleNumber != 'number') ruleNumber = 1; var sheetObject = undefined; var sheetTotal = document.styleSheets.length; if (sheetTotal != 0 && sheetIndex <= (sheetTotal - 1)) { // 兼容 IE 6/7/8 防止 unknown sheetObject = document.styleSheets[sheetIndex]; } if (typeof sheetObject != 'undefined') { var ruleTotal = Tool.ruleTotal(sheetObject); if (rulePosition > ruleTotal) rulePosition = ruleTotal; var maxNumber = ruleTotal - rulePosition; if (ruleNumber > maxNumber) ruleNumber -= ruleNumber - maxNumber; } return { position : rulePosition, sheet : sheetObject, number : ruleNumber } }; // 库方法: 检测 class Base.hasClass = function (className, elementNode) { return (new RegExp('(^|\\s+)' + className + '(\\s+|$)')).test(elementNode.className); } // 库方法: 元素节点定位器(局部搜索) Base.positioner = function (positioner) { if (typeof positioner != 'string') return false; var nodes = []; var patternId = /^id\s*=\s*([\w\-]+)$/i; var patternTag = /^tag(Name)?\s*=\s*([a-z]+)$/i; var patternClass = /^class(Name)?\s*=\s*([\w\-]+)$/i; if (patternTag.test(positioner)) { // tagName positioner = patternTag.exec(positioner)[2]; nodes = $().getTagName(positioner).getNodes(); } else if (patternClass.test(positioner)) { // class positioner = patternClass.exec(positioner)[2]; nodes = $().getClass(positioner).getNodes(); } else if (patternId.test(positioner)) { // id positioner = patternId.exec(positioner)[1]; nodes = $().getId(positioner).getNodes(); } else { // id nodes = $().getId(positioner).getNodes(); } if (nodes == $().info) return false; return nodes; }; // 库方法: 获取元素从属关系(直接从属) Base.hasChild = function (elementNode, targetNode) { var childs = targetNode.childNodes; for (var i = 0; i < childs.length; i ++) { if (childs[i].nodeType == 1 && childs[i] == elementNode) return true; } return false; }; // 库方法: 清除空白节点 Base.clear = function (elementNode) { var childs = elementNode.childNodes; var length = childs.length; for (var i = (length - 1); i >= 0; i --) { if (childs[i].nodeType == 3 && /^[\s\t ]+$/.test(childs[i].nodeValue)) elementNode.removeChild(childs[i]); } return elementNode; }; // 库方法: 清除注释节点 Base.comment = function (elementNode) { var childs = elementNode.childNodes; var length = childs.length; for (var i = (length - 1); i >= 0; i --) { if (childs[i].nodeType == 8) { elementNode.removeChild(childs[i]); } } return elementNode; }; // 库方法: 字符串删减 Base.replace = function (string, index) { if (typeof string != 'string') return string; if (string.indexOf(index) != -1) string = string.replace(new RegExp(index, 'ig'), ''); return string; }; // 库方法: 添加元素节点 Base.addElement = function (elementName, attributes, html, targetNode) { var elementNode = document.createElement(elementName); if (typeof attributes == 'string' || attributes instanceof Array) this.addAttribute(attributes, elementNode); if (typeof html != 'undefined') $(elementNode).html(html); if (typeof targetNode != 'undefined') { if (!(typeof targetNode == 'object' || targetNode.nodeType == 1)) { if (!(targetNode = this.positioner(targetNode))) targetNode = document.body; } } else { targetNode = document.body; } if (targetNode instanceof Array) { // 集群 for (var i = 0; i < targetNode.length; i ++) { targetNode[i].appendChild(elementNode.cloneNode(true)); } } else { // 单一 targetNode.appendChild(elementNode); } return elementNode; }; // 库方法: 添加属性节点 Base.addAttribute = function (attributes, elementNode) { if (attributes instanceof Array) { // 集群 for (var i = 0; i < attributes.length; i ++) { this.addAttribute(attributes[i], elementNode); } } else { // 单一 if (typeof attributes != 'string') return false; var patternAttribute = /^([a-z]+)[\s\t ]*=[\s\t ]*([\w,\(#=:\s\.\/\\\'\";\-\)]+)$/i; var key = '', value = ''; if (patternAttribute.test(attributes)) { key = patternAttribute.exec(attributes)[1]; value = patternAttribute.exec(attributes)[2]; var patternClass = /^class(Name)?$/i, className = []; if (key == 'style') { // CSS this.style(value, elementNode); } else if (patternClass.test(key)) { // class this.addClass(value, elementNode); } else { elementNode.setAttribute(key, value); } } } }; // 库方法: CSS 处理 Base.style = function (css, elementNode) { var split = /;[\s\t ]*/i; if (split.test(css)) { // 集群 css = css.split(split); for (var i = 0; i < css.length; i ++) { this.style(css[i], elementNode); } } else { // 单一 var patternCSS = /^([a-z]+)[\s\t ]*:[\s\t ]*([\w\(=\s\.,\/\\\'#\"\-\)]+)$/i, cssKey = '', cssValue = ''; if (patternCSS.test(css)) { cssKey = patternCSS.exec(css)[1]; cssValue = patternCSS.exec(css)[2]; $(elementNode).css(cssKey, cssValue); } } }; // 库方法: class 处理 Base.addClass = function (className, elementNode) { var split = /[\s\t ]+/; if (split.test(className)) { className = className.split(split); for (var i = 0; i < className.length; i ++) { this.addClass(className[i], elementNode); } } else { $(elementNode).addClass(className); } }; // 库方法: 移除元素节点 Base.removeElement = function (elementNode, targetNode) { if (!(typeof elementNode == 'object' || elementNode.nodeType == 1)) { if (!(elementNode = this.positioner(elementNode))) return false; } if (typeof targetNode == 'undefined' || !(typeof targetNode == 'object' || targetNode.nodeType == 1)) { if (!(targetNode = this.positioner(targetNode))) targetNode = document.body; } if (targetNode instanceof Array) { // 集群 for (var i = 0; i < targetNode.length; i ++) { this.removeElement(elementNode, targetNode[i]); } } else { // 单一 if (elementNode instanceof Array) { // 集群 for (var i = 0; i < elementNode.length; i ++) { this.removeElement(elementNode[i], targetNode); } } else { // 单一 if (this.hasChild(elementNode, targetNode)) targetNode.removeChild(elementNode); } } }; // 库方法: 初始化滚动条 Base.initializationScroll = function () { Tool.scrollLeft(0); Tool.scrollTop(0); }; // 库方法: 设置与获取滚动条 Base.scroll = function (x, y) { if (typeof x != 'number' && typeof y != 'number') { // 获取 return { x : Tool.scrollLeft(), y : Tool.scrollTop() } } if (typeof x == 'number') { // 设置x轴 Tool.scrollLeft(x); } if (typeof y == 'number') { // 设置y轴 Tool.scrollTop(y); } }; // 库方法: 获取网页可视区尺寸 Base.getWindowRectangle = function () { return { width : Tool.getWindowWidth(), height : Tool.getWindowHeight() } }; // 库方法: 获取元素尺寸 Base.getInnerRectangle = function (elementNode) { var width = 0, height = 0, display = '', _this = null; _this = $(elementNode); display = _this.css('display'); width = _this.css('width'); height = _this.css('height'); if (width == 'auto' || height == 'auto') { // IE 6/7/8 if (display == 'none') _this.show(); width = elementNode.offsetWidth; height = elementNode.offsetHeight; } width = this.replace(width, 'px'); height = this.replace(height, 'px'); return { width : width, height : height }; }; // 库方法: 获取元素外边距尺寸 Base.getOuterRectangle = function (elementNode) { this.absolute(elementNode); var left = 0, top = 0, display = '', _this = null; _this = $(elementNode); display = _this.css('display'); left = _this.css('left'); top = _this.css('top'); if (left == 'auto' || top == 'auto') { // IE 6/7/8 if (display == 'none') _this.show(); left = elementNode.offsetLeft; top = elementNode.offsetTop; } left = this.replace(left, 'px'); top = this.replace(top, 'px'); return { left : left, top : top }; }; // 库方法: 设置元素绝对定位 Base.absolute = function (elementNode) { var _this = $(elementNode); if (_this.css('position') != 'absolute') _this.css(['position = absolute', 'left = 0', 'top = 0']); }; // 库方法: 固定锁屏区域(修正浏览器Bug) Base.fixed = function () { // IE Bug 修正 var down = function () { var move = function (event) { event.preventDefault(); }; var up = function () { $('#screen').nodeEvent('mousemove', move, false); $('#screen').nodeEvent('mouseup', up, false); }; $('#screen').nodeEvent('mousemove', move); $('#screen').nodeEvent('mouseup', up); }; $('#screen').nodeEvent('mousedown', down); // Chrome Bug 修改 $('#screen').nodeEvent('mousewheel', function (event) { event.preventDefault(); }); };
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =================================================================================== 程序名称: JavaScript 工具库(跨浏览器兼容) BETA 3.0 版 迭代版本: BETA 2.0 功能总数: 18 个 新增总数: 2 个 删除总数: 0 个 追加功能: 1. 新增“跨浏览器获取样式规则名称集合” 2. 新增“跨浏览器 HTML DOM 加载” 优化功能: 1. 优化跨浏览器滚动条设置与获取至“scrollLeft”和“scrollTop”方法中 删除功能: 无 */ // 工具库 var Tool = { // 数组排序 sort : { minToMax : function (min, max) { if (min < max) { return -1; } else if (min > max) { return 1; } else { return 0; } }, maxToMin : function (min, max) { if (min < max) { return 1; } else if (min > max) { return -1; } else { return 0; } } }, // 跨浏览器获取计算后的样式 getStyle : function (cssKey, elementNode) { if (typeof window.getComputedStyle != 'undefined') { // W3C return window.getComputedStyle(elementNode, null)[cssKey]; } else if (typeof elementNode.currentStyle != 'undefined') { // IE 6/7/8 return elementNode.currentStyle[cssKey]; } }, // 跨浏览器获取样式规则总数 ruleTotal : function (sheetObject) { if (typeof sheetObject.cssRules != 'undefined') { // W3C return sheetObject.cssRules.length; } else if (typeof sheetObject.rules != 'undefined') { // IE 6/7/8 return sheetObject.rules.length; } }, // 跨浏览器获取样式规则名称集合 ruleNameTotal : function (sheetObject) { var nameTotal = []; if (typeof sheetObject.cssRules != 'undefined') { // W3C for (var i = 0; i < sheetObject.cssRules.length; i ++) { nameTotal.push(sheetObject.cssRules[i].selectorText); } } else if (typeof sheetObject.rules != 'undefined') { // IE 6/7/8 for (var i = 0; i < sheetObject.rules.length; i ++) { nameTotal.push(sheetObject.rules[i].selectorText); } } return nameTotal; }, // 跨浏览器添加样式规则 addRule : function (sheetObject, ruleName, ruleText, rulePosition) { if (typeof sheetObject.insertRule != 'undefined') { // W3C sheetObject.insertRule(ruleName + ' {' + ruleText + '}', rulePosition); } else if (typeof sheetObject.addRule != 'undefined') { // IE 6/7/8 sheetObject.addRule(ruleName, ruleText, rulePosition); } }, // 跨浏览器移除样式规则 removeRule : function (sheetObject, rulePosition) { if (typeof sheetObject.deleteRule != 'undefined') { // W3C sheetObject.deleteRule(rulePosition); } else if (typeof sheetObject.removeRule != 'undefined') { // IE 6/7/8 sheetObject.removeRule(rulePosition); } }, // IE 6/7/8 事件对象匹配模式 eventMatch : function (event) { event.target = event.srcElement; event.stopPropagation = function () { event.cancelBubble = true; }; event.preventDefault = function () { event.returnValue = false; }; return event; }, // 事件绑定唯一标识符 eventId : 0, // 跨浏览器现代事件绑定: 注册事件 loginEvent : function (elementNode, eventName, method) { if (typeof elementNode.addEventListener != 'undefined') { // W3C elementNode.addEventListener(eventName, method, false); } else if (typeof elementNode.attachEvent != 'undefined') { // IE 6/7/8 if (typeof elementNode.hashTable != 'object') elementNode.hashTable = {}; if (typeof elementNode.hashTable[eventName] != 'object') elementNode.hashTable[eventName] = []; var events = elementNode.hashTable[eventName]; for (var i = 0; i < events.length; i ++) { if (events[i] == method) return false; } events[this.eventId ++] = method; var _this = this; elementNode['on' + eventName] = function () { var event = _this.eventMatch(window.event); for (var i = 0; i < events.length; i ++) { if (typeof events[i] == 'function') events[i].call(this, event); } }; } }, // 跨浏览器现代事件绑定: 注销事件 logoutEvent : function (elementNode, eventName, method) { if (typeof elementNode.removeEventListener != 'undefined') { // W3C elementNode.removeEventListener(eventName, method, false); } else if (typeof elementNode.detachEvent != 'undefined') { // IE 6/7/8 if (typeof elementNode.hashTable == 'object' && typeof elementNode.hashTable[eventName] == 'object') { var events = elementNode.hashTable[eventName]; for (var i = 0; i < events.length; i ++) { if (events[i] == method) delete events[i]; } } } }, // 跨浏览器鼠标滚轮事件 mousewheel : function (elementNode, method, mode) { if (elementNode == window) elementNode = document; // IE 6/7/8 if (typeof elementNode.onmousewheel != 'undefined') { // Not Firefox mode ? this.loginEvent(elementNode, 'mousewheel', method) : this.logoutEvent(elementNode, 'mousewheel', method); } else { // Firefox mode ? this.loginEvent(elementNode, 'DOMMouseScroll', method) : this.logoutEvent(elementNode, 'DOMMouseScroll', method); } }, // 跨浏览器 HTML DOM 加载 htmlLoaded : function (method) { if ((browserDetect.opera && browserDetect.browser.version < 9) || (browserDetect.firefox && browserDetect.browser.version < 3) || (browserDetect.engine.name == 'WebKit' && browserDetect.engine.version < 525)) { // low version W3C var timer = setTimeout(function () { clearTimeout(timer); /interactive|loading|complete/i.test(document.readyState) ? method() : timer = setTimeout(arguments.callee, 1); }, 1); } else if (!browserDetect.ie || browserDetect.browser.version > 8) { // current W3C var _this = this; this.loginEvent(document, 'DOMContentLoaded', function () { _this.logoutEvent(document, 'DOMContentLoaded', arguments.callee); method(); }); } else if (browserDetect.ie && browserDetect.browser.version < 9) { // IE 6/7/8 var timer = setTimeout(function () { try { clearTimeout(timer); document.documentElement.doScroll('top'); method(); } catch (error) { timer = setTimeout(arguments.callee, 1); } }, 1); } }, // 跨浏览器获取元素从属关系(间接从属+直接从属) getContains : function (elementNode, targetNode) { if (typeof targetNode.compareDocumentPosition != 'undefined') { // W3C return targetNode.compareDocumentPosition(elementNode) == 20; } else if (typeof targetNode.contains != 'undefined') { // IE 6/7/8 return targetNode.contains(elementNode); } }, // 跨浏览器获取与设置滚动条x轴 scrollLeft : function (x) { if (browserDetect.engine.name == 'WebKit') { // Chrome / Safari if (typeof x != 'undefined') { // 设置 document.body.scrollLeft = x; } else { // 获取 return document.body.scrollLeft; } } else { // Not Chrome / Safari if (typeof x != 'undefined') { // 设置 document.documentElement.scrollLeft = x; } else { // 获取 return document.documentElement.scrollLeft; } } }, // 跨浏览器获取与控制滚动条y轴 scrollTop : function (y) { if (browserDetect.engine.name == 'WebKit') { // Chrome / Safari if (typeof y != 'undefined') { // 设置 document.body.scrollTop = y; } else { // 获取 return document.body.scrollTop; } } else { // Not Chrome / Safari if (typeof y != 'undefined') { // 设置 document.documentElement.scrollTop = y; } else { // 获取 return document.documentElement.scrollTop; } } }, // 跨浏览器获取网页x轴可视区域尺寸 getWindowWidth : function () { if (typeof window.innerWidth != 'undefined') { // W3C return window.innerWidth; } else if (typeof document.documentElement.clientWidth != 'undefined') { // IE 6/7/8 return document.documentElement.clientWidth; } }, // 跨浏览器获取网页y轴可视区域尺寸 getWindowHeight : function () { if (typeof window.innerHeight != 'undefined') { // W3C return window.innerHeight; } else if (typeof document.documentElement.clientHeight) { // IE 6/7/8 return document.documentElement.clientHeight; } }, // IE 专属: 浏览器外捕获鼠标按下 setCaptureIE : function (elementNode) { if (typeof elementNode.setCapture != 'undefined') { // IE elementNode.setCapture(); } }, // IE 专属: 浏览器外捕获鼠标弹起 releaseCaptureIE : function (elementNode) { if (typeof elementNode.releaseCapture != 'undefined') { // IE elementNode.releaseCapture(); } } };
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =================================================================================== 程序名称: 浏览器嗅探器 BETA 3.0 版本迭代: 无 功能总数: 4 个 功能介绍: 1. 支持浏览器品牌嗅探 2. 支持浏览器名称/版本嗅探 3. 支持浏览器引擎名称/版本嗅探 4. 支持浏览器隶属平台名称/版本嗅探 */ (function () { window.browserDetect = { ie : false, firefox : false, chrome : false, safari : false, opera : false, other : false, browser : { name : '', version : '' }, engine : { name : '', version : '' }, system : { name : '', version : '' } }; var info = navigator.userAgent; var system = navigator.platform; /* Browser pattern */ // check IE var patternIE = /MSIE\s([\d\.]+)/i; var patternHighIE = /rv:([\d\.]+)/i; // check Firefox var patternFirefox = /Firefox\/([\d\.]+)/i; // check Chrome var patternChrome = /Chrome\/([\d\.]+)/i; // check Safari var patternSafari = /Safari\/([\d\.]+)/i; // check Opera var patternOpera = /Opera\/([\d\.]+)/i; /* Engine pattern */ // check Trident var patternTrident = /Trident\/([\d\.]+)/i; // check Gecko var patternGecko = /Gecko\/(\d+)/i; // check WebKit var patternWebKit = /AppleWebKit\/([\d\.]+)/i; // check Presto var patternPresto = /Presto\/([\d\.]+)/i; /* System pattern */ // check Windows var patternWindows = /Windows\sNT\s([\d\.]+)/i; var patternWin = /Win/i; // check Linux var patternLinux = /Linux/i; // check Unix var patternUnix = /X11/i; // check Mac var patternMac = /Mac/i; /* Check browser */ if ((low = patternIE.test(info)) || (high = patternTrident.test(info))) { // IE browserDetect.ie = true; browserDetect.browser.name = 'Internet Explorer'; browserDetect.engine.name = 'Trident'; if (low) { // IE 6 - 10 browserDetect.browser.version = parseFloat(patternIE.exec(info)[1]); } else if (high) { // IE 11+ browserDetect.browser.version = parseFloat(patternHighIE.exec(info)[1]); } if (browserDetect.browser.version >= 6.0) { switch (browserDetect.browser.version) { // engine version case 6.0 : browserDetect.engine.version = 2.0; break; case 7.0 : browserDetect.engine.version = 3.0; break; default : browserDetect.engine.version = parseFloat(patternTrident.exec(info)[1]); } } else { browserDetect.engine.version = '2.0-'; } } else if (patternFirefox.test(info)) { // Firefox browserDetect.firefox = true; browserDetect.browser.name = 'Firefox'; browserDetect.browser.version = parseFloat(patternFirefox.exec(info)[1]); } else if (patternChrome.test(info)) { // Chrome browserDetect.chrome = true; browserDetect.browser.name = 'Chrome'; browserDetect.browser.version = parseFloat(patternChrome.exec(info)[1]); } else if (patternSafari.test(info)) { // Safari browserDetect.safari = true; browserDetect.browser.name = 'Safari'; var patternVersion = /Version\/([\d\.]+)/i; if (patternVersion.test(info)) { // high version browserDetect.browser.version = parseFloat(patternVersion.exec(info)[1]); } else { // low version browserDetect.browser.version = parseFloat(patternSafari.exec(info)[1]); } } else if (patternOpera.test(info)) { // Opera browserDetect.opera = true; browserDetect.browser.name = 'Opera'; var patternVersion = /Version\/([\d\.]+)/i; if (patternVersion.test(info)) { // high version browserDetect.browser.version = parseFloat(patternVersion.exec(info)[1]); } else { // low version browserDetect.browser.version = parseFloat(patternOpera.exec(info)[1]); } } else { // Other browserDetect.other = true; browserDetect.browser.name = 'Other'; browserDetect.browser.version = 'Unknown'; } /* Check Engine */ if (patternTrident.test(info)) { // Trident browserDetect.engine.name = 'Trident'; browserDetect.engine.version = parseFloat(patternTrident.exec(info)[1]); } else if (patternGecko.test(info)) { // Gecko browserDetect.engine.name = 'Gecko'; var patternVersion = /rv:([\d\.]+)/i; if (patternVersion.test(info)) { // high version browserDetect.engine.version = parseFloat(patternVersion.exec(info)[1]); } else { // low version browserDetect.engine.version = parseFloat(patternGecko.exec(info)[1]); } } else if (patternWebKit.test(info)) { // WebKit browserDetect.engine.name = 'WebKit'; browserDetect.engine.version = parseFloat(patternWebKit.exec(info)[1]); } else if (patternPresto.test(info)) { // Presto browserDetect.engine.name = 'Presto'; browserDetect.engine.version = parseFloat(patternPresto.exec(info)[1]); } else { // Other if (browserDetect.other) { browserDetect.engine.name = 'Other'; browserDetect.engine.version = 'Unknown'; } } /* Check System */ if (patternWindows.test(info)) { // Windows browserDetect.system.version = parseFloat(patternWindows.exec(info)[1]); switch (browserDetect.system.version) { case 5.0 : browserDetect.system.name = 'Windows 2000'; break; case 5.1 : browserDetect.system.name = 'Windows XP'; break; case 5.2 : browserDetect.system.name = 'Windows Server 2003 / Windows Server 2003 R2'; break; case 6.0 : browserDetect.system.name = 'Windows Vista / Windows Server 2008'; break; case 6.1 : browserDetect.system.name = 'Windows 7 / Windows Server 2008 R2'; break; case 6.2 : browserDetect.system.name = 'Windows 8 / Windows Server 2012 / Windows Phone 8'; break case 6.3 : browserDetect.system.name = 'Windows 8.1 / Windows Server 2012 R2'; break; default : browserDetect.system.name = 'Windows'; } } else if (patternWin.test(system)) { // Windows(low version browser) browserDetect.system.name = 'Windows'; browserDetect.system.version = 'Unknown'; } else if (patternLinux.test(system)) { // Linux browserDetect.system.name = 'Linux'; browserDetect.system.version = 'Unknown'; } else if (patternUnix.test(system)) { // Unix browserDetect.system.name = 'Unix'; browserDetect.system.version = 'Unknown'; } else if (patternMac.test(system)) { // Mac browserDetect.system.name = 'Mac'; browserDetect.system.version = 'Unknown'; } else { // Other browserDetect.system.name = 'Other'; browserDetect.system.version = 'Unknown'; } })();
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =================================================================================== 特效插件: 元素隐藏 特效插件: 元素显示 特效插件: 鼠标移入移出(下拉菜单) */ $().plugins(['hide', 'show', 'hover'], [function () { this.css('display', 'none'); return this; }, function () { this.css('display', 'block'); return this; }, function (overMethod, outMethod) { this.nodeEvent('mouseover', overMethod); this.nodeEvent('mouseout', outMethod); return this; }]);
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =================================================================================== 特效插件: 元素居中 */ $().plugins('center', function () { var windowRectangle = Base.getWindowRectangle(); var scroll = Base.scroll(); var innerRectangle = null, x = 0, y = 0; for (var i = 0; i < this.elements.length; i ++) { Base.absolute(this.elements[i]); innerRectangle = Base.getInnerRectangle(this.elements[i]); x = (windowRectangle.width - innerRectangle.width) / 2; y = (windowRectangle.height - innerRectangle.height) / 2; if (x < 0) x = 0; if (y < 0) y = 0; $(this.elements[i]).css(['left = ' + (x + scroll.x) + 'px', 'top = ' + (y + scroll.y) + 'px']); } return this; });
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =================================================================================== 特效插件: 遮罩锁屏 特效插件: 清除遮罩 */ $().plugins(['lock', 'unlock'], [function () { var screen = null; if ((screen = $('#screen').firstNode()) != this.info) { if (Base.trim($(Base.comment(screen)).html()) != '') { Base.removeElement(screen); screen = Base.addElement('div', 'id = screen'); } } else { screen = Base.addElement('div', 'id = screen'); } var scroll = Base.scroll(); $('html').css('overflow', 'hidden'); Base.scroll(scroll.x, scroll.y); var windowRectangle = Base.getWindowRectangle(); Base.absolute(screen); $(screen).css([ 'width = ' + windowRectangle.width + 'px', 'height = ' + windowRectangle.height + 'px', 'left = ' + scroll.x + 'px', 'top = ' + scroll.y + 'px', 'backgroundColor = black', 'zIndex = 9998', 'opacity = 0.4', // W3C 'filter = alpha(opacity = 40)' // IE 6/7/8 ]).show(); Base.fixed(); // 修正 Bug return this; }, function () { var screen = null; if ((screen = $('#screen').firstNode()) == this.info) return this; var scroll = Base.scroll(); $('html').css('overflow', 'auto'); Base.scroll(scroll.x, scroll.y); $(screen).hide(); return this; }]);
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =================================================================================== 特效插件: 元素拖拽 */ $().plugins('drag', function () { var selector = null, childs = []; for (var j = 0; j < this.elements.length; j ++) { if (arguments.length != 0) { for (var i = 0; i < arguments.length; i ++) { if ((selector = $(this.elements[j]).selector(arguments[i]).getNodes()) == this.info) continue; childs.push(selector); } } if (childs.length == 0) childs.push(this.elements[j]); var targetNode = this.elements[j]; Base.absolute(targetNode); var down = function (event) { var scroll = Base.scroll(); var outerRectangle = Base.getOuterRectangle(targetNode); var fixedX = (event.clientX + scroll.x) - outerRectangle.left; var fixedY = (event.clientY + scroll.y) - outerRectangle.top; var windowRectangle = Base.getWindowRectangle(); var innerRectangle = Base.getInnerRectangle(targetNode); Tool.setCaptureIE(targetNode); // 兼容 IE Bug var move = function (event) { var x = (event.clientX + scroll.x) - fixedX; var y = (event.clientY + scroll.y) - fixedY; var minX = scroll.x, minY = scroll.y, maxX = 0, maxY = 0; maxX = (windowRectangle.width + scroll.x) - innerRectangle.width; maxY = (windowRectangle.height + scroll.y) - innerRectangle.height; if (x < minX) x = minX; else if (x > maxX) x = maxX; if (y < minY) y = minY; else if (y > maxY) y = maxY; $(targetNode).css(['left = ' + x + 'px', 'top = ' + y + 'px']); }; var up = function () { Tool.releaseCaptureIE(targetNode); // 兼容 IE Bug $(document).nodeEvent('mousemove', move, false); $(document).nodeEvent('mouseup', up, false); }; $(document).nodeEvent('mousemove', move); $(document).nodeEvent('mouseup', up); }; for (var k = 0; k < childs.length; k ++) { if (childs[k] instanceof Array) { // 集群 for (var l = 0; l < childs[k].length; l ++) { $(childs[k][l]).nodeEvent('mousedown', down); } } else { // 单一 $(childs[k]).nodeEvent('mousedown', down); } } } return this; });
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =================================================================================== 特效插件: 禁止元素溢出可视区 */ $().plugins('overflow', function () { var windowRectangle = Base.getWindowRectangle(); var scroll = Base.scroll(); var minX = scroll.x, minY = scroll.y, maxX = 0, maxY = 0, x = 0, y = 0; var innerRectangle = null, outerRectangle = null; for (var i = 0; i < this.elements.length; i ++) { Base.absolute(this.elements[i]); innerRectangle = Base.getInnerRectangle(this.elements[i]); outerRectangle = Base.getOuterRectangle(this.elements[i]); x = outerRectangle.left; y = outerRectangle.top; maxX = (windowRectangle.width + scroll.x) - innerRectangle.width; maxY = (windowRectangle.height + scroll.y) - innerRectangle.height; if (x < minX) x = minX; else if (x > maxX) x = maxX; if (y < minY) y = minY; else if (y > maxY) y = maxY; $(this.elements[i]).css(['left = ' + x + 'px', 'top = ' + y + 'px']); } return this; });
关于 BETA 3.0 测试版核心源码与实例演示的获取请移动至官网下载!
感谢大家积极评测给予意见!
CNBlogs 博客:http://www.cnblogs.com/shibuyi/
CSDN 博客:http://blog.csdn.net/louis_shi/
ITeye 博客:http://shibuyi.iteye.com/
相关推荐
要充分利用JavaScript封装库BETA 4.0版,开发者应仔细阅读官方文档,了解新特性和API变更,及时更新项目中的依赖,并参与社区讨论,以便更好地适应和利用这一新版本带来的优势。同时,也要注意对新版本的兼容性测试...
在“百度地图JavaScript API离线版3.0.zip”这个压缩包中,包含了实现离线地图功能所需的各种文件和资源。主要知识点包括以下几个方面: 1. JavaScript API基础知识:这是百度地图的核心,它提供了丰富的JavaScript...
M7lrvCMS 3.0是该系统的一个重要版本,其“Beta”标签表明这是一款测试版产品,可能包含了开发者对原有系统的改进和新功能的尝试。在实际应用中,测试版通常会面临一些稳定性问题,但同时也意味着它可能会带来更多的...
【Web Calendar ver 3.0 网页日历控件!】 Web Calendar ver 3.0 是一款专为网页设计而开发的日历组件,它允许用户在网页上方便地查看、选择和管理日期。这款控件适用于各种类型的网页应用,如在线预订系统、事件...
JS校验框架3.0测试版是一款针对JavaScript编程的验证工具,旨在提高Web应用的用户体验和数据准确性。通过这款框架,开发者能够轻松实现对用户输入数据的有效性检查,确保前端数据在提交到服务器前满足预设的规则,...
JavaScript 和 Spring 3.0 是两个在 IT 领域非常重要的技术,它们分别在前端和后端开发中扮演着核心角色。JavaScript 是一种广泛应用于客户端网页开发的脚本语言,而 Spring 3.0 是 Java 企业级应用开发框架的代表性...
在移动应用开发领域,AppFramework 3.0 Beta 的发布无疑为开发者带来了一个全新的工具和框架选择。这个测试版本标志着该框架在功能和性能上的进一步提升,为构建高效、跨平台的移动应用提供了坚实的基础。接下来,...
JiveJdon3.0版本的发布,标志着该平台在功能、性能和用户体验方面又迈出了重要的一步。 在JiveJdon3.0版本中,我们可以期待以下一些关键改进和新特性: 1. **性能优化**:JiveJdon3.0可能会对数据库查询、缓存机制...
在稳定性方面,CKEditor 3.0 对各种浏览器进行了广泛的兼容性测试,确保在Firefox、Chrome、Safari、Internet Explorer等主流浏览器下都能正常工作。对于Web开发者而言,这意味着他们无需担心CKEditor在不同平台上的...
网站抓取精灵V3.0正式版是一款专用于网页数据抓取和分析的软件工具,其核心功能是帮助用户高效地从互联网上提取所需信息。该版本为V3.0,意味着它经过了多次迭代和优化,具备更稳定、更强大的性能。在使用这款软件时...
这个“ActionScript3.0中文版”资源可能是一个详细的教程或参考手册,旨在帮助中文用户理解和掌握这一技术。 在ActionScript3.0中,最显著的变化是引入了强类型系统,这意味着变量必须在声明时指定其数据类型,如...
在压缩包内,"克米X3清新蓝色风格 X3.0商业版_GBK"很可能包含了以下内容:安装文件、主题样式文件(CSS)、图片资源、JavaScript脚本、数据库配置文件以及可能的文档和示例数据。用户可以通过安装指南来部署这个主题...
在AXURE这个强大的原型设计工具中,Bootstrap元件库3.0是一个宝贵的资源,特别适合产品经理用来快速创建交互原型。这个元件库集成了Bootstrap 3.0的样式和组件,使得非编码背景的产品经理也能轻松地设计出与现代网页...
"jspftbbs3.0个人版"是一个基于Java技术构建的开源论坛系统,专为个人用户和小型社区设计。这个论坛平台集成了多种功能,旨在提供一个互动性强、易于管理的在线交流环境。 首先,从后台管理角度来看,"jspftbbs3.0...
最新版Javascript库!支持Cookie、Ajax、BlockUI and so on.
:sparkles: 拖放编码环境样板它旨在成为一个存储库,使基于Scratch-3.0组件的GUI的使用和部署变得简单! 它可能适用于: 开发不符合当前扩展限制的原型Scratch 3.0扩展尝试对Scratch 3.0 GUI进行调整快速部署Scratch...
9. **JavaScript框架和库**:如React、Vue、Angular等,它们提供了丰富的功能和工具,帮助开发者快速构建复杂Web应用。 以上就是关于CSS3.0和JavaScript的基础知识要点,通过深入理解和实践,可以提升前端开发能力...
JavaScript与Flash AS3.0之间的交互是Web开发中的一个重要领域,尤其在富互联网应用程序(RIA)的开发中。这个“javascript_to_flash_as3.0.rar”压缩包可能包含了一组教程、示例代码或者工具,旨在帮助开发者理解并...