1 /* 2 源码作者: 石不易(Louis Shi) 3 联系方式: http://www.shibuyi.net 4 =================================================================================================== 5 程序名称: JavaScript 封装库 BETA 4.0 版 6 迭代版本: BETA 3.0 7 插件总数: 12 个 8 库方法数: 26 个 9 功能总数: 67 个 10 新增总数: 22 个 11 删除总数: 3 个 12 追加功能: 13 1. 进一步完善“获取元素节点”的功能, 包括: 前后位兄弟节点、子节点、父节点 14 2. 新增“获取元素节点尺寸”, 可直接输出元素的宽高和坐标 15 3. 库方法: 新增对 CSS 样式原生风格的解析, 支持 JavaScript 与 CSS 的两种; 例如: fontSize 也可写作 font-size 16 4. 插件: 新增“元素动画”特效, 支持常规的宽高运动、坐标运动和透明度渐变; 另外也可自定义, 但必须要有对应像素单位的样式, 否则无效 17 5. 插件: 新增“事件切换器”特效, 支持任意方法数, 点击元素后迭代切换事件 18 ... 19 优化功能: 20 1. 优化了 CSS 解析功能, 支持原生和 JavaScript 两种写法, 可任意混合搭配 21 2. 优化了“移除样式规则”, 从原先的索引下标移除转变为直接输入对应选择器名称即可删除, 但 W3C 与 IE 6/7/8 在选择器多重叠加上的解释并不一致, 需谨慎使用 22 2. 优化了“自动加载插件”, 载入插件时直接将插件方法名变量传入即可, 但不支持匿名函数赋值给变量后在传入的写法 23 4. 插件: 优化了“元素居中”特效, 现已支持 3 种居中方式: 水平居中、垂直居中、水平+垂直居中 24 ... 25 删除功能: 26 1. 库方法: 删除了“初始化滚动条”方法, 今后直接通过“滚动条”方法设置即可 27 2. 库方法: 删除了“CSS 处理”方法, 与“添加元素节点属性”结合, 无需再分离调用 28 3. 库方法: 删除了“class 处理”方法, 与“添加元素节点属性”结合, 无需再分离调用 29 */ 30 31 // 实例化基础库 32 var $ = function () { 33 var nodes = []; 34 for (var i = 0; i < arguments.length; i ++) { 35 nodes.push(arguments[i]); 36 } 37 return new Base(nodes); 38 }; 39 40 // 基础库构造方法 41 var Base = function (nodes) { 42 this.info = '无节点'; 43 this.elements = []; 44 if (nodes instanceof Array) { 45 var node = null, patternSpace = /[\s\t ]+/, elements = [], elementsList = []; 46 for (var i = 0; i < nodes.length; i ++) { 47 node = nodes[i]; 48 if (typeof node == 'string') { // 节点选择器 49 if ((node = Base.trim(node)) == '') continue; 50 if (patternSpace.test(node)) { // 多层 51 elements = node.split(patternSpace); 52 } else { // 单层 53 elements.push(node); 54 } 55 for (var j = 0; j < elements.length; j ++) { 56 if (this.selector(elements[j]).getNodes() == this.info) break; 57 } 58 for (var k = 0; k < this.elements.length; k ++) { 59 elementsList.push(this.elements[k]); 60 } 61 this.elements = [], elements = []; 62 } else if (Base.checkNode(node) || node === window) { 63 elementsList.push(node); 64 } else if (typeof node == 'function') { // HTML DOM 65 this.loaded(node); 66 } 67 } 68 this.elements = elementsList; 69 } 70 }; 71 72 // 元素节点选择器 73 Base.prototype.selector = function (selector) { 74 if (typeof selector != 'string') { 75 this.elements = []; 76 return this; 77 } 78 selector = Base.trim(selector); 79 var nodes = null, elements = []; 80 if (this.elements.length == 0) this.elements.push(document); 81 switch (selector.charAt(0)) { 82 case '#' : // id 83 selector = selector.substring(1); 84 for (var i = 0; i < this.elements.length; i ++) { 85 if ((nodes = $().getId(selector, this.elements[i]).getNodes()) != this.info) elements.push(nodes); 86 } 87 break; 88 case '.' : // class 89 selector = selector.substring(1); 90 for (var i = 0; i < this.elements.length; i ++) { 91 if ((nodes = $().getClass(selector, this.elements[i]).getNodes()) != this.info) { 92 if (nodes instanceof Array) { // 集群 93 for (var j = 0; j < nodes.length; j ++) { 94 elements.push(nodes[j]); 95 } 96 } else { // 单一 97 elements.push(nodes); 98 } 99 } 100 } 101 break; 102 default : // tagName 103 var patternId = /^(\w+)#([\-\w]+)$/i, patternClass = /^(\w+)\.([\-\w]+)$/i; 104 if (patternId.test(selector)) { // tagName + id 105 var tagName = patternId.exec(selector)[1]; 106 var id = patternId.exec(selector)[2]; 107 for (var i = 0; i < this.elements.length; i ++) { 108 if ((nodes = $().getTagName(tagName, this.elements[i]).getNodes()) != this.info) { 109 if (nodes instanceof Array) { // 集群 110 for (var j = 0; j < nodes.length; j ++) { 111 if (nodes[j].id == id) elements.push(nodes[j]); 112 } 113 } else { // 单一 114 if (nodes.id == id) elements.push(nodes); 115 } 116 } 117 } 118 } else if (patternClass.test(selector)) { // tagName + class 119 var tagName = patternClass.exec(selector)[1]; 120 var className = patternClass.exec(selector)[2]; 121 for (var i = 0; i < this.elements.length; i ++) { 122 if ((nodes = $().getTagName(tagName, this.elements[i]).getNodes()) != this.info) { 123 if (nodes instanceof Array) { // 集群 124 for (var j = 0; j < nodes.length; j ++) { 125 if (Base.hasClass(className, nodes[j])) elements.push(nodes[j]); 126 } 127 } else { // 单一 128 if (Base.hasClass(className, nodes)) elements.push(nodes); 129 } 130 } 131 } 132 } else { // tagName 133 for (var i = 0; i < this.elements.length; i ++) { 134 if ((nodes = $().getTagName(selector, this.elements[i]).getNodes()) != this.info) { 135 if (nodes instanceof Array) { // 集群 136 for (var j = 0; j < nodes.length; j ++) { 137 elements.push(nodes[j]); 138 } 139 } else { // 单一 140 elements.push(nodes); 141 } 142 } 143 } 144 } 145 } 146 this.elements = elements; 147 return this; 148 }; 149 150 // 获取 id 元素节点 151 Base.prototype.getId = function (ids, positioner) { 152 if (ids instanceof Array) { // 集群 153 for (var i = 0; i < ids.length; i ++) { 154 this.getId(ids[i], positioner); 155 } 156 } else { // 单一 157 for (var k = 0; k < this.elements.length; k ++) { 158 if (this.elements[k].id == ids) return this; 159 } 160 var selector = null, node = null; 161 selector = Base.targetNode(positioner); 162 if (selector === false) return this; 163 if (selector instanceof Array) { // 集群 164 for (var i = 0; i < selector.length; i ++) { 165 node = selector[i].getElementsByTagName('*'); 166 for (var j = 0; j < node.length; j ++) { 167 if (node[j].id == ids) { 168 this.elements.push(node[j]); 169 } 170 } 171 } 172 } else { // 单一 173 node = selector.getElementsByTagName('*'); 174 for (var i = 0; i < node.length; i ++) { 175 if (node[i].id == ids) this.elements.push(node[i]); 176 } 177 } 178 } 179 return this; 180 }; 181 182 // 获取 tagName 元素节点 183 Base.prototype.getTagName = function (tagName, positioner) { 184 var selector = null, node = null; 185 selector = Base.targetNode(positioner); 186 if (selector === false) return this; 187 if (selector instanceof Array) { // 集群 188 for (var i = 0; i < selector.length; i ++) { 189 node = selector[i].getElementsByTagName(tagName); 190 for (var j = 0; j < node.length; j ++) { 191 this.elements.push(node[j]); 192 } 193 } 194 } else { // 单一 195 node = selector.getElementsByTagName(tagName); 196 for (var i = 0; i < node.length; i ++) { 197 this.elements.push(node[i]); 198 } 199 } 200 return this; 201 }; 202 203 // 获取 class 元素节点 204 Base.prototype.getClass = function (className, positioner) { 205 var selector = null, node = null; 206 selector = Base.targetNode(positioner); 207 if (selector === false) return this; 208 if (selector instanceof Array) { // 集群 209 for (var i = 0; i < selector.length; i ++) { 210 node = selector[i].getElementsByTagName('*'); 211 for (var j = 0; j < node.length; j ++) { 212 if (Base.hasClass(className, node[j])) this.elements.push(node[j]); 213 } 214 } 215 } else { // 单一 216 node = selector.getElementsByTagName('*'); 217 for (var i = 0; i < node.length; i ++) { 218 if (Base.hasClass(className, node[i])) this.elements.push(node[i]); 219 } 220 } 221 return this; 222 }; 223 224 // 获取 name 元素节点 225 Base.prototype.getName = function (name, positioner) { 226 var selector = null, node= null; 227 selector = Base.targetNode(positioner); 228 if (selector === false) return this; 229 if (selector instanceof Array) { // 集群 230 for (var i = 0; i < selector.length; i ++) { 231 node = selector[i].getElementsByTagName('*'); 232 for (var j = 0; j < node.length; j ++) { 233 if (node[j].name == name) this.elements.push(node[j]); 234 } 235 } 236 } else { // 单一 237 node = selector.getElementsByTagName('*'); 238 for (var i = 0; i < node.length; i ++) { 239 if (node[i].name == name) this.elements.push(node[i]); 240 } 241 } 242 return this; 243 }; 244 245 // 返回元素节点 246 Base.prototype.getNodes = function () { 247 if (this.elements.length == 0) return this.info; 248 if (this.elements.length == 1) return this.elements[0]; 249 return this.elements; 250 }; 251 252 // 返回第一个元素节点 253 Base.prototype.firstNode = function () { 254 if (this.elements.length == 0) return this.info; 255 return this.elements[0]; 256 }; 257 258 // 返回最后一个元素节点 259 Base.prototype.lastNode = function () { 260 if (this.elements.length == 0) return this.info; 261 return this.elements[this.elements.length - 1]; 262 }; 263 264 // 获取第一个子元素节点 265 Base.prototype.firstChild = function () { 266 var childs = []; 267 var length = this.elements.length - 1; 268 for (var i = length; i >= 0; i --) { 269 Base.space(Base.comment(this.elements[i])); 270 if (!(childs = Base.getChilds(this.elements[i]))) { 271 this.elements.splice(i, 1); 272 continue; 273 } 274 this.elements[i] = childs[0]; 275 } 276 return this; 277 }; 278 279 // 获取最后一个子元素节点 280 Base.prototype.lastChild = function () { 281 var childs = []; 282 var length = this.elements.length - 1; 283 for (var i = length; i >= 0; i --) { 284 Base.space(Base.comment(this.elements[i])); 285 if (!(childs = Base.getChilds(this.elements[i]))) { 286 this.elements.splice(i, 1); 287 continue; 288 } 289 this.elements[i] = childs[childs.length - 1]; 290 } 291 return this; 292 }; 293 294 // 获取子元素节点 295 Base.prototype.childNodes = function () { 296 var childs = []; 297 var length = this.elements.length - 1; 298 for (var i = length; i >= 0; i --) { 299 Base.space(Base.comment(this.elements[i])); 300 childs = Base.getChilds(this.elements[i]) 301 this.elements.splice(i, 1); 302 if (!childs) continue; 303 for (var j = 0; j < childs.length; j ++) { 304 this.elements.splice(i + j, 0, childs[j]); 305 } 306 } 307 return this; 308 }; 309 310 // 获取上一位兄弟节点 311 Base.prototype.previousNode = function () { 312 var length = this.elements.length - 1; 313 for (var i = length; i >= 0; i --) { 314 Base.space(Base.comment(this.elements[i].parentNode)); 315 if (!(this.elements[i] = Base.siblingNode(this.elements[i], false))) this.elements.splice(i, 1); 316 } 317 return this; 318 }; 319 320 // 获取下一位兄弟节点 321 Base.prototype.nextNode = function () { 322 var length = this.elements.length - 1; 323 for (var i = length; i >= 0; i --) { 324 Base.space(Base.comment(this.elements[i].parentNode)); 325 if (!(this.elements[i] = Base.siblingNode(this.elements[i], true))) this.elements.splice(i, 1); 326 } 327 return this; 328 }; 329 330 // 获取之前所有兄弟节点 331 Base.prototype.previousAllNode = function () { 332 var length = this.elements.length - 1; 333 var siblings = []; 334 for (var i = length; i >= 0; i --) { 335 Base.space(Base.comment(this.elements[i].parentNode)); 336 if (!(this.elements[i] = Base.siblingNode(this.elements[i], false))) { 337 this.elements.splice(i, 1); 338 for (var j = 0; j < siblings.length; j ++) { 339 this.elements.splice(i + j, 0, siblings[j]); 340 } 341 siblings = []; 342 } else { 343 siblings.push(this.elements[i]); 344 i ++; 345 } 346 } 347 return this; 348 }; 349 350 // 获取之后所有兄弟节点 351 Base.prototype.nextAllNode = function () { 352 var length = this.elements.length - 1; 353 var siblings = []; 354 for (var i = length; i >= 0; i --) { 355 Base.space((Base.comment(this.elements[i].parentNode))); 356 if (!(this.elements[i] = Base.siblingNode(this.elements[i]))) { 357 this.elements.splice(i, 1); 358 for (var j = 0; j < siblings.length; j ++) { 359 this.elements.splice(i + j, 0, siblings[j]); 360 } 361 siblings = []; 362 } else { 363 siblings.push(this.elements[i]); 364 i ++; 365 } 366 } 367 return this; 368 }; 369 370 // 获取父级元素节点 371 Base.prototype.parentNode = function () { 372 var length = this.elements.length - 1; 373 for (var i = length; i >= 0; i --) { 374 if (!Base.checkNode(this.elements[i]) || this.elements[i].parentNode === null) { 375 this.elements.splice(i, 1); 376 continue; 377 } 378 this.elements[i] = this.elements[i].parentNode; 379 } 380 return this; 381 }; 382 383 // 设置与获取元素内容 384 Base.prototype.html = function (text) { 385 var html = []; 386 for (var i = 0; i < this.elements.length; i ++) { 387 if (typeof text != 'undefined') { // 设置 388 this.elements[i].innerHTML = text; 389 } else { // 获取 390 html.push(this.elements[i].innerHTML); 391 } 392 } 393 switch (html.length) { 394 case 0 : 395 return this; 396 break; 397 case 1 : 398 return html[0]; 399 break; 400 default : 401 return html; 402 } 403 }; 404 405 // 设置与获取表单内容 406 Base.prototype.value = function (text) { 407 var value = []; 408 for (var i = 0; i < this.elements.length; i ++) { 409 if (typeof this.elements[i].value == 'undefined') continue; 410 if (typeof text != 'undefined') { // 设置 411 this.elements[i].value = text; 412 } else { // 获取 413 value.push(this.elements[i].value); 414 } 415 } 416 switch (value.length) { 417 case 0 : 418 return this; 419 break; 420 case 1 : 421 return value[0]; 422 break; 423 default : 424 return value; 425 } 426 }; 427 428 // 设置与获取元素样式 429 Base.prototype.css = function (cssKey, cssValue) { 430 var css = [], isList = false; 431 var patternCss = /^([a-z\-\s]+)([\s\t ]*)(=|:)\2([\s\w\-\.\\\/\'\"#%\(\)=]+);?$/i; 432 var key = '', value = ''; 433 cssValue = Base.trim(cssValue); 434 if (!(isList = cssKey instanceof Array)) cssKey = Base.trim(cssKey); 435 for (var i = 0; i < this.elements.length; i ++) { 436 if (typeof cssValue != 'undefined' || isList) { // 设置 437 if (isList) { // 集群 438 for (var j = 0; j < cssKey.length; j ++) { 439 cssKey[j] = Base.trim(cssKey[j]); 440 if (patternCss.test(cssKey[j])) { 441 key = Base.toStyle(Base.trim(patternCss.exec(cssKey[j])[1])); 442 value = Base.trim(patternCss.exec(cssKey[j])[4]); 443 this.elements[i].style[key] = value; 444 } 445 } 446 } else { // 单一 447 cssKey = Base.toStyle(Base.trim(cssKey)); 448 this.elements[i].style[cssKey] = cssValue; 449 } 450 } else { // 获取 451 css.push(Tool.getStyle(cssKey, this.elements[i])); 452 } 453 } 454 switch (css.length) { 455 case 0 : 456 return this; 457 break; 458 case 1 : 459 return css[0]; 460 break; 461 default : 462 return css; 463 } 464 }; 465 466 // 添加 class 选择器 467 Base.prototype.addClass = function (className) { 468 if (typeof className == 'undefined') return this; 469 className = Base.trim(className); 470 if (arguments.length == 1) { // 单一 471 var space = ''; 472 for (var i = 0; i < this.elements.length; i ++) { 473 if (this.elements[i].className != '') space = ' '; 474 if (!Base.hasClass(className, this.elements[i])) this.elements[i].className += space + className; 475 space = ''; 476 } 477 } else { // 集群 478 for (var i = 0; i < arguments.length; i ++) { 479 this.addClass(arguments[i]); 480 } 481 } 482 return this; 483 }; 484 485 // 移除 class 选择器 486 Base.prototype.removeClass = function (className) { 487 if (typeof className == 'undefined') return this; 488 className = Base.trim(className); 489 if (arguments.length == 1) { // 单一 490 for (var i = 0; i < this.elements.length; i ++) { 491 if (Base.hasClass(className, this.elements[i])) { 492 this.elements[i].className = this.elements[i].className.replace(new RegExp('(^|\\s+)' + className + '(\\s+|$)'), ' '); 493 this.elements[i].className = Base.trim(this.elements[i].className); 494 } 495 } 496 } else { // 集群 497 for (var i = 0; i < arguments.length; i ++) { 498 this.removeClass(arguments[i]); 499 } 500 } 501 return this; 502 }; 503 504 // 添加样式规则 505 Base.prototype.addRule = function (ruleName, ruleText, positioner, sheetIndex) { 506 var rule = null; 507 if (!(rule = Base.checkRule(positioner, sheetIndex))) return this; 508 if (ruleName instanceof Array) { // 集群 509 var patternRule = /^([\w\-#\.:,\s]+)[\s\t ]*\{([\s\w\-\.\\\/\'\"#%\(\)=:;]+)\}$/i; 510 var _rule = null; 511 for (var i = 0; i < ruleName.length; i ++) { 512 _rule = Base.trim(ruleName[i]); 513 if (patternRule.test(_rule)) { 514 this.addRule(patternRule.exec(_rule)[1], patternRule.exec(_rule)[2], positioner, sheetIndex); 515 } 516 } 517 } else { // 单一 518 ruleText = Base.trim(ruleText); 519 if (ruleName.indexOf(',') != -1) { 520 ruleName = ruleName.split(','); 521 for (var i = 0 ; i < ruleName.length; i ++) { 522 ruleName[i] = Base.trim(ruleName[i]); 523 if (ruleName[i] == '') continue; 524 Tool.addRule(rule.sheet, ruleName[i], ruleText, rule.positioner); 525 } 526 } else { 527 ruleName = Base.trim(ruleName); 528 if (ruleName == '') return this; 529 Tool.addRule(rule.sheet, ruleName, ruleText, rule.positioner); 530 } 531 } 532 return this; 533 }; 534 535 // 移除样式规则 536 Base.prototype.removeRule = function (ruleName, sheetIndex) { 537 var rule = null; 538 if (!(rule = Base.checkRule(null, sheetIndex))) return this; 539 if (ruleName instanceof Array) { // 集群 540 for (var i = 0; i < ruleName.length; i ++) { 541 this.removeRule(ruleName[i], sheetIndex); 542 } 543 } else { // 单一 544 var nameList = Tool.ruleName(rule.sheet); 545 ruleName = Base.trim(ruleName.toLowerCase()); 546 var patternName = ''; 547 switch (ruleName.charAt(0)) { 548 case '#' : // id 549 patternName = '(^|\\s+|[a-z]+)' + ruleName + '(:[a-z]+)*($|,)'; 550 break; 551 case '.' : // class 552 patternName = '(^|\\s+|[a-z]+)' + ruleName + '(:[a-z]+)*($|,)'; 553 break; 554 default : // tagName 555 patternName = '(^|\\s+)' + ruleName + '(:[a-z]+)*($|,)'; 556 break; 557 } 558 for (var i = nameList.length - 1; i >= 0; i --) { 559 nameList[i] = Base.trim(nameList[i].toLowerCase()); 560 if ((new RegExp(patternName, 'i')).test(nameList[i])) Tool.removeRule(rule.sheet, i); 561 } 562 } 563 return this; 564 }; 565 566 // 获取元素尺寸信息 567 Base.prototype.getSize = function () { 568 var elementNode = null, innerRectangle = null, outerRectangle = null, size = []; 569 for (var i = 0; i < this.elements.length; i ++) { 570 elementNode = this.elements[i]; 571 innerRectangle = Base.getInnerRectangle(elementNode); 572 outerRectangle = Base.getOuterRectangle(elementNode); 573 size.push({ 574 width : innerRectangle.width, 575 height : innerRectangle.height, 576 x : outerRectangle.left, 577 y : outerRectangle.top 578 }); 579 } 580 switch (size.length) { 581 case 0 : 582 return this; 583 break; 584 case 1 : 585 return size[0]; 586 break; 587 default : 588 return size; 589 } 590 }; 591 592 // 元素事件绑定 593 Base.prototype.nodeEvent = function (eventName, method, mode) { 594 if (typeof eventName != 'string' || typeof method != 'function') return this; 595 eventName = eventName.toLowerCase(); 596 if (eventName.indexOf('on') == 0) eventName = eventName.substring(2); 597 if (typeof mode == 'undefined') mode = true; 598 for (var i = 0; i < this.elements.length; i ++) { 599 switch (eventName) { 600 case 'scroll' : // 滚动条事件 601 Tool.scroll(this.elements[i], method, !!mode); 602 break; 603 case 'mousewheel' : // 鼠标滚轮事件 604 Tool.mousewheel(this.elements[i], method, !!mode); 605 break; 606 case 'mouseover' : // 鼠标移入 607 Tool.mouseover(this.elements[i], method, !!mode); 608 break; 609 case 'mouseout' : // 鼠标移出 610 Tool.mouseout(this.elements[i], method, !!mode); 611 break; 612 default : // 其他事件 613 !!mode ? Tool.loginEvent(this.elements[i], eventName, method) : Tool.logoutEvent(this.elements[i], eventName, method); 614 } 615 } 616 return this; 617 }; 618 619 // HTML DOM 加载 620 Base.prototype.loaded = function (method) { 621 if (typeof method != 'function') return this; 622 Tool.loaded(method); 623 return this; 624 }; 625 626 // 自动加载插件 627 Base.prototype.plugins = function () { 628 var plugins = arguments; 629 if (plugins.length != 0) { 630 var pluginName = ''; 631 for (var i = 0; i < plugins.length; i ++) { 632 if (!(pluginName = Base.getMethodName(plugins[i]))) continue; 633 Base.prototype[pluginName] = plugins[i]; 634 } 635 } 636 return this; 637 }; 638 639 /* 640 库方法集合 641 */ 642 // 库方法: 检测元素内容是否为空 643 Base.empty = function (elementNode) { 644 elementNode = this.comment(elementNode); 645 var text = this.trim($(elementNode).html()); 646 return text == ''; 647 }; 648 649 // 库方法: 获取方法名称 650 Base.getMethodName = function (method) { 651 var patternMethodName = /^function\s([\$\w]+)\(/i; 652 if (typeof method != 'function' || !patternMethodName.test(method.toString())) return false; 653 return patternMethodName.exec(method.toString())[1]; 654 }; 655 656 // 库方法: 禁止页面滚动(浏览器 BUG 修正) 657 Base.fixed = function (elementNode) { 658 // 兼容 IE 659 $(elementNode).nodeEvent('mousedown', function () { 660 var scroll = Base.scroll(); 661 $(document.documentElement).nodeEvent('mouseup', function () { 662 Base.scroll(scroll.x, scroll.y); 663 $(this).nodeEvent('mouseup', arguments.callee, false); 664 }); 665 }); 666 // 兼容 Chrome 667 $(elementNode).nodeEvent('mousewheel', function () {}); 668 }; 669 670 // 库方法: 动画参数容错处理 671 Base.checkAnimation = function (animation, elementNode) { 672 // 动画参数对象容错处理 673 if (typeof animation != 'object') return false; 674 675 // 动画缓冲开关容错处理 676 if (typeof animation.isBuffer != 'undefined') { 677 animation.isBuffer = !!animation.isBuffer; 678 } else { 679 animation.isBuffer = false; 680 } 681 682 // 动画缓冲速率容错处理 683 animation.speed = Math.abs(this.number(animation.speed, 20)); 684 685 // 动画迭代毫秒数容错处理 686 animation.time = Math.abs(this.number(animation.time, 30)); 687 688 // 动画列队容错处理 689 if (typeof animation.method != 'function') { 690 animation.method = function () {}; 691 } 692 693 // 动画增减量容错处理 694 animation.value = this.number(animation.value, false); 695 696 // 动画步长值容错处理 697 animation.step = Math.abs(this.number(animation.step, 10)); 698 699 // 同步动画容错处理 700 if (typeof animation.multiple != 'object') animation.multiple = {}; 701 702 function checkAction(action) { 703 if (typeof action != 'string') return false; 704 action = Base.trim(action.toLowerCase()); 705 switch (action) { 706 case 'x' : 707 case 'l' : 708 action = 'left'; 709 break; 710 case 'y' : 711 case 't' : 712 action = 'top'; 713 break; 714 case 'w' : 715 action = 'width'; 716 break; 717 case 'h' : 718 action = 'height'; 719 break; 720 case 'o' : 721 action = 'opacity'; 722 break; 723 default : 724 action = Base.toStyle(action); 725 } 726 return action; 727 } 728 729 function checkStart(start, action) { 730 start = Base.trim(start); 731 return Base.number(start, function () { 732 if (/^(left)|(top)$/.test(action)) Base.absolute(elementNode); 733 if (action == 'opacity') { 734 if (browserDetect.ie && browserDetect.browser.version < 9) { 735 var opacity = Base.trim($(elementNode).css('filter')); 736 var patternOpacity = /alpha\(opacity([\s\t ]*)=\1(\d+)\)/i; 737 if (patternOpacity.test(opacity)) { 738 return patternOpacity.exec(opacity)[2]; 739 } else { 740 return 100; 741 } 742 } else { 743 return $(elementNode).css('opacity') * 100; 744 } 745 } else { 746 return parseInt(Base.replace($(elementNode).css(action), 'px')); 747 } 748 }); 749 } 750 751 function checkTarget(target) { 752 target = Base.trim(target); 753 return Base.number(target, function () { 754 return animation.value === false ? animation.value : animation.start + animation.value; 755 }); 756 } 757 758 function checkOpacity(value) { 759 if (value < 0) value = 0; 760 if (value > 100) value = 100; 761 return value; 762 } 763 764 for (var key in animation.multiple) {} 765 766 // 检测是否同步动画 767 if (typeof key == 'undefined') { // 单一动画 768 769 // 动画运动方式容错处理 770 animation.action = checkAction(animation.action); 771 772 if (animation.action === false) return false; 773 774 // 动画初始值容错处理 775 animation.start = checkStart(animation.start, animation.action); 776 777 // 动画目标量容错处理 778 animation.target = checkTarget(animation.target); 779 780 if (animation.action == 'opacity') { 781 animation.start = checkOpacity(animation.start); 782 animation.target = checkOpacity(animation.target); 783 } 784 if (animation.target === false || animation.start == animation.target) return false; 785 786 if (animation.start > animation.target) animation.step = -animation.step; 787 788 // 容错机制 789 if (animation.start == 0) animation.start = 1; 790 791 animation.action = [animation.action]; 792 animation.start = [animation.start]; 793 animation.target = [animation.target]; 794 animation.step = [animation.step]; 795 } else { // 同步动画 796 // 遍历同步动画 797 animation.action = []; 798 animation.start = []; 799 animation.target = []; 800 var id = 0, value = '', patternValue = /^([\d\.]+)([\s\t ]*)=>\2([\d\.]+)$/; 801 var action = '', start = 0, target = 0, step = animation.step; 802 animation.step = []; 803 for (var i in animation.multiple) { 804 value = Base.trim(animation.multiple[i]); 805 action = checkAction(i); 806 if (patternValue.test(value)) { // 初始值 + 目标量 807 start = checkStart(patternValue.exec(value)[1], action); 808 target = checkTarget(patternValue.exec(value)[3]); 809 } else { // 目标量 810 start = checkStart(null, action); 811 target = checkTarget(value); 812 } 813 if (action == 'opacity') { 814 start = checkOpacity(start); 815 target = checkOpacity(target); 816 } 817 if (target === false || target == start) continue; 818 819 // 容错机制 820 if (start == 0) start = 1; 821 822 animation.action[id] = action; 823 animation.start[id] = start; 824 animation.target[id] = target; 825 animation.step[id] = start > target ? -step : step; 826 id ++; 827 } 828 } 829 return animation; 830 }; 831 832 // 库方法: 数字转换 833 Base.number = function (number, replace) { 834 if (typeof number != 'undefined' && !isNaN(parseInt(number))) { 835 return parseInt(number); 836 } else { 837 switch (typeof replace) { 838 case 'undefined' : 839 return 0; 840 break; 841 case 'function' : 842 return replace(); 843 break; 844 default : 845 return replace; 846 } 847 } 848 }; 849 850 // 库方法: 添加元素节点属性 851 Base.attribute = function (attributes, elementNode) { 852 if (attributes instanceof Array) { // 集群 853 for (var i = 0; i < attributes.length; i ++) { 854 this.attribute(attributes[i], elementNode); 855 } 856 } else { // 单一 857 attributes = this.trim(attributes); 858 var patternAttribute = /^([a-z]+)([\s\t ]*)=\2([=\w\'\":;\.\\\/\-%#\(\)\s]+)$/i, key = '', value = ''; 859 if (patternAttribute.test(attributes)) { 860 key = patternAttribute.exec(attributes)[1]; 861 value = patternAttribute.exec(attributes)[3]; 862 if (/^class(Name)?$/i.test(key)) { // class 863 var patternSpace = /[\s\t ]+/; 864 if (patternSpace.test(value)) { // 集群 865 var valueList = value.split(patternSpace); 866 for (var i = 0; i < valueList.length; i ++) { 867 if (valueList[i] == '') continue; 868 $(elementNode).addClass(valueList[i]); 869 } 870 } else { // 单一 871 $(elementNode).addClass(value); 872 } 873 } else if (key == 'style') { // css 874 var patternSpace = /;[\s\t ]*/; 875 if (patternSpace.test(value)) { // 集群 876 var valueList = value.split(patternSpace); 877 $(elementNode).css(valueList); 878 } else { // 单一 879 $(elementNode).css([value]); 880 } 881 } else { 882 elementNode.setAttribute(key, value); 883 } 884 } 885 } 886 return elementNode; 887 }; 888 889 // 库方法: 获取元素兄弟节点 890 Base.siblingNode = function (elementNode, siblingMode) { 891 if (!this.checkNode(elementNode) && elementNode.nodeType != 3) return false; 892 if (typeof siblingMode == 'undefined') siblingMode = true; 893 siblingNode = !!siblingMode ? elementNode.nextSibling : elementNode.previousSibling; 894 if (siblingNode === null) { 895 return false; 896 } else if (siblingNode.nodeType != 1) { 897 return this.siblingNode(siblingNode, siblingMode); 898 } else { 899 return siblingNode; 900 } 901 }; 902 903 // 库方法: 添加节点对象 904 Base.addElement = function (elementName, attributes, htmlText, elementTarget) { 905 if (typeof elementName != 'string') return false; 906 var elementNode = document.createElement(elementName); 907 if (typeof attributes == 'string' || attributes instanceof Array) this.attribute(attributes, elementNode); 908 if (typeof htmlText != 'undefined' && htmlText !== null) $(elementNode).html(htmlText); 909 if (typeof elementTarget == 'undefined' || !(elementTarget = this.targetNode(elementTarget))) elementTarget = document.body; 910 var nodes = []; 911 if (elementTarget instanceof Array) { // 集群 912 for (var i = 0; i < elementTarget.length; i ++) { 913 nodes.push(elementTarget[i].appendChild(elementNode.cloneNode(true))); 914 } 915 } else { // 单一 916 nodes.push(elementTarget.appendChild(elementNode)); 917 } 918 return nodes.length == 1 ? nodes[0] : nodes; 919 }; 920 921 // 库方法: 移除节点对象 922 Base.removeElement = function (elementNode, elementTarget) { 923 if (typeof elementNode == 'undefined' || (!(elementNode instanceof Array) && !(elementNode = this.targetNode(elementNode)))) return false; 924 if (typeof elementTarget == 'undefined' || (!(elementTarget instanceof Array) && !(elementTarget = this.targetNode(elementTarget)))) elementTarget = false; 925 if (elementTarget instanceof Array) { 926 for (var i = 0; i < elementTarget.length; i ++) { 927 this.removeElement(elementNode, elementTarget[i]); 928 } 929 } else { 930 if (elementNode instanceof Array) { 931 for (var i = 0; i < elementNode.length; i ++) { 932 this.removeElement(elementNode[i], elementTarget); 933 } 934 } else { 935 if (elementTarget === false) elementTarget = elementNode.parentNode; 936 if (this.contains(elementNode, elementTarget)) elementTarget.removeChild(elementNode); 937 } 938 } 939 }; 940 941 // 库方法: 元素直接从属关系 942 Base.contains = function (elementNode, elementTarget) { 943 if (!this.checkNode(elementNode) || !this.checkNode(elementTarget)) return false; 944 var children = elementTarget.childNodes; 945 for (var i = 0; i < children.length; i ++) { 946 if (children[i] == elementNode) return true; 947 } 948 return false; 949 }; 950 951 // 库方法: 获取浏览器可视区域大小 952 Base.getWindowRectangle = function () { 953 return { 954 width : Tool.getWindowWidth(), 955 height : Tool.getWindowHeight() 956 } 957 }; 958 959 // 库方法: 获取元素内部区域大小 960 Base.getInnerRectangle = function (elementNode) { 961 var _this = $(elementNode); 962 var display = _this.css('display'); 963 var width = 0, height = 0; 964 if (display != 'none') { 965 width = elementNode.offsetWidth; 966 height = elementNode.offsetHeight; 967 } else { 968 width = this.number(this.replace(_this.css('width'), 'px')); 969 height = this.number(this.replace(_this.css('height'), 'px')); 970 } 971 return { 972 width : width, 973 height : height 974 } 975 }; 976 977 // 库方法: 获取元素外部区域大小 978 Base.getOuterRectangle = function (elementNode) { 979 var _this = $(elementNode); 980 var display = _this.css('display'); 981 var left = 0, top = 0; 982 if (display != 'none') { 983 left = elementNode.offsetLeft; 984 top = elementNode.offsetTop; 985 } else { 986 this.absolute(elementNode); 987 left = this.number(this.replace(_this.css('left'), 'px')); 988 top = this.number(this.replace(_this.css('top'), 'px')); 989 } 990 return { 991 left : left, 992 top : top 993 } 994 }; 995 996 // 库方法: 设置元素绝对定位 997 Base.absolute = function (elementNode) { 998 var _this = $(elementNode); 999 if (_this.css('position') == 'static') _this.css('position', 'absolute'); 1000 }; 1001 1002 // 库方法: 样式风格转换 1003 Base.toStyle = function (cssKey) { 1004 if (typeof cssKey != 'string') return cssKey; 1005 if (cssKey.indexOf('-') != -1) { 1006 var keyList = cssKey.split('-'); 1007 for (var i = 0; i < keyList.length; i ++) { 1008 if ((keyList[i] = Base.trim(keyList[i])) == '') continue; 1009 if (i == 0) { 1010 cssKey = keyList[i]; 1011 } else { 1012 cssKey += keyList[i].charAt(0).toUpperCase() + keyList[i].substring(1); 1013 } 1014 } 1015 } 1016 return cssKey; 1017 }; 1018 1019 // 库方法: 获取目标节点 1020 Base.targetNode = function (positioner) { 1021 var selector = null; 1022 if (typeof positioner != 'undefined') { // 局部 1023 if (this.checkNode(positioner)) { 1024 selector = positioner; 1025 } else { 1026 if (!(selector = this.positioner(positioner))) return false; 1027 } 1028 } else { // 全局 1029 selector = document; 1030 } 1031 return selector; 1032 }; 1033 1034 // 库方法: 获取与设置浏览器滚动条 1035 Base.scroll = function (x, y) { 1036 if (typeof x == 'number') { 1037 Tool.scrollX(Math.abs(parseInt(x))); 1038 } 1039 if (typeof y == 'number') { 1040 Tool.scrollY(Math.abs(parseInt(y))); 1041 } 1042 return { 1043 x : Tool.scrollX(), 1044 y : Tool.scrollY() 1045 } 1046 }; 1047 1048 // 库方法: 检测样式规则参数 1049 Base.checkRule = function (positioner, sheetIndex) { 1050 var sheets = document.styleSheets; 1051 if (typeof positioner == 'undefiend' || isNaN(positioner)) positioner = 0; 1052 if (typeof sheetIndex == 'undefined' || isNaN(sheetIndex)) sheetIndex = 0; 1053 if (sheetIndex >= sheets.length) sheetIndex = sheets.length - 1; 1054 sheetIndex = Math.abs(parseInt(sheetIndex)); 1055 positioner = Math.abs(parseInt(positioner)); 1056 if (typeof sheets[sheetIndex] == 'undefined' || typeof sheets[sheetIndex] == 'unknown') return false; 1057 var sheet = sheets[sheetIndex]; 1058 var positionerMax = Tool.ruleTotal(sheet) - 1; 1059 if (positioner > positionerMax) positioner = positionerMax; 1060 return { 1061 positioner : positioner, 1062 sheet : sheet 1063 } 1064 }; 1065 1066 // 库方法: 检测 class 1067 Base.hasClass = function (className, elementNode) { 1068 return (new RegExp('(^|\\s+)' + className + '(\\s+|$)', 'i')).test(elementNode.className); 1069 }; 1070 1071 // 库方法: 元素节点定位器 1072 Base.positioner = function (selector) { 1073 if (typeof selector != 'string') return false; 1074 selector = this.trim(selector); 1075 var patternId = /^(id([\s\t ]*)=\2)?(\w+)$/i, 1076 patternTag = /^tag(Name)?([\s\t ]*)=\2(\w+)$/i, 1077 patternClass = /^class(Name)?([\s\t ]*)=\2(\w+)$/i; 1078 if (patternId.test(selector)) { // id node 1079 selector = patternId.exec(selector)[3]; 1080 selector = $().getId(selector).getNodes(); 1081 } else if (patternTag.test(selector)) { // tagName node 1082 selector = patternTag.exec(selector)[3]; 1083 selector = $().getTagName(selector).getNodes(); 1084 } else if (patternClass.test(selector)) { // class node 1085 selector = patternClass.exec(selector)[3]; 1086 selector = $().getClass(selector).getNodes(); 1087 } 1088 if (selector == $().info) return false; 1089 return selector; 1090 }; 1091 1092 // 库方法: 移除两边空格 1093 Base.trim = function (string) { 1094 if (typeof string != 'string') return string; 1095 var spaceLeft = /^[\s\t ]+/, spaceRight = /[\s\t ]+$/; 1096 if (spaceLeft.test(string)) string = string.replace(spaceLeft, ''); 1097 if (spaceRight.test(string)) string = string.replace(spaceRight, ''); 1098 return string; 1099 }; 1100 1101 // 库方法: 移除指定字符串 1102 Base.replace = function (string, index) { 1103 if (typeof string != 'string' || typeof index == 'undefined') return string; 1104 if (string.toLowerCase().indexOf(index.toLowerCase()) != -1) string = string.replace(new RegExp(index, 'gi'), ''); 1105 return string; 1106 }; 1107 1108 // 库方法: 检测是否元素节点 1109 Base.checkNode = function (elementNode) { 1110 if (elementNode === null 1111 || 1112 typeof elementNode != 'object' 1113 || 1114 typeof elementNode.nodeType != 'number' 1115 || 1116 (elementNode.nodeType != 1 && elementNode.nodeType != 9)) return false; 1117 return true; 1118 }; 1119 1120 // 库方法: 移除空白节点 1121 Base.space = function (elementNode) { 1122 if (!this.checkNode(elementNode)) return elementNode; 1123 var childs = elementNode.childNodes; 1124 var maxIndex = childs.length - 1; 1125 for (var i = maxIndex; i >= 0; i --) { 1126 if (childs[i].nodeType == 3 && /^[\s\t ]+$/.test(childs[i].nodeValue)) elementNode.removeChild(childs[i]); 1127 } 1128 return elementNode; 1129 }; 1130 1131 // 库方法: 移除注释节点 1132 Base.comment = function (elementNode) { 1133 if (!this.checkNode(elementNode)) return elementNode; 1134 var childs = elementNode.childNodes; 1135 var maxIndex = childs.length - 1; 1136 for (var i = maxIndex; i >= 0; i --) { 1137 if (childs[i].nodeType == 8) elementNode.removeChild(childs[i]); 1138 } 1139 return elementNode; 1140 }; 1141 1142 // 库方法: 获取子元素节点 1143 Base.getChilds = function (elementNode) { 1144 if (!this.checkNode(elementNode)) return false; 1145 var childs = elementNode.childNodes; 1146 var childList = []; 1147 for (var i = 0; i < childs.length; i ++) { 1148 if (childs[i].nodeType == 1) childList.push(childs[i]); 1149 } 1150 return childList.length != 0 ? childList : false; 1151 };
1 /* 2 源码作者: 石不易(Louis Shi) 3 联系方式: http://www.shibuyi.net 4 =================================================================================================== 5 程序名称: JavaScript 工具库(跨浏览器兼容) BETA 4.0 版 6 迭代版本: BETA 3.0 7 功能总数: 21 个 8 新增总数: 3 个 9 删除总数: 0 个 10 追加功能: 11 1. 新增“跨浏览器鼠标移入事件”, 解决元素对子元素的移入误判, 导致事件反复执行的BUG 12 2. 新增“跨浏览器鼠标移出事件”, 解决元素对子元素的移出误判, 导致事件反复执行的BUG 13 3. 新增“跨浏览器滚动条事件”, 解决 IE 6/7/8 无法识别 document 节点的滚动条事件 14 优化功能: 15 1. 删除了“跨浏览器现代事件绑定: 注册事件”使用的唯一标识符, 直接用数组 push 方法添加 16 2. 对“IE 6/7/8 专属: 匹配 W3C 事件 Event 对象”方法新增了 mouseover 与 mouseout 事件中获取周围元素的方法 17 删除功能: 18 无 19 */ 20 21 var Tool = { 22 // 数组排序 23 sort : { 24 minToMax : function (min, max) { 25 if (min < max) { 26 return -1; 27 } else if (min > max) { 28 return 1; 29 } else { 30 return 0; 31 } 32 }, 33 maxToMin : function (min, max) { 34 if (min < max) { 35 return 1; 36 } else if (min > max) { 37 return -1; 38 } else { 39 return 0; 40 } 41 } 42 }, 43 44 // 跨浏览器获取计算后的样式 45 getStyle : function (cssKey, elementNode) { 46 if (typeof window.getComputedStyle != 'undefined') { // W3C 47 return window.getComputedStyle(elementNode, null)[cssKey]; 48 } else if (typeof elementNode.currentStyle) { // IE 6/7/8 49 return elementNode.currentStyle[cssKey]; 50 } 51 }, 52 53 // 跨浏览器获取样式规则总数 54 ruleTotal : function (sheet) { 55 if (typeof sheet.cssRules != 'undefined') { // W3C 56 return sheet.cssRules.length; 57 } else if (typeof sheet.rules != 'undefined') { // IE 6/7/8 58 return sheet.rules.length; 59 } 60 }, 61 62 // 跨浏览器获取样式规则名称 63 ruleName : function (sheet) { 64 var nameList = [], rules = null; 65 if (typeof sheet.cssRules != 'undefined') { // W3C 66 rules = sheet.cssRules; 67 } else if (typeof sheet.rules != 'undefined') { // IE 6/7/8 68 rules = sheet.rules; 69 } 70 for (var i = 0; i < rules.length; i ++) { 71 nameList.push(rules[i].selectorText); 72 } 73 return nameList; 74 }, 75 76 // 跨浏览器添加样式规则 77 addRule : function (sheet, ruleName, ruleText, positioner) { 78 if (typeof sheet.insertRule != 'undefined') { // W3C 79 sheet.insertRule(ruleName + ' {' + ruleText + '}', positioner); 80 } else if (typeof sheet.addRule != 'undefined') { // IE 6/7/8 81 sheet.addRule(ruleName, ruleText, positioner); 82 } 83 }, 84 85 // 跨浏览器删除样式规则 86 removeRule : function (sheet, positioner) { 87 if (typeof sheet.deleteRule != 'undefined') { // W3C 88 sheet.deleteRule(positioner); 89 } else if (typeof sheet.removeRule != 'undefined') { // IE 6/7/8 90 sheet.removeRule(positioner); 91 } 92 }, 93 94 // 跨浏览器现代事件绑定: 注册事件 95 loginEvent : function (elementNode, eventName, method) { 96 if (typeof elementNode.addEventListener != 'undefined') { // W3C 97 elementNode.addEventListener(eventName, method, false); 98 } else if (typeof elementNode.attachEvent != 'undefined') { // IE 6/7/8 99 // 创建哈希表 100 if (typeof elementNode.hashTable != 'object') elementNode.hashTable = {}; 101 // 创建事件数组 102 if (!(elementNode.hashTable[eventName] instanceof Array)) elementNode.hashTable[eventName] = []; 103 var events = elementNode.hashTable[eventName]; 104 // 检测方法是否重复 105 for (var i = 0; i < events.length; i ++) { 106 if (events[i] == method) return false; 107 } 108 // 存储事件方法 109 events.push(method); 110 var _this = this; 111 // 执行事件 112 elementNode['on' + eventName] = function () { 113 var event = _this.eventIE(window.event); 114 for (var i = 0; i < events.length; i ++) { 115 events[i].call(this, event); 116 } 117 }; 118 } 119 }, 120 121 // 跨浏览器现代事件绑定: 注销事件 122 logoutEvent : function (elementNode, eventName, method) { 123 if (typeof elementNode.removeEventListener != 'undefined') { // W3C 124 elementNode.removeEventListener(eventName, method, false); 125 } else if (typeof elementNode.detachEvent != 'undefined') { // IE 6/7/8 126 if (typeof elementNode.hashTable != 'object' || !(elementNode.hashTable[eventName] instanceof Array)) return false; 127 var events = elementNode.hashTable[eventName]; 128 for (var i = 0; i < events.length; i ++) { 129 if (events[i] == method) events.splice(i, 1); 130 } 131 } 132 }, 133 134 // IE 6/7/8 专属: 匹配 W3C 事件 Event 对象 135 eventIE : function (event) { 136 event.target = event.srcElement; 137 event.preventDefault = function () { 138 event.returnValue = false; 139 }; 140 event.stopPropagation = function () { 141 event.cancelBubble = true; 142 }; 143 event.relatedTarget = function () { 144 switch (event.type) { 145 case 'mouseover' : 146 return event.fromElement; 147 break; 148 case 'mouseout' : 149 return event.toElement; 150 break; 151 default : 152 return null; 153 } 154 }(); 155 return event; 156 }, 157 158 // 跨浏览器鼠标滚轮(鼠标中键)事件 159 mousewheel : function (elementNode, method, mode) { 160 if (elementNode == window) elementNode = document; // IE 6/7/8 161 if (typeof elementNode.onmousewheel != 'undefined') { // Not Firefox 162 mode ? this.loginEvent(elementNode, 'mousewheel', method) : this.logoutEvent(elementNode, 'mousewheel', method); 163 } else { // Firefox 164 mode ? this.loginEvent(elementNode, 'DOMMouseScroll', method) : this.logoutEvent(elementNode, 'DOMMouseScroll', method); 165 } 166 }, 167 168 // 跨浏览器鼠标移入事件 169 mouseover : function (elementNode, method, mode) { 170 mode ? this.loginEvent(elementNode, 'mouseover', function (event) { 171 if (event.relatedTarget !== null && (event.relatedTarget == this || Tool.contains(event.relatedTarget, this))) return false; 172 method.call(this, event); 173 }) : this.logoutEvent(elementNode, 'mouseover', method); 174 }, 175 176 // 跨浏览器鼠标移出事件 177 mouseout : function (elementNode, method, mode) { 178 mode ? this.loginEvent(elementNode, 'mouseout', function (event) { 179 if (event.relatedTarget !== null && (event.relatedTarget == this || Tool.contains(event.relatedTarget, this))) return false; 180 method.call(this, event); 181 }) : this.logoutEvent(elementNode, 'mouseout', method); 182 }, 183 184 // 跨浏览器滚动条事件 185 scroll : function (elementNode, method, mode) { 186 if (elementNode == document) this.scroll(document.documentElement, method, mode); // IE 6/7/8 187 mode ? this.loginEvent(elementNode, 'scroll', method) : this.logoutEvent(elementNode, 'scroll', method); 188 }, 189 190 // 跨浏览器设置与获取 X 轴滚动条 191 scrollX : function (x) { 192 var html = document.documentElement; 193 var body = document.body; 194 if (typeof x != 'undefined') { // 设置 195 html.scrollLeft = x; 196 body.scrollTop = x; 197 } else { // 获取 198 return html.scrollLeft == 0 ? body.scrollLeft : html.scrollLeft; 199 } 200 }, 201 202 // 跨浏览器设置与获取 Y 轴滚动条 203 scrollY : function (y) { 204 var html = document.documentElement; 205 var body = document.body; 206 if (typeof y != 'undefined') { // 设置 207 html.scrollTop = y; 208 body.scrollTop = y; 209 } else { // 获取 210 return html.scrollTop == 0 ? body.scrollTop : html.scrollTop; 211 } 212 }, 213 214 // 跨浏览器 HTML DOM 记载 215 loaded : function (method) { 216 if ((browserDetect.opera && browserDetect.browser.version < 9) 217 || 218 (/WebKit/i.test(browserDetect.engine.name) && browserDetect.engine.version < 525) 219 || 220 (browserDetect.firefox && browserDetect.browser.version < 3)) { // 低版本 W3C 221 var timer = setTimeout(function () { 222 clearTimeout(timer); 223 if (!/^(loading)|(interactive)|(loaded)|(complete)$/i.test(document.readyState)) { 224 timer = setTimeout(arguments.callee, 1); // 递归 225 } 226 method(); 227 }, 1); 228 229 } else if (!/Trident/i.test(browserDetect.engine.name) || browserDetect.engine.version >= 5) { // 主流 W3C 230 var _this = this; 231 _this.loginEvent(document, 'DOMContentLoaded', function () { 232 _this.logoutEvent(document, 'DOMContentLoaded', arguments.callee); 233 method(); 234 }); 235 } else if (/Trident/i.test(browserDetect.engine.name) && browserDetect.engine.version < 5) { // IE 6/7/8 236 var timer = setTimeout(function () { 237 clearTimeout(timer); 238 try { 239 document.documentElement.doScroll('top'); 240 method(); 241 } catch(error) { 242 timer = clearTimeout(arguments.callee, 1); 243 } 244 }, 1); 245 } 246 }, 247 248 // 跨浏览器检测元素从属关系(间接从属+直接从属) 249 contains : function (elementNode, elementTarget) { 250 if (typeof elementTarget.compareDocumentPosition != 'undefined') { // W3C 251 return elementTarget.compareDocumentPosition(elementNode) == 20; 252 } else if (typeof elementTarget.contains != 'undefined') { // IE 6/7/8 253 return elementTarget.contains(elementNode); 254 } 255 }, 256 257 // 跨浏览器获取浏览器可视区域 X 轴大小 258 getWindowWidth : function () { 259 if (typeof window.innerWidth != 'undefined') { // W3C 260 return window.innerWidth; 261 } else if (typeof document.documentElement.clientWidth != 'undefined') { // IE 6/7/8 262 return document.documentElement.clientWidth; 263 } 264 }, 265 266 // 跨浏览器获取浏览器可视区域 Y 轴大小 267 getWindowHeight : function () { 268 if (typeof window.innerHeight != 'undefined') { // W3C 269 return window.innerHeight; 270 } else if (typeof document.documentElement.clientHeight != 'undefined') { // IE 6/7/8 271 return document.documentElement.clientHeight; 272 } 273 }, 274 275 // IE 专属: 浏览器外捕获鼠标按下 276 setCaptureIE : function (elementNode) { 277 if (typeof elementNode.setCapture != 'undefined') { 278 elementNode.setCapture(); 279 } 280 }, 281 282 // IE 专属: 浏览器外捕获鼠标松开 283 releaseCaptureIE : function (elementNode) { 284 if (typeof elementNode.releaseCapture != 'undefined') { 285 elementNode.releaseCapture(); 286 } 287 } 288 };
1 /* 2 源码作者: 石不易(Louis Shi) 3 联系方式: http://www.shibuyi.net 4 =================================================================================================== 5 程序名称: 浏览器嗅探器 BETA 4.0 6 版本迭代: BETA 3.0 7 功能总数: 4 个 8 新增总数: 0 个 9 删除总数: 0 个 10 追加功能: 11 无 12 优化功能: 13 1. 优化了 IE 浏览器版本的嗅探 14 删除功能: 15 无 16 */ 17 18 // 浏览器嗅探器 19 (function () { 20 21 window.browserDetect = { 22 ie : false, 23 firefox : false, 24 chrome : false, 25 safari : false, 26 opera : false, 27 other : false, 28 29 browser : { 30 name : '', 31 version : 0 32 }, 33 34 engine : { 35 name : '', 36 version : 0 37 }, 38 39 system : { 40 name : '', 41 version : 0 42 } 43 44 }; 45 46 // 获取用户代理字符串 47 var userAgent = navigator.userAgent; 48 var platform = navigator.platform; 49 50 var patternIELow = /MSIE\s([\.\d]+)/i; 51 var patternIEHigh = /Trident(.+)rv:([\.\d]+)/i; 52 var patternFirefox = /Firefox\/([\.\d]+)/i; 53 var patternChrome = /Chrome\/([\.\d]+)/i; 54 var patternSafari = /Safari\/([\.\d]+)/i; 55 var patternOpera = /Opera\/([\.\d]+)/i; 56 57 var patternTrident = /Trident\/([\.\d]+)/i; 58 var patternVersion = /Version\/([\.\d]+)/i; 59 60 // 嗅探浏览器型号 61 if (patternIELow.test(userAgent) || patternIEHigh.test(userAgent)) { // IE 62 browserDetect.ie = true; 63 browserDetect.browser.name = 'Internet Explorer'; 64 // 浏览器版本 65 if (patternIELow.test(userAgent)) { // IE 6~10 66 browserDetect.browser.version = parseFloat(patternIELow.exec(userAgent)[1]); 67 } else { // IE 11+ 68 browserDetect.browser.version = parseFloat(patternIEHigh.exec(userAgent)[2]); 69 } 70 browserDetect.engine.name = 'Trident'; 71 switch (browserDetect.browser.version) { 72 case 6 : 73 browserDetect.engine.version = 2; 74 break; 75 case 7 : 76 browserDetect.engine.version = 3; 77 break; 78 default : 79 browserDetect.engine.version = 'unknown'; 80 } 81 } else if (patternFirefox.test(userAgent)) { // Firefox 82 browserDetect.firefox = true; 83 browserDetect.browser.name = 'Firefox'; 84 browserDetect.browser.version = parseFloat(patternFirefox.exec(userAgent)[1]); 85 } else if (patternChrome.test(userAgent)) { // Chrome 86 browserDetect.chrome = true; 87 browserDetect.browser.name = 'Chrome'; 88 browserDetect.browser.version = parseFloat(patternChrome.exec(userAgent)[1]); 89 } else if (patternSafari.test(userAgent)) { // Safari 90 browserDetect.safari = true; 91 browserDetect.browser.name = 'Safari'; 92 if (patternVersion.test(userAgent)) { // 高版本 93 browserDetect.browser.version = parseFloat(patternVersion.exec(userAgent)[1]); 94 } else { // 低版本 95 browserDetect.browser.version = parseFloat(patternSafari.exec(userAgent)[1]); 96 } 97 } else if (patternOpera.test(userAgent)) { // Opera 98 browserDetect.opera = true; 99 browserDetect.browser.name = 'Opera'; 100 if (patternVersion.test(userAgent)) { // 高版本 101 browserDetect.browser.version = parseFloat(patternVersion.exec(userAgent)[1]); 102 } else { // 低版本 103 browserDetect.browser.version = parseFloat(patternOpera.exec(userAgent)[1]); 104 } 105 } else { // Other 106 browserDetect.other = true; 107 browserDetect.browser.name = 'unknown'; 108 browserDetect.browser.version = 'unknown'; 109 } 110 111 var patternGecko = /Gecko\/([\.\d]+)/i; 112 var patternWebKit = /WebKit\/([\.\d]+)/i; 113 var patternPresto = /Presto\/([\.\d]+)/i; 114 115 // 嗅探浏览器内核(引擎) 116 if (patternTrident.test(userAgent)) { // Trident 117 browserDetect.engine.name = 'Trident'; 118 browserDetect.engine.version = parseFloat(patternTrident.exec(userAgent)[1]); 119 } else if (patternGecko.test(userAgent)) { // Gecko 120 browserDetect.engine.name = 'Gecko'; 121 patternVersion = /rv:([\.\d]+)/i; 122 if (patternVersion.test(userAgent)) { // 高版本 123 browserDetect.engine.version = parseFloat(patternVersion.exec(userAgent)[1]); 124 } else { // 低版本 125 browserDetect.engine.version = parseFloat(patternGecko.exec(userAgent)[1]); 126 } 127 } else if (patternWebKit.test(userAgent)) { // WebKit 128 browserDetect.engine.name = 'WebKit'; 129 browserDetect.engine.version = parseFloat(patternWebKit.exec(userAgent)[1]); 130 } else if (patternPresto.test(userAgent)) { // Presto 131 browserDetect.engine.name = 'Presto'; 132 browserDetect.engine.version = parseFloat(patternPresto.exec(userAgent)[1]); 133 } else if (!browserDetect.ie) { // Other 134 browserDetect.engine.name = 'unknown'; 135 browserDetect.engine.version = 'unknown'; 136 } 137 138 var patternWindows = /Windows\sNT\s([\.\d]+)/i; 139 var patternWin = /Win/i; 140 var patternLinux = /Linux/i; 141 var patternUnix = /X11/i; 142 var patternMac = /Mac/i; 143 144 // 嗅探系统平台 145 if (patternWindows.test(userAgent)) { // Windows (高版本浏览器) 146 browserDetect.system.version = parseFloat(patternWindows.exec(userAgent)[1]); 147 switch (browserDetect.system.version) { 148 case 5.0 : 149 browserDetect.system.name = 'Windows 2000'; 150 break; 151 case 5.1 : 152 browserDetect.system.name = 'Windows XP'; 153 break; 154 case 5.2 : 155 browserDetect.system.name = 'Windows Server 2003 / Windows Server 2003 R2'; 156 break; 157 case 6.0 : 158 browserDetect.system.name = 'Windows Vista / Windows Server 2008'; 159 break; 160 case 6.1 : 161 browserDetect.system.name = 'Windows 7 / Windows Server 2008 R2'; 162 break; 163 case 6.2 : 164 browserDetect.system.name = 'Windows 8 / Windows Server 2012 / Windows Phone 8'; 165 break; 166 case 6.3 : 167 browserDetect.system.name = 'Windows 8.1 / Windows Server 2012 R2'; 168 break; 169 default : 170 browserDetect.system.name = 'Windows'; 171 } 172 } else if (patternWin.test(platform)) { // Windows (低版本浏览器) 173 browserDetect.system.name = 'Windows'; 174 browserDetect.system.version = 'unknown'; 175 } else if (patternLinux.test(platform)) { // Linux 176 browserDetect.system.name = 'Linux'; 177 browserDetect.system.version = 'unknown'; 178 } else if (patternUnix.test(platform)) { // Unix 179 browserDetect.system.name = 'Unix'; 180 browserDetect.system.version = 'unknown'; 181 } else if (patternMac.test(platform)) { // Mac 182 browserDetect.system.name = 'Macintosh'; 183 browserDetect.system.version = 'unknown'; 184 } else { // Other 185 browserDetect.system.name = 'unknown'; 186 browserDetect.system.version = 'unknown'; 187 } 188 })();
1 /* 2 源码作者: 石不易(Louis Shi) 3 联系方式: http://www.shibuyi.net 4 =================================================================================================== 5 插件: 6 1. 元素隐藏 7 2. 元素显示 8 3. 鼠标移入移出 9 */ 10 11 $().plugins(hide, show, hover); 12 13 // 元素隐藏 14 function hide() { 15 this.css('display', 'none'); 16 return this; 17 } 18 19 // 元素显示 20 function show() { 21 this.css('display', 'block'); 22 return this; 23 } 24 25 // 鼠标移入移出 26 function hover(overMethod, outMethod) { 27 this.nodeEvent('mouseover', overMethod); 28 this.nodeEvent('mouseout', outMethod); 29 return this; 30 }
1 /* 2 源码作者: 石不易(Louis Shi) 3 联系方式: http://www.shibuyi.net 4 =================================================================================================== 5 插件: 6 1. 元素垂直居中 7 2. 元素水平居中 8 3. 元素垂直水平居中 9 */ 10 11 $().plugins(yCenter, xCenter, center); 12 13 // 元素垂直居中 14 function yCenter(animationMode) { 15 if (typeof animationMode == 'undefined') animationMode = false; 16 var windowHeight = Base.getWindowRectangle().height; 17 var innerHeight = 0, y = 0, elementNode = null; 18 for (var i = 0; i < this.elements.length; i ++) { 19 elementNode = this.elements[i]; 20 Base.absolute(elementNode); 21 innerHeight = Base.getInnerRectangle(elementNode).height; 22 y = (windowHeight - innerHeight) / 2; 23 if (y < 0) y = 0; 24 y += Base.scroll().y; 25 if (!!animationMode) { // 动画模式 26 $(elementNode).animation({ 27 action : 'y', 28 target : y, 29 isBuffer : true, 30 speed : 10 31 }); 32 } else { 33 $(elementNode).css('top', y + 'px'); 34 } 35 } 36 return this; 37 } 38 39 // 元素水平居中 40 function xCenter(animationMode) { 41 if (typeof animationMode == 'undefined') animationMode = false; 42 var windowWidth = Base.getWindowRectangle().width; 43 var innerWidth = 0, x = 0, elementNode = null; 44 for (var i = 0; i < this.elements.length; i ++) { 45 elementNode = this.elements[i]; 46 Base.absolute(elementNode); 47 innerWidth = Base.getInnerRectangle(elementNode).width; 48 x = (windowWidth - innerWidth) / 2; 49 if (x < 0) x = 0; 50 x += Base.scroll().x; 51 if (!!animationMode) { 52 $(elementNode).animation({ 53 action : 'x', 54 target : x, 55 isBuffer : true, 56 speed : 10 57 }); 58 } else { 59 $(elementNode).css('left', x + 'px'); 60 } 61 } 62 return this; 63 } 64 65 // 元素垂直水平居中 66 function center(animationMode) { 67 if (typeof animationMode == 'undefined') animationMode = false; 68 var windowRectangle = Base.getWindowRectangle(); 69 var scroll = Base.scroll(); 70 var innerRectangle = 0, x = 0, y = 0, elementNode = null; 71 for (var i = 0; i < this.elements.length; i ++) { 72 elementNode = this.elements[i]; 73 Base.absolute(elementNode); 74 innerRectangle = Base.getInnerRectangle(elementNode); 75 x = (windowRectangle.width - innerRectangle.width) / 2; 76 y = (windowRectangle.height - innerRectangle.height) / 2; 77 if (x < 0) x = 0; 78 if (y < 0) y = 0; 79 x += scroll.x; 80 y += scroll.y; 81 if (!!animationMode) { 82 $(elementNode).animation({ 83 multiple : { 84 x : x, 85 y : y 86 }, 87 isBuffer : true, 88 speed : 10 89 }); 90 } else { 91 $(elementNode).css(['left : ' + x + 'px', 'top : ' + y + 'px']); 92 } 93 } 94 return this; 95 }
1 /* 2 源码作者: 石不易(Louis Shi) 3 联系方式: http://www.shibuyi.net 4 =================================================================================================== 5 插件: 6 1. 遮罩锁屏 7 2. 清除锁屏 8 */ 9 10 $().plugins(lock, unlock); 11 12 // 遮罩锁屏 13 function lock(animationMode) { 14 if (typeof animationMode == 'undefined') animationMode = false; 15 var screen = null; 16 if ((screen = $('#screen_lock').getNodes()) != this.info) { 17 if (!Base.empty(screen)) { 18 Base.removeElement('id = screen_lock'); 19 screen = Base.addElement('div', 'id = screen_lock', null, document.body); 20 }; 21 } else { 22 screen = Base.addElement('div', 'id = screen_lock', null, document.body); 23 } 24 Base.absolute(screen); 25 var scroll = Base.scroll(); 26 $(document.documentElement).css('overflow', 'hidden'); 27 var windowRectangle = Base.getWindowRectangle(); 28 Base.scroll(scroll.x, scroll.y); 29 $(screen).css([ 30 'color : green', 31 'zIndex : 9998', 32 'top : ' + scroll.y + 'px', 33 'left : ' + scroll.x + 'px', 34 'width : ' + windowRectangle.width + 'px', 35 'height : ' + windowRectangle.height + 'px', 36 'background-color : black' 37 ]).show(); 38 if (!!animationMode) { 39 $(screen).animation({ 40 action : 'o', 41 start : 0, 42 target : 40, 43 isBuffer : true, 44 speed : 10 45 }); 46 } else { 47 $(screen).css(['opacity : 0.4', 'filter : alpha(opacity = 40)']); 48 } 49 Base.fixed(screen); 50 return this; 51 } 52 53 // 清除锁屏 54 function unlock(animationMode) { 55 if (typeof animationMode == 'undefined') animationMode = false; 56 var screen = null; 57 if (!(screen = $('#screen_lock').getNodes()) != this.info && Base.empty(screen)) { 58 if (!!animationMode) { 59 $(screen).animation({ 60 action : 'o', 61 target : 0, 62 isBuffer : true, 63 speed : 10, 64 method : close 65 }); 66 } else { 67 close(); 68 } 69 } 70 function close() { 71 $(screen).hide(); 72 var scroll = Base.scroll(); 73 $(document.documentElement).css('overflow', 'auto'); 74 Base.scroll(scroll.x, scroll.y); 75 } 76 return this; 77 }
1 /* 2 源码作者: 石不易(Louis Shi) 3 联系方式: http://www.shibuyi.net 4 =================================================================================================== 5 插件: 元素拖拽 6 */ 7 8 $().plugins(drag); 9 10 // 元素拖拽 11 function drag(selector) { 12 var elementNode = null, targetNode = null; 13 for (var i = 0; i < this.elements.length; i ++) { 14 elementNode = this.elements[i]; 15 if (typeof selector == 'string') { // 拖拽子节点 16 if ((targetNode = $(elementNode).selector(selector).getNodes()) == this.info) continue; 17 } else { // 拖拽自身 18 targetNode = elementNode; 19 } 20 if (targetNode instanceof Array) { // 集群 21 for (var j = 0; j < targetNode.length; j ++) { 22 $(targetNode[j]).css('cursor', 'move').nodeEvent('mousedown', down); 23 } 24 } else { // 单一 25 $(targetNode).css('cursor', 'move').nodeEvent('mousedown', down); 26 } 27 Base.fixed(elementNode); 28 } 29 function down(event) { 30 Base.absolute(elementNode); 31 if (Base.empty(elementNode)) event.preventDefault(); // Firefox Bug 32 Tool.setCaptureIE(elementNode); 33 var outerRectangle = Base.getOuterRectangle(elementNode); 34 var windowRectangle = Base.getWindowRectangle(); 35 var innerRectangle = Base.getInnerRectangle(elementNode); 36 var scroll = Base.scroll(); 37 var fixedX = event.clientX - outerRectangle.left; 38 var fixedY = event.clientY - outerRectangle.top; 39 var minX = scroll.x; 40 var minY = scroll.y; 41 var maxX = (windowRectangle.width - innerRectangle.width) + scroll.x; 42 var maxY = (windowRectangle.height - innerRectangle.height) + scroll.y; 43 $(document).nodeEvent('mousemove', move); 44 $(document).nodeEvent('mouseup', up); 45 function move(event) { 46 var x = event.clientX - fixedX; 47 var y = event.clientY - fixedY; 48 if (x < minX) x = minX; 49 if (x > maxX) x = maxX; 50 if (y < minY) y = minY; 51 if (y > maxY) y = maxY; 52 $(elementNode).css(['left : ' + x + 'px', 'top : ' + y + 'px']); 53 } 54 function up() { 55 Tool.releaseCaptureIE(elementNode); 56 $(this).nodeEvent('mousemove', move, false); 57 $(this).nodeEvent('mouseup', up, false); 58 } 59 } 60 return this; 61 }
1 /* 2 源码作者: 石不易(Louis Shi) 3 联系方式: http://www.shibuyi.net 4 =================================================================================================== 5 插件: 禁止元素溢出可视区 6 */ 7 8 $().plugins(overflow); 9 10 // 禁止元素溢出 11 function overflow() { 12 var scroll = Base.scroll(); 13 var windowRectangle = Base.getWindowRectangle(); 14 var outerRectangle = null, innerRectangle = null, elementNode = null; 15 var minX = scroll.x, minY = scroll.y, maxX = 0, maxY = 0, x = 0, y = 0; 16 for (var i = 0; i < this.elements.length; i ++) { 17 elementNode = this.elements[i]; 18 Base.absolute(elementNode); 19 outerRectangle = Base.getOuterRectangle(elementNode); 20 innerRectangle = Base.getInnerRectangle(elementNode); 21 x = outerRectangle.left; 22 y = outerRectangle.top; 23 maxX = (scroll.x + windowRectangle.width) - innerRectangle.width; 24 maxY = (scroll.y + windowRectangle.height) - innerRectangle.height; 25 if (x < minX) x = minX; 26 if (x > maxX) x = maxX; 27 if (y < minY) y = minY; 28 if (y > maxY) y = maxY; 29 $(elementNode).css(['left : ' + x + 'px', 'top : ' + y + 'px']); 30 } 31 return this; 32 }
1 /* 2 源码作者: 石不易(Louis Shi) 3 联系方式: http://www.shibuyi.net 4 =================================================================================================== 5 插件: 元素动画 6 */ 7 8 $().plugins(animation); 9 10 // 元素动画 11 function animation(animation) { 12 var elementNode = null, animationList = []; 13 for (var i = 0; i < this.elements.length; i ++) { 14 elementNode = this.elements[i]; 15 if (i == 0 && !(animation = Base.checkAnimation(animation, elementNode))) { 16 return this; 17 } 18 var unique = ''; 19 for (var j = 0; j < animation.action.length; j ++) { 20 // 组合唯一标识符(用于定时器) 21 unique += animation.action[j]; 22 if (animation.action[j] == 'opacity') { 23 $(elementNode).css('opacity', animation.start[j] / 100); 24 $(elementNode).css('filter', 'alpha(opacity=' + animation.start[j] + ')'); 25 } else { 26 // 设置初始值 27 $(elementNode).css(animation.action[j], animation.start[j] + 'px'); 28 } 29 } 30 // 清除动画唯一标识 31 if (typeof elementNode[unique] == 'number') clearTimeout(elementNode[unique]); 32 33 (function (elementNode) { // 警告: IE 6/7/8 出现多定时器, 多重触发动画时, 会严重卡顿 34 // 定时器 35 elementNode[unique] = setTimeout(function () { 36 clearTimeout(elementNode[unique]); 37 var value = 0, target = 0, step = 0, action = '', flags = []; 38 for (var i = 0; i < animation.action.length; i ++) { 39 // 动画动作 40 action = animation.action[i]; 41 // 目标量 42 target = animation.target[i]; 43 // 步长量 44 step = animation.step[i]; 45 46 if (action == 'opacity') { 47 if (browserDetect.ie && browserDetect.browser.version < 9) { 48 var opacity = Base.trim($(elementNode).css('filter')); 49 value = Base.number(/alpha\(opacity([\s\t ]*)=\1(\d+)\)/i.exec(opacity)[2]); 50 } else { 51 value = $(elementNode).css('opacity') * 100; 52 } 53 } else { 54 // 获取当前值 55 value = parseInt(Base.replace($(elementNode).css(action), 'px')); 56 } 57 58 // 缓冲处理 59 if (animation.isBuffer) { 60 var speed = (target - value) / animation.speed; 61 step = step > 0 ? Math.ceil(speed) : Math.floor(speed); 62 } 63 64 if (target == value || step == 0) continue; 65 66 // 检测动画当前值 67 if (step > 0 && Math.abs(target - value) <= step) { // 结束(递增) 68 setAnimation(target); 69 flags[i] = false; 70 } else if (step < 0 && Math.abs(value - target) <= Math.abs(step)) { // 结束(递减) 71 setAnimation(target); 72 flags[i] = false; 73 } else { // 继续执行动画 74 setAnimation(value + step); 75 flags[i] = true; 76 } 77 } 78 79 // 执行动画 80 function setAnimation(value) { 81 if (action == 'opacity') { 82 $(elementNode).css('opacity', Base.number(value) / 100); 83 $(elementNode).css('filter', 'alpha(opacity=' + Base.number(value) + ')'); 84 } else { 85 $(elementNode).css(action, Base.number(value) + 'px'); 86 } 87 } 88 89 // 遍历动画开关 90 for (var k = 0; k < flags.length; k ++) { 91 if (flags[k]) { 92 flags = true; 93 break; 94 } 95 } 96 97 if (flags === true) { // 继续执行 98 elementNode[unique] = setTimeout(arguments.callee, animation.time); 99 } else { // 执行列队 100 animation.method(); 101 } 102 }, animation.time); 103 })(elementNode); 104 } 105 return this; 106 }
1 /* 2 源码作者: 石不易(Louis Shi) 3 联系方式: http://www.shibuyi.net 4 =================================================================================================== 5 插件: 事件切换器 6 */ 7 8 $().plugins(toggle); 9 10 // 事件切换器 11 function toggle() { 12 var methodList = arguments; 13 if (methodList.length != 0) { 14 var elementNode = null; 15 for (var i = 0; i < this.elements.length; i ++) { 16 elementNode = this.elements[i]; 17 // 闭包实现计数器唯一性 18 (function () { 19 var counter = 0; 20 $(elementNode).nodeEvent('click', function () { 21 methodList[counter ++].call(this); 22 if (counter >= methodList.length) counter = 0; 23 }); 24 })(); 25 } 26 } 27 return this; 28 }
关于 BETA 4.0 测试版核心源码与实例演示的获取请移动至官网下载!
感谢大家积极评测给予意见!
CNBlogs 博客:http://www.cnblogs.com/shibuyi/
CSDN 博客:http://blog.csdn.net/louis_shi/
ITeye 博客:http://shibuyi.iteye.com/