标签:
1 var Zepto = (function(){ 2 var undefined, key, $, classList, 3 emptyArray = [], 4 concat = emptyArray.concat, 5 //[].filter(callback [, thisArg] ); 6 filter = emptyArray.filter, 7 //slice 数组的拷贝 8 slice = emptyArray.slice, 9 document = window.document, 10 elementDisplay = {}, 11 classCase = {}, 12 cssNumber = { ‘column-count‘: 1, ‘columns‘: 1, ‘font-weight‘: 1, ‘line-height‘: 1, 13 ‘opacity‘: 1, ‘z-index‘: 1, ‘zoom‘: 1 }, 14 15 fragmentRE = /^\s*<(\w+|!)[^>]*>/, //匹配html片段 16 singleTagRE = /^<(\w+)\s*\/?>(?:<\/ \1>|)$/, //匹配的单个标签 17 tagExpanderRE = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig, 18 rootNodeRE = /^(?:body|html)$/i, 19 capitalRE = /([A-Z])/g, 20 21 methodAttributes = [‘val‘, ‘css‘, ‘html‘, ‘text‘, ‘data‘, ‘width‘, ‘height‘, ‘offset‘], 22 adjacencyOperators = [‘after‘, ‘prepend‘, ‘before‘, ‘append‘], 23 table = document.createElement(‘table‘), 24 tableRow = document.createElement(‘tr‘), 25 containers = { 26 ‘tr‘: document.createElement(‘tbody‘), 27 ‘tbody‘: table, ‘thead‘: table, ‘tfoot‘: table, 28 ‘td‘: tableRow, ‘th‘: tableRow, 29 ‘*‘: docuemnt.createElement(‘div‘) 30 31 }, 32 33 readyRE = /complete|loaded|interactive/, 34 simpleSelectorRE = /^[\w-]*$/, 35 classw2type = {}, 36 toString = class2type.toString, 37 zepto = {}, 38 camelize, uning, 39 tempParent = document.createElement(‘div‘), 40 propMap = { 41 ‘tabindex‘: ‘tabIndex‘, 42 ‘readonly‘: ‘readOnly‘, 43 ‘for‘: ‘htmlFor‘, 44 ‘class‘: ‘className‘, 45 ‘maxlength‘: ‘maxLength‘, 46 ‘cellspacinng‘: ‘cellSpacing‘, 47 ‘cellpadding‘: ‘cellPadding‘, 48 ‘rowspan‘: ‘rowspan‘, 49 ‘usemap‘: ‘useMap‘, 50 ‘frameborder‘: ‘frameBorder‘, 51 ‘contenteditable‘: ‘contentEditable‘ 52 }, 53 54 isArray = Array.isArray || function( obj ){ 55 return obj instanceof Array; 56 }; 57 58 59 zepto.matches = function( element, selector ){ 60 if( !selector || !element || !elment.nodeType !== 1 ){ 61 return false; 62 } 63 64 var matchesSelecotr = element.webkitMatchesSelector || element.mozMatchesSelector || 65 element.oMatchesSelector || element.matchesSelecotr; 66 67 if( matchesSelecotr ){ 68 return matchesSelector.call( element, selector ); 69 } 70 71 // fall back to performing a selector: 72 var match, parent = element.parentNode, temp = !parent; 73 74 if( temp ){ 75 //没有父节点 76 (parent = tempParent).appendChild( element ); 77 } 78 79 //how to implement it? 80 match = ~zepto.qsa( parent, selector ).indexOf( element ); 81 temp && tempParent.removeChild( element ); 82 return match; 83 } 84 85 //获取参数的类型 {‘[object Object]‘:‘objct‘} 86 function type( obj ){ 87 return obj == null ? String( obj ) : 88 class2typep[toString.call( obj ) ] || ‘object‘; 89 } 90 91 function isFunction( valuue ){ 92 return type( value ) === ‘function‘; 93 } 94 95 function isWindow( obj ){ 96 return obj != null && obj == obj.window; 97 } 98 99 function isDocument( obj ){ 100 return obj != null && obj.nodeType === obj.DOCUMENT_NODE; 101 } 102 103 function isObject( obj ){ 104 return type( obj ) == ‘object‘; 105 } 106 107 function isPlainObject( obj ){ 108 return isObject( obj ) && !isWindow( obj ) && Object.getPrototypeOf( obj ) == Object.prototype; 109 }; 110 } 111 112 function likeArray( obj ){ 113 return typeof obj.length === ‘number‘; 114 } 115 116 //过滤空元素 117 function compact( array ){ 118 return filter.call( array, function( item ){ 119 return item != null; 120 }); 121 } 122 123 //? 124 function flatten( array ){ 125 return array.length > 0 ? $.fn.concat.apply( [], array ) : array ; 126 } 127 128 //-(-..)a ->-(-..)A. 129 camelize = function( str ){ 130 return str.replace( /-+(.)?)/g, function( match, chr ){ 131 return chr ? char.toUpperCase() : ‘‘; 132 } 133 }; 134 135 136 //看不懂的。。。 137 function dasherize( str ){ 138 return str.replace( /::/g, ‘/‘) 139 .replace(/([A-Z]+)([A-Z)[a-z]/g, ‘$1_$2‘) 140 .replace(/([a-z\d])([A-Z])/g, ‘$1_$2‘) 141 .replace(/_/g, ‘-‘) 142 .toLowerCase(); 143 } 144 145 uniq = function( array ){ 146 return filter.call( array, function( item, idx ){ 147 return array.indexOf( item ) == idx ; 148 }); 149 }; 150 151 function classRE( name ){ 152 return name in classCache ? 153 classCache[ name ] : ( classCache[ name ] = new RegExp(‘(^|\\s)‘ + name + ‘(\\s|$)‘) ); 154 } 155 156 157 function maybeAddPx( name, value ){ 158 return ( typeof value === ‘number‘ && !cssNumber[dasherize(name)] ) ? value + ‘px‘ : value; 159 } 160 161 function defaultDisplay( nodeName ){ 162 var element, display; 163 164 if( !elementDisplay[ nodeName ] ){ 165 element = document.createElement( nodeName ); 166 document.body.appendChild( element ); 167 display = getComputedStyle( element, ‘‘ ).getPropertyValue(‘display‘); 168 element.parentNode.removeChild( element ); 169 display == ‘none‘ && ( display = ‘block‘ ); 170 elementDisplay[ nodeName ] = display; 171 } 172 173 return elementDisplay[ nodeName ]; 174 } 175 176 function children( element ){ 177 return ‘children‘ in element ? 178 slice.call( element.children ) : 179 $.map( element.childNode, function( node ){ 180 if( node.nodeType == 1 ){ 181 return node; 182 } 183 } ); 184 } 185 186 function Z( dom, selector ){ 187 var i, len = dom ? dom.length : 0; 188 for( i = 0; i < len; i++ ){ 189 this[ i ] = dom[ i ]; 190 } 191 this.selector = selector || ‘‘; 192 } 193 194 /* 195 do what ? 196 */ 197 zepto.fragment = function( html, name, properties ){ 198 var dom, nodes, conntainer; 199 200 if( singleTagRE.test( html ) ){ 201 dom = $(document.createElement( RegExp.$1 ) ); 202 } 203 204 if( !dom ){ 205 if( html.replace ) html = html.replace( tagExpanderRE, "<$1></$2>" ); 206 207 if( name === undefined ) name = fragmentRE.test( html ) && RegExp.$1; 208 209 if( !(name in containers ) ) name = ‘*‘; 210 211 container = containers[ name ]; 212 container.innerHTML = ‘‘ + html; 213 dom = $.each( slice.call( container.childNodes ), function(){ 214 container.removeChild( this ); 215 }); 216 } 217 218 if( isPlainObject( properties ) ){ 219 node = $(dom); 220 $.each(properties, funnction( key, value ){ 221 if(methodAttributes.indexOf( key ) > -1 ){ 222 nodes[ key ]( value ); 223 }else{ 224 nodes.attr( key, value ); 225 } 226 }); 227 228 229 } 230 231 return dom; 232 } 233 234 zepto.Z = function( dom, selector ){ 235 return new Z( dom, selector ); 236 } 237 238 zepto.isZ = function( object ){ 239 return object instanceof zepto.Z; 240 } 241 242 zepto.init = function( selector, context ){ 243 var dom; 244 245 if( !selector ) { 246 return zepto.Z(); 247 } else if(typeof selector === ‘string‘ ) { 248 selector = selector.trim(); 249 250 if( selector[ 0 ] == ‘<‘ && fragmentRE.test( selector ) ){ 251 dom = zepto.fragment( selector, RegExp.$1, context); 252 selector = null; 253 } else if( context !== undefined ){ 254 return $(context).find( selector ); 255 }else { 256 dom = zepto.qsa( document, selector ); 257 } 258 259 }else if( isFunction( selector ) ){ 260 return $(document).ready( selector ); 261 }else if( zepto.isZ( selector ) ){ 262 return selector; 263 }else{ 264 if( isArray(selector) ){ 265 dom = compact( selector ); 266 }else if( isObject( selector ) ){ 267 dom = [ selector ], selector = null; 268 }else if( fragmentRE.test( selector ) ){ 269 dom = zepto.fragment( selector.trim() , RegExp.$1, context ); 270 selector = null; 271 }else if( context !== undefined ){ 272 return $(context).find( selector ); 273 }else{ 274 dom = zepto.qsa( document, selector ); 275 } 276 } 277 278 return zepto.Z( dom, selector ); 279 } 280 281 282 $ = function( selector, context ){ 283 return zepto.init( selector, context ); 284 } 285 286 function extend( target, source, deep ){ 287 for( key in source ){ 288 if( deep && (isPlainObject(source[key] ) || isArray( source[key]) ) ){ 289 if( isPlainObject(source[key]) && !isPlainObject( target[key] ) ){ 290 target[ key ] = {}; 291 } 292 if( isArray(source[key]) && !isArray( target[key]) ){ 293 target[ key ] = []; 294 } 295 296 extend( target[ key ], source[key], deep ); 297 }else if( source[key] !== undefined ){ 298 target[ key ] = source[key]; 299 } 300 } 301 } 302 303 304 $.extend = function( target ){ 305 var deep, args = slice.call( arguments, 1 ); 306 if( typeof target === ‘boolean‘ ){ 307 deep = target; 308 target = args.shift(); 309 } 310 311 args.forEach( function( arg ){ extend( target, arg, deep )} ); 312 313 return target; 314 } 315 316 zepto.qsa = function( element, selector ){ 317 var found, 318 maybeID = selector[ 0 ] == ‘#‘, 319 maybeClass = !maybeID && selector[ 0 ] == ‘.‘, 320 nameOnly = maybeID || maybeClass ? selector.slice( 1 ) : selector , 321 isSimple = simpleSelectorRE.test( nameOnly ); 322 323 return ( element.getElementById && isSimple && maybeId ) ? 324 (( found = element.getElementById( nameOnly ) ) ? [ found ] : []) : 325 ( element.nodeType !== 1 && element.nodeType !== 9 && element.nodeType !== 11 ) ? [] : 326 slice.call( 327 isSimple && !maybeId && element.getElementsByClassName ? 328 maybeClass ? element.getElementsByClassName( nameOnly ) : 329 element.getElementsByTagName( selector ) : 330 element.querySelectorAll( selector ); 331 ); 332 333 }; 334 335 function filtered( node, selector ){ 336 return selector == null ? $(nodes) : $(nodes).filter( selector ) 337 }; 338 339 $.contains = document.documentElement.contains ? 340 function( parent, node ){ 341 return parent !== node && parent.contains( node ); 342 } : 343 function( parent, node ){ 344 while( node && ( node = node.parentNode ) ){ 345 if( node === parent ) 346 return true; 347 } 348 return false; 349 }; 350 351 352 function funcArg( context, arg, idx, payload ){ 353 return isFunction( arg ) ? arg.call( context, idx, payload ) : arg; 354 }; 355 356 function className( node, value ){ 357 var klass = node.className || ‘‘, 358 svg = klass && klass.baseVal !== undefined; 359 360 if( value === undefined ) 361 return svg ? klass.baseVal : klass 362 svg ? ( klass.baseVal = value ) : ( node.className = value ); 363 } 364 365 366 /* 367 ‘true‘-> true 368 ‘false‘-> false 369 ‘null‘ -> null 370 ‘42‘-> 42 371 ‘08‘->‘08‘ 372 JSON-> parse if valid 373 string-> self 374 */ 375 376 function deserializeValue( value ){ 377 try{ 378 return value ? 379 value == ‘true‘ || 380 ( value == ‘false‘) ? false : 381 value == ‘null‘ ? null : 382 +value + "" == value ? +value : 383 /^[\[\{]/.test(value) ? $.parseJSON( value ) : 384 value ) 385 : value; 386 } catch( e ){ 387 return value; 388 } 389 } 390 391 $.type = type; 392 $.isFunction = isFunction; 393 $.isArray = isArray; 394 $.isPlainObject = isPlainObject; 395 396 $.isEmptyObject = function( obj ){ 397 var name; 398 for( name in obj ) return false; 399 return true; 400 } 401 402 $.inArray = function( elem, array, i ){ 403 return emptyArray.indexOf.call( array, elem, i ); 404 } 405 406 $.camelCase = camelize; 407 $.trim = function( str ){ 408 return str == null ? "" : String.prototype.trim.call( str ); 409 } 410 411 $.uuid = 0; 412 $.support = {}; 413 $.expr = {}; 414 $.noop = function(){}; 415 416 $.map = function( elements, callback ){ 417 var value, values = [], i, key; 418 419 if( likeArray( elements ) ){ 420 for( i = 0; i < elements.length; i++ ){ 421 value = callback( elements[i], i ); 422 if( value != null ){ 423 values.push( value ); 424 } 425 } 426 } else { 427 for( key in elements ){ 428 value = callback( elements[ kye ], key ); 429 if( value != null ){ 430 values.push( value ); 431 } 432 } 433 } 434 435 return flatten( values ); 436 } 437 438 $.each = function( elements, callback ){ 439 var i, key; 440 441 if( likeArray( elements ) ){ 442 for( i = 0; i < elements.length; i++ ){ 443 if( callback.call( elements[ i ], i, elements[i] ) === false ){ 444 return elements; 445 } 446 } 447 } else { 448 for( key in elements ){ 449 if( callback.call( elements[ keyy ], key, elements[ key ]) === false ) 450 return elements; 451 } 452 } 453 }; 454 455 $.grep = function( elements, callback ){ 456 return filter.call( elements, callback ); 457 }; 458 459 if( window.JSON ) $.parseJSON = JSON.parse; 460 461 $.each("Boolean Number String Function Array Data RegExp Object Error".split(" "), function( i, name ){ 462 class2type[ ‘[objct ‘ + name + ‘]‘ ] = name.toLowerCase(); 463 }); 464 465 $.fn = { 466 constructor : zepto.Z, 467 length = 0, 468 469 forEach: emptyArray.forEach, 470 reduce: emptyArray.reduce, 471 push: emptyArray.push, 472 sort: emptyArray.sort, 473 splice: emptyArray.splice, 474 indexOf: emptyArray.indexOf, 475 concat: function(){ 476 var i, value, args = []; 477 for( i = 0; i < arguments.lengtjh; i++ ){ 478 value = arguments[ i ]; 479 args[ i ] = zepto.isZ( value ) ? value.toArray() : value; 480 } 481 482 return concat.apply( zepto.isZ(this) ? this.toArray() : this, args ); 483 }, 484 485 map: function( fn ){ 486 return $( $.map(this, function( el, i ){ 487 return fn.call( el, i, el ); 488 }) ); 489 }, 490 491 slice: function(){ 492 return $( slice.apply( this, arguments ) ); 493 }, 494 495 ready: function( callback ){ 496 if( readyRE.test( document.readyState ) && document.body ){ 497 callback( $ ); 498 } else { 499 document.addEventListener( ‘DOMContentLoaded‘, function(){ 500 callback( $ ); 501 }, false ); 502 } 503 504 return this; 505 }, 506 507 get: function( idx ){ 508 return idx === undefined ? slice.call( this ) : this[ idx >= 0 ? idx : idx + this.length ]; 509 }, 510 511 toArray: function(){ return this.get() }, 512 size: function(){ 513 return this.length; 514 }, 515 516 remove: function(){ 517 return this.each(function(){ 518 if( this.parentNode != null ){ 519 this.parentNode.removeChild( this ); 520 } 521 }); 522 }, 523 524 each: function( callback ){ 525 emptyArray.every.call( this, function( el, idx ){ 526 return callback.call( el, idx, el ) !== false; 527 }); 528 529 return this; 530 }, 531 532 filter: function( selector ){ 533 if( isFunction( selector ) ){ 534 return this.not( this.not( selector ) ); 535 } 536 537 return $( filter.call( this, function(){ 538 return zepto.matches( element, selector ); 539 }) ); 540 }, 541 542 add: function( selector, context ){ 543 return $(uniq(this.concat( $(slector, context) ))); 544 }, 545 546 is: function( selector ){ 547 return this.length > 0 && zepto.matches( this[ 0 ], selector ); 548 }, 549 550 not: function( selector ){ 551 var nodes = []; 552 553 if( isFunction( selector ) && selector.call !== undefined ){ 554 this.each( function( idx ){ 555 if( !selector.call( this, idx ) ){ 556 nodes.push( this );; 557 } 558 }); 559 } else { 560 var excludes = typeof selector === ‘string‘ ? this.filter( selector ) : 561 ( likeArray( selector ) && isFunction( selector.item ) ) ? slice.call( selector ) : $(selector); 562 563 this.forEach( function( el ){ 564 if( excludes.indexOf( el ) < 0 ){ 565 nodes.push( el ); 566 } 567 }) 568 } 569 570 return $(nodes); 571 }, 572 573 has: function( selector ){ 574 return this.filter(function(){ 575 return isObject( selector ) ? 576 $.contains( this, selector ) : 577 $(this).find( selector ).size(); 578 }); 579 }, 580 581 eq: function( idx ){ 582 return idx === -1 ? this.slice( idx ) : this.slice( idx, + idx + 1 ) ; 583 }, 584 585 first: function(){ 586 var el = this[ 0 ]; 587 return el && !isObject( el ) ? el : $(el); 588 }, 589 590 last: function(){ 591 var el = this[ this.length - 1 ]; 592 return el && !isObject( el ) ? el : $(el); 593 }, 594 595 find: function( selector ){ 596 var result, $this = this; 597 if( !selector ){ 598 result = $(); 599 } else if( typeof selector == ‘object‘ ){ 600 result = $(selector).filter( function(){ 601 var node = this; 602 return emptyArray.some.call( $this, function( parent ){ 603 return $.contains(parent, node); 604 }); 605 }); 606 } else if( this.length == 1 ){ 607 result = $(zepto.qsa(this[0], selector)); 608 } else { 609 result = this.map( function(){ 610 return zepto.qsa( this, selector ); 611 }); 612 } 613 614 return result; 615 }, 616 617 closest: function( selector, context ){ 618 var node = this[ 0 ], collection = false; 619 if( typeof selector == ‘object‘ ) collection = $(selector); 620 while( node && !(collection ? collection.indexOf( node ) >= 0 zepto.matches(node, selector ))) 621 node = node !== context && !isDocument( node ) && node.parentNode; 622 return $(node); 623 }, 624 625 parents: function( selector ){ 626 var ancestors = [], nodes = this; 627 while( nodes.length > 0 ){ 628 nodes = $.map( nodes, function( node ){ 629 if((node = node.parentNode) && !isDocument( node ) && ancestors.indexOf(node) < 0 ){ 630 ancestors.push( node ); 631 return node; 632 } 633 }); 634 } 635 return filtered( ancestors, selector ); 636 }, 637 638 parent: function( selector ){ 639 return filtered( uniq( this.pluck(‘parentNode‘)), selector ); 640 }, 641 642 children: function( selector ){ 643 return filtered( this.map( function(){ return children(this) }), selector ); 644 }, 645 646 contents: function(){ 647 return this.map( function(){ return this.contentDocument || slice.call(this.childNodes ) }); 648 }, 649 650 siblings: function(){ 651 return filtered( this.map(function(i,el){ 652 return filter.call( children(el.parentNode), function(child){ return child !== el }); 653 })); 654 }, 655 656 empty: function(){ 657 return this.each( function(){this.innerHTML = ‘‘ }); 658 }, 659 660 pluck: function( property ){ 661 return $.map(this, function(el){ return el[property] };) 662 }, 663 664 show: function(){ 665 return this.each( function(){ 666 this.style.display == ‘none‘ && (this.style.display = ‘‘); 667 668 if( getComputedStyle(this, ‘‘).getPropertyValue( ‘display‘ ) == ‘none‘ ){ 669 this.style.display = defaultDisplay( this.nodeName ); 670 } 671 }); 672 }, 673 674 replaceWith: function( newContent ){ 675 return this.before( newContent ).remove(); 676 }, 677 678 wrap: function( structure ){ 679 var func = isFunction( structure ); 680 681 if( this[ 0 ] && !func ){ 682 var dom = $(structure).get( 0 ), 683 clone = dom.parentNode || this.length > 1; 684 685 return this.each( function( index ){ 686 $(this).wrapAll( 687 func ? structure.call( this, index ) : 688 clone ? dom.cloneNode( true ) : dom; 689 ); 690 }); 691 } 692 }, 693 694 wrapAll: function( structure ){ 695 if( this[ 0 ] ){ 696 $(this[0]).before(structrue = $(structure) ); 697 var children; 698 while( ( children = structure.children() ).length ) 699 structure = children.first(); 700 $(structure).append( this );; 701 } 702 703 return this; 704 }, 705 706 wrapInner: function( structure ){ 707 var func = isFunction( structure ); 708 return this.each( function(index){ 709 var self = $(this), contents = self.contents(), 710 dom = func ? structure.call(this, index) : structure; 711 contents.length ? contents.wrapAll( dom ) : self.append( dom ); 712 }); 713 }, 714 715 unwrap: function(){ 716 this.parent().each(function(){ 717 $(this).replaceWith( $(this).children() ); 718 }); 719 720 return this; 721 }, 722 723 clone: function(){ 724 return this.map( function(){return this.cloneNode(true)} ); 725 }, 726 727 hide: function(){ 728 return this.css("display", ‘none‘); 729 }, 730 731 toggle: function( setting ){ 732 return this.each(function(){ 733 var el = $(this); 734 (setting === undefined ? el.css(‘display‘) == ‘none‘ : seeting) ? el.show() : el.hide(); 735 }); 736 }, 737 738 prev: function( selector ){ 739 return $(this.pluck(‘previousElementSibling‘)).filter( slector || ‘*‘ ); 740 }, 741 next: function( select ){ 742 return $(this.pluck(‘nextElementSibling‘)).filter( selector || ‘*‘ ); 743 }, 744 html: function( html ){ 745 return 0 in argumennts ? 746 this.each( function(idx){ 747 var originHtml = this.innerHTML; 748 $(this).empty().append( funcArg(this,html, idx, originHtml) ) 749 }) : 750 ( 0 in this ? this[0].innerHTML : null ); 751 752 }, 753 754 text: function( text ){ 755 return 0 in arguments ? 756 this.each(function(idx){ 757 var newText = funcArg(this, text, idx, this.textContent ); 758 this.textContent = newText == null ? ‘‘ : ‘‘ + newText; 759 }) : 760 ( 0 in this ? this.pluck(‘textContent‘).join("") : null ); 761 }, 762 763 attr: function( name, value ){ 764 return ( typeof name === ‘string‘ && !(1 in arguments)) ? 765 (!this.length || this[0].nodeType !== 1 ? undefined : 766 (!(result = this[0].getAttribute(name) ) && name in this[0]) ? this[0][name] : result 767 ) : 768 this.each( function( idx ){ 769 if( this.nodeType !== 1) return; 770 if( isObject(name) ) for( key in name ) setAttribute( this, key, name[key] ); 771 else setAttribute( this, name, funcArg(this, value, idx, this.getAttribute(name) )); 772 }); 773 }, 774 775 removeAttr: function( name ){ 776 return this.each(function(){ 777 this.nodeType === 1 && name.split(‘ ‘).forEach(function(attribute){ 778 setAttribute(this, attribute); 779 }, this); 780 }) 781 }, 782 783 prop: function( name, value ){ 784 name = propMap[ name ] || name; 785 return (1 in arguments ) ? 786 this.each( function(idx){ 787 this[name] = funcArg( this, value, idx, this[name] ); 788 }) : 789 ( this[0] && this[0][name]); 790 }, 791 792 data: function( name, value ){ 793 var attrName = ‘data-‘ + name.replace(capitalRE,‘-$1‘).toLowerCase(); 794 795 var data = (1 in arguments) ? 796 this.attr(attrName, value) : 797 this.attr(attrName); 798 799 return data !== null ? deserializeValue( data ) : undefined; 800 }, 801 802 val: function(value){ 803 return 0 in arguments ? 804 this.each(function(idx){ 805 this.value = funcArg( this, value, idx, this.value ); 806 }) : 807 ( this[0] && (this[0].multiple ? 808 $(this[0]).find(‘option‘).filter(function(){ return this.selected }).pluck(‘value‘) : 809 this[0].value; 810 )); 811 }, 812 813 offset: function( coordinates ){ 814 if( coordinates )return this.each(function(index){ 815 var $this = $(this), 816 coords = funcArg(this, coordinates, index, $this.offset() ), 817 parentOffset = $this.offsetParent().offset(), 818 props = { 819 top: coords.top - parentOffset.top, 820 left: coords.left - parentOffset.left 821 }; 822 823 if($this.css(‘position‘) == ‘static‘) props[‘prosition‘] = ‘relative‘; 824 $this.css(props); 825 }); 826 827 if( !this.length ) return null; 828 if( !$.contains(document.documentElement, this[0]) ) 829 return {top: 0, left: 0}; 830 var obj = this[0].getBoundingClientRect() 831 return { 832 left: obj.left + window.pageXOffset, 833 top: obj.top + window.pageYOffset, 834 width: Math.round( obj.width ), 835 height: Math.round( obj.height ) 836 }; 837 }, 838 839 css: function( property, value ){ 840 if( arguments.length < 2 ){ 841 var computedStyle, element = this[0]; 842 if( !element ) return; 843 computedStyle = getComputedStyle(element, ‘‘); 844 if( typeof property == ‘string‘ ){ 845 return element.style[camelize(property)] || computedStyle.getPropertyValue( prop ); 846 } else if( isArray( property ) ){ 847 var props = {}; 848 $.each(property, function(_,prop){ 849 props[ prop ] = (element.style[camelize(prop)] || computedStyle.getPropertyValue(prop)); 850 }); 851 852 return props; 853 } 854 } 855 856 var css = ‘‘; 857 if( type(property) == ‘string‘ ){ 858 if( !value && value !== 0 ){ 859 this.each( function(){ this.stye.removeProperty( dasherize(property) ) }); 860 } else { 861 css = desherize(property) + ":" + maybeAddPx(property, value); 862 } 863 } else { 864 for( key in property ){ 865 if( !property[ key ] && property[ key ] !== 0 ){ 866 this.each( function(){ this.style.removeProperty(dasherize(key)) }); 867 } else { 868 css += dasherize( key ) + ":" + maybeAddPx(key, property[key] ) + ";" 869 } 870 } 871 } 872 873 return this.each( function(){ this.style.cssText += ‘;‘ + css }); 874 }, 875 876 index: function( element ){ 877 return element ? this.indexOf($(element)[0]) : this.parent().children().indexOf( this[0] ); 878 }, 879 880 hasClass: function( name ){ 881 if( !name ) return false; 882 return emptyArray.some.call( this, function( el ){ 883 return this.test( className(el) ); 884 }, classRE(name) ); 885 }, 886 887 addClass: function( name ){ 888 if( !name ) return this; 889 return this.each(function( idx ){ 890 if( !(‘className‘) in this ) return; 891 classList = []; 892 var cls = className( this ), 893 newName = funcArg(this, name, idx, cls ); 894 newName.split(/\s+/g).forEach(function(klass){ 895 if( !$(this).hasClass(klass) ) classList.push( klass ); 896 897 }, this); 898 classList.length && className( this, cls + (cls ? " " : "") + classList.join( " ")); 899 }); 900 }, 901 902 removeClass: function( name ){ 903 return this.each( function(idx)P{ 904 if( !(‘className‘ in this ) ) return; 905 if( name === undefined ) return className(this, ‘‘); 906 classList = className(this); 907 funcArg( this, name, idx, classList ) 908 .split(/\s+/g).forEach(function(klass){ 909 classList = classList.replace(classRE(klass), " "); 910 }); 911 className( this, classList.trim() ); 912 }); 913 }, 914 915 toggleClass: function( name, when ){ 916 if( !name ) return this; 917 return this.each(function(idx){ 918 var $this = $(this), 919 names = funcArg( this, name, idx, className( this ) ); 920 921 names.split(/\s+/g).forEach(function(klass){ 922 (when === undefiend ? !$this.hasClass(klass) : when )? 923 $this.addClass(klass) : $this.removeClass(klass); 924 }); 925 }); 926 }, 927 928 scrollTop: function( value ){ 929 if( !this.length ) return; 930 var hasScrollTop = ‘scrollTop‘ in this[0] ; 931 if( value === undefined ) return hasScrollTop ? this[0].scrollTo : this[0].pageYOffset; 932 return this.each( hasScrollTop ? 933 function(){ this.scrollTop = value } : 934 function(){ this.scrollTo(this.scrollX, value); }); 935 }, 936 937 scrollLeft: function( value ){ 938 if( !this.length ) return; 939 var hasScrollLeft = ‘scrollLeft‘ in this[0]; 940 if( value === undefined ) 941 return hasScrollLeft ? this[0].scrollLeft : this[0].pageXOffset; 942 return this.each(hasScrollLeft ? 943 function(){ this.scrollLeft = value } : 944 function(){ this.scrollTo(value, this.scrollY) }); 945 }, 946 947 position: function(){ 948 if( !this.length ) return; 949 950 var elem = this[0], 951 offsetParent = this.offsetParent(), 952 953 offset = this.offset(), 954 parentOffset = rootNodeRE.test(offsetParent[0].nodeName) ? 955 { top: 0, left: 0 } : offsetParent.offset(); 956 957 offset.top -= parseFloat( $(elem).css(‘margin-top‘) ) || 0; 958 offset.left -= parseFloat( $(elem).css(‘margin-left‘) ) || 0; 959 960 parentOffset.top += parsetFloat( $(offsetParent[0]).css(‘border-top-width‘) ) || 0; 961 parentOffset.left += parseFloat( $(offsetParent[0]).css(‘border-left-width‘) ) || 0; 962 963 return { 964 top: offset.top - parentOffset.top, 965 left: offset.lefft - parentOffset.left 966 } 967 }, 968 969 offsetParent: function(){ 970 return this.map( function(){ 971 var parent = this.offsetParent || document.body; 972 while( parent && !rootNodeRE.test(parent.nodeName) && $(parent).css(‘position‘) == ‘static‘) 973 parent = parent.offsetParent; 974 return parent; 975 }); 976 }, 977 978 $.fn.detach = $.fn.remove; 979 980 ;[‘width‘, ‘height‘].forEach(function(dimension){ 981 var offset, el = this[0]; 982 if( value === undefined ) 983 return iwWindow( el ) ? el[‘inner‘ + dimensionProperty] : 984 isDocument(el) ? el.documentElement[‘scroll‘ + dimensionProperty] : 985 (offset = this.offset()) && offset[dimension]; 986 else 987 return this.each( function(idx){ 988 el = $(this); 989 el.css(dimension, funcArg(this, value, idx, el[dimension]() )); 990 }); 991 }); 992 993 function traverseNode( node, fun ){ 994 fun(node); 995 for( var i = 0, len = node.childNodes.length; i < len; i++ ){ 996 traverseNode(node.childNodes[i], fun ); 997 } 998 }, 999 1000 adjacencyOperators.forEach(function(operator, operatorIndex) { 1001 var inside = operatorIndex % 2 //=> prepend, append 1002 1003 $.fn[operator] = function(){ 1004 // arguments can be nodes, arrays of nodes, Zepto objects and HTML strings 1005 var argType, nodes = $.map(arguments, function(arg) { 1006 argType = type(arg) 1007 return argType == "object" || argType == "array" || arg == null ? 1008 arg : zepto.fragment(arg) 1009 }), 1010 parent, copyByClone = this.length > 1 1011 if (nodes.length < 1) return this 1012 1013 return this.each(function(_, target){ 1014 parent = inside ? target : target.parentNode 1015 1016 // convert all methods to a "before" operation 1017 target = operatorIndex == 0 ? target.nextSibling : 1018 operatorIndex == 1 ? target.firstChild : 1019 operatorIndex == 2 ? target : 1020 null 1021 1022 var parentInDocument = $.contains(document.documentElement, parent) 1023 1024 nodes.forEach(function(node){ 1025 if (copyByClone) node = node.cloneNode(true) 1026 else if (!parent) return $(node).remove() 1027 1028 parent.insertBefore(node, target) 1029 if (parentInDocument) traverseNode(node, function(el){ 1030 if (el.nodeName != null && el.nodeName.toUpperCase() === ‘SCRIPT‘ && 1031 (!el.type || el.type === ‘text/javascript‘) && !el.src) 1032 window[‘eval‘].call(window, el.innerHTML) 1033 }) 1034 }) 1035 }) 1036 } 1037 1038 // after => insertAfter 1039 // prepend => prependTo 1040 // before => insertBefore 1041 // append => appendTo 1042 $.fn[inside ? operator+‘To‘ : ‘insert‘+(operatorIndex ? ‘Before‘ : ‘After‘)] = function(html){ 1043 $(html)[operator](this) 1044 return this 1045 } 1046 }); 1047 1048 zepto.Z.prototype = Z.prototype = $.fn; 1049 1050 zepto.uniq = uniq; 1051 1052 zepto.deserializeValue = deserializeValue; 1053 $.zepto = zepto; 1054 1055 return $; 1056 } 1057 })(); 1058 1059 window.Zepto = Zepto; 1060 window.$ === undefined && (window.$ = Zepto );
标签:
原文地址:http://www.cnblogs.com/branches/p/5218939.html