【JavaScript 封装库】BETA 1.0 测试版发布!
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =================================================================================================== 程序名称: JavaScript 封装库 BETA 1.0 版 迭代版本: Prototype 功能总数: 44 个 新增总数: 30 个 删除总数: 0 个 追加功能: 1. 元素隐藏与显示 2. 表单 value 属性获取 3. 实现 mouseover / mouseout / mousemove / mousedown / mouseup / scroll / resize / load / mousewheel 等事件 4. 实现 元素居中 / 下拉菜单 / 元素拖拽 / 遮罩锁屏 / 禁止溢出 等特效 ... 优化功能: 1. 实现元素节点直传 2. 完善 id 多节点获取 3. 完善 css 设置, 支持滤镜 4. 完善 class 选择器, 支持多选择器添加和移除 5. 完善 rule 样式规则, 支持多规则的添加和移除 6. 实现二级子方法 7. 实现代码分层至 Tool 工具库 ... 删除功能: 无 */ // 实例化封装库 function $(_this) { return new Base(_this); } // 封装库构造方法 function Base(_this) { this.elements = []; // 初始化元素列表 if (typeof _this == ‘object‘) this.elements.push(_this); } // 获取元素节点 Base.prototype.getNodes = function () { if (this.elements.length == 0) return ‘暂无任何元素节点‘; if (this.elements.length == 1) return this.elements[0]; return this.elements; }; // 获取 id 元素节点 Base.prototype.getId = function (id) { if (id instanceof Array) { // 集群 for (var i = 0; i < id.length; i ++) { this.getId(id[i]); } } else { // 单个 var node = {}; node = document.getElementById(id); if (!node) return this; for (var i = 0; i < this.elements.length; i ++) { if (this.elements[i] == node) return this; } this.elements.push(node); } return this; }; // 获取 name 元素节点 Base.prototype.getName = function (name, positionId) { var nodes = {}; if (typeof positionId != ‘undefined‘) { // 局部 nodes = $().getTagName(‘*‘, positionId).getNodes(); } else { // 全局 nodes = document.getElementsByName(name); } for (var i = 0; i < nodes.length; i ++) { if (nodes[i].name == name) this.elements.push(nodes[i]); } return this; }; // 获取 tagName 元素节点 Base.prototype.getTagName = function (tagName, positionId) { var nodes = {}; if (typeof positionId != ‘undefined‘) { // 局部 nodes = $().getId(positionId).getNodes().getElementsByTagName(tagName); } else { // 全局 nodes = document.getElementsByTagName(tagName); } for (var i = 0; i < nodes.length; i ++) { this.elements.push(nodes[i]); } return this; }; // 获取 class 元素节点 Base.prototype.getClass = function (className, positionId) { var nodes = {}, results = []; if (typeof positionId != ‘undefined‘) { // 局部 nodes = $().getTagName(‘*‘, positionId); results = $().getClass.hasClass.call(nodes, className); } else { // 全局 nodes = $().getTagName(‘*‘); results = $().getClass.hasClass.call(nodes, className); } nodes = nodes.getNodes(); for (var i = 0; i < results.length; i ++) { if (results[i]) this.elements.push(nodes[i]); } return this; }; // getClass 附属方法: 检测 class Base.prototype.getClass.hasClass = function (className) { var results = []; for (var i = 0; i < this.elements.length; i ++) { results.push((new RegExp(‘(^|\\s+)‘ + className + ‘(\\s+|$)‘)).test(this.elements[i].className)); } return results; }; // 获取与设置 innerHTML(双标记) Base.prototype.html = function (text) { if (typeof text != ‘undefined‘) { // 设置 for (var i = 0; i < this.elements.length; i ++) { this.elements[i].innerHTML = text; } } else { // 获取 var html = []; for (var i = 0; i < this.elements.length; i ++) { html.push(this.elements[i].innerHTML); } if (html.length == 1) return html[0]; return html; } return this; }; // 获取与设置 value(表单) Base.prototype.value = function (text) { if (typeof text != ‘undefined‘) { // 设置 for (var i = 0; i < this.elements.length; i ++) { if (typeof this.elements[i].value != ‘undefined‘) this.elements[i].value = text; } } else { // 获取 var value = []; for (var i = 0; i < this.elements.length; i ++) { if (typeof this.elements[i].value != ‘undefined‘) value.push(this.elements[i].value); } if (value.length == 1) return value[0]; return value; } return this; }; // 获取与设置 CSS Base.prototype.css = function (cssKey, cssValue) { if (typeof cssValue != ‘undefined‘ || cssKey instanceof Array) { // 设置 if (cssKey instanceof Array) { // 集群 var _cssKey = ‘‘, _cssValue = ‘‘, pattern = /^\s*([a-z]+)\s*=\s*([\w#=\s\(\.\)\-\‘\"\/\\]+)\s*$/i; for (var i = 0; i < cssKey.length; i ++) { if (pattern.test(cssKey[i])) { _cssKey = pattern.exec(cssKey[i])[1]; _cssValue = pattern.exec(cssKey[i])[2]; for (var j = 0; j < this.elements.length; j ++) { this.elements[j].style[_cssKey] = _cssValue; } } } } else { // 单个 for (var i = 0; i < this.elements.length; i ++) { this.elements[i].style[cssKey] = cssValue; } } } else { // 获取 var css = [], _cssValue = ‘‘; for (var i = 0; i < this.elements.length; i ++) { _cssValue = Tool.getStyle(this.elements[i], cssKey); if (typeof _cssValue != ‘undefined‘) css.push(_cssValue); } if (css.length == 1) return css[0]; return css; } 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 results = this.getClass.hasClass.call(this, className); var space = ‘‘; for (var i = 0; i < results.length; i ++) { if (this.elements[i].className != ‘‘) space = ‘ ‘; if (!results[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 results = this.getClass.hasClass.call(this, className); for (var i = 0; i < results.length; i ++) { if (results[i]) this.elements[i].className = this.elements[i].className.replace(new RegExp(‘(^|\\s)‘ + className + ‘(\\s|$)‘), ‘‘); } } return this; }; // 添加 rule Base.prototype.addRule = function (ruleName, ruleText, positionIndex, sheetIndex) { if (ruleName instanceof Array) { // 集群 if (!ruleText instanceof Array || ruleName.length != ruleText.length) return this; for (var i = 0; i < ruleName.length; i ++) { this.addRule(ruleName[i], ruleText[i], positionIndex, sheetIndex); } } else { // 单个 var rule = this.addRule.checkRule(positionIndex, sheetIndex); if (typeof rule.sheetObject != ‘undefined‘) { if ((positionIndexMax = Tool.getRules(rule.sheetObject).length) < rule.positionIndex) rule.positionIndex = positionIndexMax; Tool.addRule(rule.sheetObject, ruleName, ruleText, rule.positionIndex); } } return this; }; // addRule 附属方法: rule 容错处理 Base.prototype.addRule.checkRule = function (positionIndex, sheetIndex, positionLength) { if (typeof positionIndex == ‘undefined‘ || isNaN(positionIndex)) positionIndex = 0; if (typeof sheetIndex == ‘undefined‘ || isNaN(sheetIndex)) sheetIndex = 0; if (typeof positionLength == ‘undefined‘ || isNaN(positionLength)) positionLength = 1; return { positionIndex : positionIndex, positionLength : positionLength, sheetObject : document.styleSheets[sheetIndex] } }; // 删除 rule Base.prototype.removeRule = function (positionIndex, positionLength, sheetIndex) { var rule = this.addRule.checkRule(positionIndex, sheetIndex, positionLength); if ((positionIndexMax = Tool.getRules(rule.sheetObject).length) <= rule.positionIndex || (positionIndexMax - rule.positionIndex) < rule.positionLength) return this; for (var i = 0; i < rule.positionLength; i ++) { Tool.removeRule(rule.sheetObject, rule.positionIndex); } return this; }; // 显示元素 Base.prototype.show = function () { for (var i = 0; i < this.elements.length; i ++) { $(this.elements[i]).css(‘display‘, ‘block‘); } return this; }; // 隐藏元素 Base.prototype.hide = function () { for (var i = 0; i < this.elements.length; i ++) { $(this.elements[i]).css(‘display‘, ‘none‘); } return this; }; // 鼠标 click 事件 Base.prototype.click = function (method) { if (method instanceof Function) { for (var i = 0; i < this.elements.length; i ++) { this.elements[i].onclick = function (eventObject) { method.call(this, Tool.getEvent(eventObject)); }; } } return this; }; // 鼠标 mouseover 事件 Base.prototype.mouseover = function (method) { if (method instanceof Function) { for (var i = 0; i < this.elements.length; i ++) { this.elements[i].onmouseover = function (eventObject) { method.call(this, Tool.getEvent(eventObject)); }; } } return this; }; // 鼠标 mouseout 事件 Base.prototype.mouseout = function (method) { if (method instanceof Function) { for (var i = 0; i < this.elements.length; i ++) { this.elements[i].onmouseout = function (eventObject) { method.call(this, Tool.getEvent(eventObject)); }; } } return this; }; // 鼠标 mousedown 事件 Base.prototype.mousedown = function (method) { if (method instanceof Function) { for (var i = 0; i < this.elements.length; i ++) { this.elements[i].onmousedown = function (eventObject) { method.call(this, Tool.getEvent(eventObject)); }; } } return this; }; // 鼠标 mousemove 事件 Base.prototype.mousemove = function (method) { if (method instanceof Function) { for (var i = 0; i < this.elements.length; i ++) { this.elements[i].onmousemove = function (eventObject) { method.call(this, Tool.getEvent(eventObject)); }; } } return this; }; // 鼠标 mouseup 事件 Base.prototype.mouseup = function (method) { if (method instanceof Function) { for (var i = 0; i < this.elements.length; i ++) { this.elements[i].onmouseup = function (eventObject) { method.call(this, Tool.getEvent(eventObject)); }; } } return this; }; // 鼠标 mousewheel 事件 Base.prototype.mousewheel = function (method) { if (method instanceof Function) { for (var i = 0; i < this.elements.length; i ++) { Tool.mousewheelEvent(this.elements[i], method); } } return this; }; // 元素 load 事件 Base.prototype.load = function (method) { if (method instanceof Function) { for (var i = 0; i < this.elements.length; i ++) { this.elements[i].onload = function (eventObject) { method.call(this, Tool.getEvent(eventObject)); }; } } return this; }; // 元素 scroll 事件 Base.prototype.scroll = function (method) { if (method instanceof Function) { for (var i = 0; i < this.elements.length; i ++) { this.elements[i].onscroll = function (eventObject) { method.call(this, Tool.getEvent(eventObject)); }; } } return this; }; // 窗口 resize 事件 Base.prototype.resize = function (method) { if (method instanceof Function) { window.onresize = function (eventObject) { method.call(this, Tool.getEvent(eventObject)); }; } return this; }; // 鼠标 hover 特效 Base.prototype.hover = function (overMethod, outMethod) { this.mouseover(overMethod); this.mouseout(outMethod); return this; }; // 元素居中显示特效 Base.prototype.center = function () { var innerRectangle = Tool.getInnerRectangle(); var rectangle = {}, fixedX = 0, fixedY = 0, minX = 0, minY = 0, maxX = 0, maxY = 0; for (var i = 0; i < this.elements.length; i ++) { this.center.absolutePosition(this.elements[i]); rectangle = this.center.getRectangle(this.elements[i]); maxX = innerRectangle.innerWidth - rectangle.width; maxY = innerRectangle.innerHeight - rectangle.height; fixedX = maxX / 2; fixedY = maxY / 2; if (fixedX < minX) { fixedX = minX; } else if (fixedX > maxX) { fixedX = maxX; } if (fixedY < minY) { fixedY = minY; } else if (fixedY > maxY) { fixedY = maxY; } $(this.elements[i]).css([‘left = ‘ + fixedX + ‘px‘, ‘top = ‘ + fixedY + ‘px‘]); } return this; }; // center 隶属方法: 获取元素长度 Base.prototype.center.getRectangle = function (elementNode) { var _this = $(elementNode); var width = _this.css(‘width‘); var height = _this.css(‘height‘); var display = _this.css(‘display‘); if (display != ‘none‘ || width == ‘auto‘ || height == ‘auto‘) { _this.show(); width = elementNode.offsetWidth; height = elementNode.offsetHeight; } width = this.split(width, ‘px‘); height = this.split(height, ‘px‘); return { width : width, height : height } }; // center 隶属方法: 裁剪字符串 Base.prototype.center.split = function (string, pattern) { if (typeof string == ‘string‘) { if (string.indexOf(pattern) != -1) string = string.replace(pattern, ‘‘); } return string; }; // center 隶属方法: 元素设置绝对定位 Base.prototype.center.absolutePosition = function (elementNode) { if ($(elementNode).css(‘position‘) != ‘absolute‘) { $(elementNode).css([‘position = absolute‘, ‘left = 0‘, ‘top = 0‘]); } }; // 屏幕遮罩特效: 锁屏 Base.prototype.lock = function () { var screenLock = $().getId(‘screen_lock‘).getNodes(); // 获取锁屏节点 // 锁屏节点智能生成 if (typeof screenLock == ‘object‘) { // 存在 this.lock.comment(screenLock); // 清除注释 if (this.lock.trim($(screenLock).html()).length != 0) { this.lock.deleteElement(‘id = screen_lock‘); screenLock = this.lock.createElement(‘div‘, ‘id = screen_lock‘); } } else { // 不存在 screenLock = this.lock.createElement(‘div‘, ‘id = screen_lock‘); // 创建锁屏节点 } $(document.documentElement).css(‘overflow‘, ‘hidden‘); Tool.scrollInitialization(); var innerRectangle = Tool.getInnerRectangle(); this.center.absolutePosition(screenLock); var _this = $(screenLock); // 锁屏样式 _this.css([ ‘width = ‘ + innerRectangle.innerWidth + ‘px‘, ‘height = ‘ + innerRectangle.innerHeight +‘px‘, ‘backgroundColor = black‘, ‘opacity = 0.4‘, // W3C 透明效果 ‘filter = alpha(opacity = 40)‘, // IE 6/7/8 透明效果 ‘zIndex = 10‘ ]).show(); // IE 6/7/8 与 Chrome 鼠标滚轮 BUG 处理 _this.mousedown(function () { // 禁用 IE 6/7/8 鼠标默认行为 _this.mousemove(function (eventObject) { Tool.preventEventDefault(eventObject); }); _this.mouseup(function () { _this.mousedown(function () { return null; }); _this.mouseup(function () { return null; }); }); }); _this.mousewheel(function (eventObject) { // Chrome 禁用鼠标滚轮 Tool.preventEventDefault(eventObject); }); return this; }; // 屏幕遮罩特效: 销锁 Base.prototype.unlock = function () { var screenLock = $().getId(‘screen_lock‘).getNodes(); if (typeof screenLock == ‘object‘) { $(screenLock).hide(); $(document.documentElement).css(‘overflow‘, ‘auto‘); } return this; }; // lock 隶属方法: 匹配元素属性 Base.prototype.lock.matchAttribute = function (pattern, attribute, elementNode) { if (pattern.test(attribute)) { var attributeKey = pattern.exec(attribute)[1]; var attributeValue = pattern.exec(attribute)[2]; if (attributeKey == ‘class‘) attributeKey = ‘className‘; if (typeof elementNode == ‘object‘) elementNode[attributeKey] = attributeValue; return { attributeKey : attributeKey, attributeValue : attributeValue } } }; // lock 隶属方法: 创建元素 Base.prototype.lock.createElement = function (elementName, attribute, html, parentElement) { if (typeof parentElement == ‘undefined‘) parentElement = document.body; var elementNode = document.createElement(elementName); if (typeof attribute != ‘undefined‘) { var pattern = /^\s*([a-z]+)\s*=\s*([\w\-\s\.\\\/\=\(\)]+)\s*$/i; if (attribute instanceof Array) { for (var i = 0; i < attribute.length; i ++) { this.matchAttribute(pattern, attribute[i], elementNode); } } else { this.matchAttribute(pattern, attribute, elementNode); } } if (typeof html == ‘string‘) { $(elementNode).html(html); } parentElement.appendChild(elementNode); return elementNode; }; // lock 隶属方法: 删除元素 Base.prototype.lock.deleteElement = function (attribute) { if (typeof attribute != ‘undefined‘) { if (attribute instanceof Array) { for (var i = 0; i < attribute.length; i ++) { this.deleteElement(attribute[i]); } } else { var pattern = /^\s*(id|class|className|name)\s*=\s*([\w\-\s\.\\\/\=\(\)]+)\s*$/i; var nodes = [], parentElement = {}; if (pattern.test(attribute)) { arrtibuteObject = $().lock.matchAttribute(pattern, attribute); switch (arrtibuteObject.attributeKey) { case ‘id‘ : nodes = $().getId(arrtibuteObject.attributeValue).getNodes(); break; case ‘name‘: nodes = $().getName(arrtibuteObject.attributeValue).getNodes(); break; case ‘className‘ : nodes = $().getClass(arrtibuteObject.attributeValue).getNodes(); break; } } else { // tagName nodes = $().getTagName(attribute).getNodes(); } if (nodes instanceof Array) { for (var i = 0; i < nodes.length; i ++) { parentElement = nodes[i].parentNode; if (typeof parentElement == ‘undefined‘) continue; parentElement.removeChild(nodes[i]); } } else { parentElement = nodes.parentNode; if (typeof parentElement == ‘undefined‘) return false; parentElement.removeChild(nodes); } } } }; // lock 隶属方法: 清除字符串首位空格 Base.prototype.lock.trim = function (string) { var leftPattern = /^([\s ]+)/; var rightPattern = /([\s ]+)$/; if (leftPattern.test(string)) string = string.replace(leftPattern, ‘‘); if (rightPattern.test(string)) string = string.replace(rightPattern, ‘‘); return string; }; // lock 隶属方法: 清除元素内部注释 Base.prototype.lock.comment = function (elementNode) { if (elementNode.id != ‘‘) var nodes = $().getTagName(‘!‘, elementNode.id).getNodes(); // IE 6/7/8 if (typeof nodes != ‘object‘) nodes = elementNode.childNodes; // W3C for (var i = 0; i < nodes.length; i ++) { if (nodes[i].nodeType == 8) elementNode.removeChild(nodes[i]); } return elementNode; }; // lock 隶属方法: 清除元素内部空白文本节点 Base.prototype.lock.spaceNode = function (elementNode) { var nodes = elementNode.childNodes; for (var i = 0; i < nodes.length; i ++) { if (nodes[i].nodeType == 3 && /^([ \s]+)$/.test(nodes[i].nodeValue)) elementNode.removeChild(nodes[i]); } return elementNode; }; // 元素拖拽特效 Base.prototype.drag = function (mode) { if (typeof mode == ‘undefined‘) mode = true; this.mousedown(function (eventObject) { // 拖拽模式选择: true 拖拽元素自身、false 拖拽父级元素 var _this = mode ? this : this.parentNode; $().lock.comment(_this); if ($().lock.trim($(_this).html()).length == 0) Tool.preventEventDefault(eventObject); // 兼容 Firefox 低版本拖拽 BUG // 设置绝对定位 $().center.absolutePosition(_this); // 捕获鼠标当前坐标 var mouseX = eventObject.clientX; var mouseY = eventObject.clientY; // 获取元素外边距 var outerRectangle = $().drag.getOuterRectangle(_this); // 获取固定距离 var fixedX = mouseX - outerRectangle.outerX; var fixedY = mouseY - outerRectangle.outerY; var innerRectangle = Tool.getInnerRectangle(); var rectangle = $().center.getRectangle(_this); // 设置容错距离 var minX = 0, minY = 0, maxX = 0, maxY = 0; maxX = innerRectangle.innerWidth - rectangle.width; maxY = innerRectangle.innerHeight - rectangle.height; // 兼容 IE 6/7/8 拖拽 BUG Tool.setCaptureIE(_this); $(document).mousemove(function (eventObject) { // 捕获鼠标当前坐标 mouseX = eventObject.clientX; mouseY = eventObject.clientY; // 获取元素位置 var moveX = mouseX - fixedX; var moveY = mouseY - fixedY; if (moveX < minX) { moveX = minX; } else if (moveX > maxX) { moveX = maxX; } if (moveY < minY) { moveY = minY; } else if (moveY > maxY) { moveY = maxY; } $(_this).css([‘left = ‘ + moveX + ‘px‘, ‘top = ‘ + moveY + ‘px‘]); }); $(document).mouseup(function () { // 兼容 IE 6/7/8 拖拽 BUG Tool.releaseCaptureIE(_this); $(document).mousemove(function () { return null; }); return null; }); }); return this; }; // drag 隶属方法: 获取元素外边距 Base.prototype.drag.getOuterRectangle = function (elementNode) { var _this = $(elementNode); var outerX = _this.css(‘left‘); var outerY = _this.css(‘top‘); var display = _this.css(‘display‘); if (display != ‘none‘ || outerX == ‘auto‘ || outerY == ‘auto‘) { _this.show(); outerX = elementNode.offsetLeft; outerY = elementNode.offsetTop; } outerX = _this.center.split(outerX, ‘px‘); outerY = _this.center.split(outerY, ‘px‘); return { outerX : outerX, outerY : outerY } }; // 防止元素溢出 Base.prototype.overflow = function () { var innerRectangle = Tool.getInnerRectangle(); var outerRectangle = {}, rectangle = {}, minX = 0, minY = 0, maxX = 0, maxY = 0; for (var i = 0; i < this.elements.length; i ++) { this.center.absolutePosition(this.elements[i]); outerRectangle = this.drag.getOuterRectangle(this.elements[i]); rectangle = this.center.getRectangle(this.elements[i]); maxX = innerRectangle.innerWidth - rectangle.width; maxY = innerRectangle.innerHeight - rectangle.height; if (outerRectangle.outerX < minX) { outerRectangle.outerX = minX; } else if (outerRectangle.outerX > maxX) { outerRectangle.outerX = maxX; } if (outerRectangle.outerY < minY) { outerRectangle.outerY = minY; } else if (outerRectangle.outerY > maxY) { outerRectangle.outerY = maxY; } $(this.elements[i]).css([‘left = ‘ + outerRectangle.outerX + ‘px‘, ‘top = ‘ + outerRectangle.outerY + ‘px‘]); } return this; };
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =================================================================================================== 程序名称: JavaScript 工具库(跨浏览器兼容) BETA 1.0 版 迭代版本: 无 功能总数: 17 个 功能介绍: 1. 实现获取计算样式 2. 样式规则添加和移除 3. 获取事件对象 event 4. 取消事件默认行为与冒泡处理 5. 现代事件绑定添加和移除 ... */ var Tool = { // 跨浏览器获取计算后的样式 getStyle : function (elementNode, cssKey) { 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]; } }, // 跨浏览器添加 rule addRule : function (sheetObject, ruleName, ruleText, positionIndex) { if (typeof sheetObject.insertRule != ‘undefined‘) { // W3C sheetObject.insertRule(ruleName + ‘ {‘ + ruleText + ‘}‘, positionIndex); } else if (typeof sheetObject.addRule != ‘undefined‘) { // IE 6/7/8 sheetObject.addRule(ruleName, ruleText, positionIndex); } }, // 跨浏览器移除 rule removeRule : function (sheetObject, positionIndex) { if (typeof sheetObject.deleteRule != ‘undefined‘) { // W3C sheetObject.deleteRule(positionIndex); } else if (typeof sheetObject.removeRule != ‘undefined‘) { // IE 6/7/8 sheetObject.removeRule(positionIndex); } }, // 跨浏览器获取rule集合 getRules : function (sheetObject) { if (typeof sheetObject.cssRules != ‘undefined‘) { // W3C return sheetObject.cssRules; } else if (typeof sheetObject.rules != ‘udnefined‘) { // IE 6/7/8 return sheetObject.rules; } }, // 数组排序 sort : function () { return { 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; } } } }, // 跨浏览器获取 event 对象 getEvent : function (eventObject) { if (typeof eventObject == ‘object‘) return eventObject; // W3C return window.event; // IE 6/7/8 }, // 跨浏览器获取事件绑定所属元素引用 getEventTarget : function (eventObject) { eventObject = this.getEvent(eventObject); if (typeof eventObject.target != ‘undefined‘) { // W3C return eventObject.target; } else if (typeof eventObject.srcElement != ‘undefined‘) { // IE 6/7/8 return eventObject.srcElement; } }, // 跨浏览器取消事件默认行为 preventEventDefault : function (eventObject) { eventObject = this.getEvent(eventObject); if (typeof eventObject.cancelable != ‘undefined‘ && typeof eventObject.preventDefault != ‘undefined‘) { // W3C if (eventObject.cancelable) eventObject.preventDefault(); } else { // IE 6/7/8 eventObject.returnValue = false; } }, // 跨浏览器取消事件冒泡 cancelEventPropagation : function (eventObject) { eventObject = this.getEvent(eventObject); if (typeof eventObject.bubbles != ‘undefined‘ && typeof eventObject.stopPropagation != ‘undefined‘) { // W3C if (eventObject.bubbles) eventObject.stopPropagation(); } else if (typeof eventObject.cancelBubble != ‘undefined‘) { // IE 6/7/8 eventObject.cancelBubble = true; } }, // 事件名称容错处理 checkEventName : function (eventName) { if (typeof eventName == ‘string‘ && eventName.indexOf(‘on‘) == 0) eventName = eventName.substring(2, eventName.length); return eventName; }, // 跨浏览器现代事件绑定: 注册事件 loginEvent : function (elementNode, eventName, method) { eventName = this.checkEventName(eventName); if (typeof elementNode.addEventListener != ‘undefined‘) { // W3C elementNode.addEventListener(eventName, method, false); } else if (typeof elementNode.attachEvent != ‘undefined‘) { // IE 6/7/8 elementNode.attachEvent(‘on‘ + eventName, method); } }, // 款浏览器现代事件绑定: 注销事件 logoutEvent : function (elementNode, eventName, method) { eventName = this.checkEventName(eventName); if (typeof elementNode.removeEventListener != ‘undefined‘) { // W3C elementNode.removeEventListener(eventName, method, false); } else if (typeof elementNode.detachEvent != ‘undefined‘) { // IE 6/7/8 elementNode.detachEvent(‘on‘ + eventName, method); } }, // 跨浏览器绑定鼠标滚轮事件 mousewheelEvent : function (elementNode, method) { if (typeof elementNode.onmousewheel != ‘undefined‘) { // 非 Firefox elementNode.onmousewheel = function (eventObject) { method.call(this, Tool.getEvent(eventObject)); }; } else { // Firefox this.loginEvent(elementNode, ‘DOMMouseScroll‘, method); } }, // 跨浏览器获取网页可视区长度 getInnerRectangle : function () { var width = 0; height = 0; if (typeof window.innerWidth != ‘undefined‘ && typeof window.innerHeight != ‘undefined‘) { // W3C width = window.innerWidth; height = window.innerHeight; } else { // IE 6/7/8 width = document.documentElement.clientWidth; height = document.documentElement.clientHeight; } return { innerWidth : width, innerHeight : height } }, // 跨浏览器滚动条重置 scrollInitialization : function () { document.documentElement.scrollTop = 0; // 非 Chrome / Safari document.documentElement.scrollLeft = 0; // 非 Chrome / Safari document.body.scrollTop = 0; // Chrome / Safari document.body.scrollLeft = 0; // Chrome / Safari }, // IE 浏览器独占: 监听鼠标点击 setCaptureIE : function (elementNode) { if (typeof elementNode.setCapture != ‘undefined‘) { elementNode.setCapture(); } }, // IE 浏览器独占: 监听释放鼠标 releaseCaptureIE : function (elementNode) { if (typeof elementNode.releaseCapture != ‘undefined‘) { elementNode.releaseCapture(); } } };
关于 BETA 1.0 原型版核心源码与实例演示的获取请移动至官网下载!
感谢大家积极评测给予意见!
CNBlogs 博客:http://www.cnblogs.com/shibuyi/
CSDN 博客:http://blog.csdn.net/louis_shi/
ITeye 博客:http://shibuyi.iteye.com/
本文出自 “石不易的心情博客” 博客,请务必保留此出处http://shibuyi.blog.51cto.com/9460768/1562089
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。