-
Notifications
You must be signed in to change notification settings - Fork 0
/
myLibrary.js
507 lines (385 loc) · 13.7 KB
/
myLibrary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
function getById(elemId){
return document.getElementById(elemId);
}
/**
@clsName 搜索的class名称
@[context] 上下文
*/
function getByClass(clsName, context, tag){
var result = [];
context = context || document;
tag = tag || '*';
var arr = context.getElementsByTagName(tag);
for(var i=0; i<arr.length; i++){
var re = new RegExp("\\b"+clsName+"\\b", 'g');
if(re.test(arr[i].className)){
result.push(arr[i]);
}
}
return result;
}
function getByTag(tagName, context){
context = context || document;
return context.getElementsByTagName(tagName);
}
/**
@elem 指定的元素
@name 元素的属性名称
@value 元素的属性值
@return 属性值
*/
function attr(elem, name, value) {
//Make sure that a valid name was provided
if ( !name || name.constructor != String )
return '';
//Figure out if the name is one of the weird naming cases
name = { 'for': 'htmlFor', 'class': 'className' }[name] || name;
// If the user is setting a value, also
if ( value != null ) {
// Set the quick way first
elem[name] = value;
// If we can, use setAttribute
if ( elem.setAttribute )
elem.setAttribute(name,value);
}
// Return the value of the attribute
return elem[name] || elem.getAttribute(name) || '';
}
function getStyle( elem, name ) {
//If the property exists in style[], then it’s been set recently (and is current)
if (elem.style[name])
return elem.style[name];
// Otherwise, try to use IE’s method
else if (elem.currentStyle)
return elem.currentStyle[name];
// Or the W3C’s method, if it exists
else if (document.defaultView && document.defaultView.getComputedStyle) {
// It uses the traditional ‘text-align’ style of rule writing, instead of textAlign
name = name.replace(/([A-Z])/g,"-$1"); //textAlgin=>text-Align
name = name.toLowerCase();//text-Align => text-align
// Get the style object and get the value of the property (if it exists)
var s = document.defaultView.getComputedStyle(elem,"");
return s && s.getPropertyValue(name);
// Otherwise, we’re using some other browser
} else
return null;
}
function setCss(elem, attr, value){
switch(attr){
case 'width':
case 'height':
case 'padding':
case 'paddingLeft':
case 'paddingRight':
case 'paddingTop':
case 'paddingBottom':
value = /\%/.test(value)?value:Math.max(parseInt(value), 0) + 'px';
break;
case 'left':
case 'top':
case 'bottom':
case 'right':
case 'margin':
case 'marginLeft':
case 'marginRight':
case 'marginTop':
case 'marginBottom':
value = /\%/.test(value)?value:parseInt(value) + 'px';
break;
}
elem.style[attr] = value;
}
function css(elem, attr, value){
if(value){
setCss(elem, attr, value);
}else{
if(typeof attr == 'string'){
return getStyle(elem, attr);
}else{
var obj = attr;//{width:300, height:200}
for(var p in obj){
setCss(elem, p, obj[p])
}
}
}
}
function addClass(elem, className){
// elem.className += " " + className;
var re = new RegExp('\\b' + className + '\\b', 'g');
if(!re.test(elem.className)){
elem.className += " " + className;
}
}
function removeClass(elem, className){
// elem.className += " " + className;
var re = new RegExp('\\b' + className + '\\b', 'g');
if(re.test(elem.className)){
elem.className = trim(elem.className.replace(re, ""));//"bb aa" => "bb"
}
}
function trim(str){
return str.replace(/^\s+|\s+$/g, "");
}
// Get the actual height (using the computed CSS) of an element
function getHeight( elem ) {
// Gets the computed CSS value and parses out a usable number
return parseInt( getStyle( elem, 'height' ) );
}
// Get the actual width (using the computed CSS) of an element
function getWidth( elem ) {
// Gets the computed CSS value and parses out a usable number
return parseInt( getStyle( elem, 'width') );
}
// Find the height of the viewport
function windowHeight() {
// A shortcut, in case we’re using Internet Explorer 6 in Strict Mode
var de = document.documentElement;
// If the innerHeight of the browser is available, use that
return self.innerHeight ||
// Otherwise, try to get the height off of the root node
( de && de.clientHeight ) ||
// Finally, try to get the height off of the body element
document.body.clientHeight;
}
// Find the width of the viewport
function windowWidth() {
// A shortcut, in case we’re using Internet Explorer 6 in Strict Mode
var de = document.documentElement;
// If the innerWidth of the browser is available, use that
return self.innerWidth ||
// Otherwise, try to get the width off of the root node
( de && de.clientWidth ) ||
// Finally, try to get the width off of the body element
document.body.clientWidth;
}
// Returns the height of the web page
// (could change if new content is added to the page)
function pageHeight() {
return document.body.scrollHeight;
}
// Returns the width of the web page
function pageWidth() {
return document.body.scrollWidth;
}
// Find the full, possible, height of an element (not the actual,
// current, height)
function fullHeight( elem ) {
// If the element is being displayed, then offsetHeight
// should do the trick, barring that, getHeight() will work
if ( getStyle( elem, 'display' ) != 'none' )
return elem.offsetHeight || getHeight( elem );
// Otherwise, we have to deal with an element with a display
// of none, so we need to reset its CSS properties to get a more
// accurate reading
var old = resetCSS( elem, {
display: 'block',
visibility: 'hidden',
position: 'absolute'
});
// Figure out what the full height of the element is, using clientHeight
// and if that doesn't work, use getHeight
var h = elem.clientHeight || getHeight( elem );
// Finally, restore the CSS properties back to what they were
restoreCSS( elem, old );
// and return the full height of the element
return h;
}
// Find the full, possible, width of an element (not the actual,
// current, width)
function fullWidth( elem ) {
// If the element is being displayed, then offsetWidth
// should do the trick, barring that, getWidth() will work
if ( getStyle( elem, 'display' ) != 'none' )
return elem.offsetWidth || getWidth( elem );
// Otherwise, we have to deal with an element with a display
// of none, so we need to reset its CSS properties to get a more
// accurate reading
var old = resetCSS( elem, {
display: 'block',
visibility: 'hidden',
position: 'absolute'
});
// Figure out what the full width of the element is, using clientWidth
// and if that doesn't work, use getWidth
var w = elem.clientWidth || getWidth( elem );
// Finally, restore the CSS properties back to what they were
restoreCSS( elem, old );
// and return the full width of the element
return w;
}
// A function used for setting a set of CSS properties, which
// can then be restored back again later
function resetCSS( elem, prop ) {
var old = {};
// Go through each of the properties
for ( var i in prop ) {
// Remember the old property value
old[ i ] = elem.style[ i ];
// And set the new value
elem.style[ i ] = prop[i];
}
// Retun the set of changed values, to be used by restoreCSS
return old;
}
// A function for restoring the side effects of the resetCSS function
function restoreCSS( elem, prop ) {
// Reset all the properties back to their original values
for ( var i in prop )
elem.style[ i ] = prop[ i ];
}
// Find the X (Horizontal, Left) position of an element
function pageX(elem) {
var p = 0;
// We need to add up all of the offsets for every parent
while ( elem.offsetParent ) {
// Add the offset to the current count
p += elem.offsetLeft;
// and continue on to the next parent
elem = elem.offsetParent;
}
return p;
}
// Find the Y (Vertical, Top) position of an element
function pageY(elem) {
var p = 0;
// We need to add up all the offsets for every parent
while ( elem.offsetParent ) {
// Add the offset to the current count
p += elem.offsetTop;
// and continue on to the next parent
elem = elem.offsetParent;
}
return p;
}
// Find the left position of an element
function posX(elem) {
// Get the computed style and get the number out of the value
return parseInt( getStyle( elem, "left" ) );
}
// Find the top position of an element
function posY(elem) {
// Get the computed style and get the number out of the value
return parseInt( getStyle( elem, "top" ) );
}
// Find the horizontal positioing of an element within its parent
function parentX(elem) {
// If the offsetParent is the element’s parent, break early
return elem.parentNode == elem.offsetParent ?
elem.offsetLeft :
// Otherwise, we need to find the position relative to the entire
// page for both elements, and find the difference
pageX( elem ) - pageX( elem.parentNode );
}
// Find the vertical positioing of an element within its parent
function parentY(elem) {
// If the offsetParent is the element’s parent, break early
return elem.parentNode == elem.offsetParent ?
elem.offsetTop :
// Otherwise, we need to find the position relative to the entire
// page for both elements, and find the difference
pageY( elem ) - pageY( elem.parentNode );
}
// Find the horizontal position of the cursor
function getX(e) {
// Normalize the event object
e = e || window.event;
// Check for the non-IE position, then the IE position, and finally return 0
return e.pageX || e.clientX + document.body.scrollLeft || 0;
}
// Find the vertical position of the cursor
function getY(e) {
// Normalize the event object
e = e || window.event;
// Check for the non-IE position, then the IE position, and finally return 0
return e.pageY || e.clientY + document.body.scrollTop || 0;
}
// A function for adding a number of pixels to the horizontal
// position of an element
function addX(elem,pos) {
// Get the current horz. position and add the offset to it.
setX( posX(elem) + pos );
}
// A function that can be used to add a number of pixels to the
// vertical position of an element
function addY(elem,pos) {
// Get the current vertical position and add the offset to it
setY( posY(elem) + pos );
}
// A function for determining how far horizontally the browser is scrolled
function scrollX() {
// A shortcut, in case we’re using Internet Explorer 6 in Strict Mode
var de = document.documentElement;
// If the pageXOffset of the browser is available, use that
return self.pageXOffset ||
// Otherwise, try to get the scroll left off of the root node
( de && de.scrollLeft ) ||
// Finally, try to get the scroll left off of the body element
document.body.scrollLeft;
}
// A function for determining how far vertically the browser is scrolled
function scrollY() {
// A shortcut, in case we’re using Internet Explorer 6 in Strict Mode
var de = document.documentElement;
// If the pageYOffset of the browser is available, use that
return self.pageYOffset ||
// Otherwise, try to get the scroll top off of the root node
( de && de.scrollTop ) ||
// Finally, try to get the scroll top off of the body element
document.body.scrollTop;
}
// Get the X position of the mouse relative to the element target
// used in event object ‘e’
function getElementX( e ) {
// Find the appropriate element offset
return ( e && e.layerX ) || window.event.offsetX;
}
// Get the Y position of the mouse relative to the element target
// used in event object ‘e’
function getElementY( e ) {
// Find the appropriate element offset
return ( e && e.layerY ) || window.event.offsetY;
}
function append( parent, elem ) {
// Get the array of elements
var elems = checkElem( elem );
// Append them all to the element
for ( var i = 0; i < elems.length; i++ ) {
parent.appendChild( elems[i] );
}
}
function checkElem(a) {
var r = [];
// Force the argument into an array, if it isn’t already
if ( a.constructor != Array ) a = [ a ];//a => array
//a = [dom]
for ( var i = 0; i < a.length; i++ ) {
// If there’s a String
//a[i]="<div><h1>hehehe</h1></div>"
if ( a[i].constructor == String ) {
// Create a temporary element to house the HTML
var div = document.createElement("div");
// Inject the HTML, to convert it into a DOM structure
div.innerHTML = "<div><h1>hehehe</h1></div>";
// Extract the DOM structure back out of the temp DIV
for ( var j = 0; j < div.childNodes.length; j++ ){
r[r.length] = div.childNodes[j];
}
} else if ( a[i].length ) { // If it’s an array
// Assume that it’s an array of DOM Nodes
for ( var j = 0; j < a[i].length; j++ )
r[r.length] = a[i][j];
} else { // Otherwise, assume it’s a DOM Node
r[r.length] = a[i];
}
}
return r;
}
function before( parent, before, elem ) {
// Check to see if no parent node was provided
if ( elem == null ) {
elem = before;
before = parent;
parent = before.parentNode;
}
parent.insertBefore( checkElem( elem ), before );
}