| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
(function() { |
| 10 |
'use strict'; |
| 11 |
|
| 12 |
|
| 13 |
if (typeof window !== 'object') { |
| 14 |
return; |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
if ('IntersectionObserver' in window && |
| 20 |
'IntersectionObserverEntry' in window && |
| 21 |
'intersectionRatio' in window.IntersectionObserverEntry.prototype) { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
if (!('isIntersecting' in window.IntersectionObserverEntry.prototype)) { |
| 26 |
Object.defineProperty(window.IntersectionObserverEntry.prototype, |
| 27 |
'isIntersecting', { |
| 28 |
get: function () { |
| 29 |
return this.intersectionRatio > 0; |
| 30 |
} |
| 31 |
}); |
| 32 |
} |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
function getFrameElement(doc) { |
| 42 |
try { |
| 43 |
return doc.defaultView && doc.defaultView.frameElement || null; |
| 44 |
} catch (e) { |
| 45 |
|
| 46 |
return null; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
var document = (function(startDoc) { |
| 54 |
var doc = startDoc; |
| 55 |
var frame = getFrameElement(doc); |
| 56 |
while (frame) { |
| 57 |
doc = frame.ownerDocument; |
| 58 |
frame = getFrameElement(doc); |
| 59 |
} |
| 60 |
return doc; |
| 61 |
})(window.document); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
var registry = []; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
var crossOriginUpdater = null; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
var crossOriginRect = null; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
function IntersectionObserverEntry(entry) { |
| 92 |
this.time = entry.time; |
| 93 |
this.target = entry.target; |
| 94 |
this.rootBounds = ensureDOMRect(entry.rootBounds); |
| 95 |
this.boundingClientRect = ensureDOMRect(entry.boundingClientRect); |
| 96 |
this.intersectionRect = ensureDOMRect(entry.intersectionRect || getEmptyRect()); |
| 97 |
this.isIntersecting = !!entry.intersectionRect; |
| 98 |
|
| 99 |
|
| 100 |
var targetRect = this.boundingClientRect; |
| 101 |
var targetArea = targetRect.width * targetRect.height; |
| 102 |
var intersectionRect = this.intersectionRect; |
| 103 |
var intersectionArea = intersectionRect.width * intersectionRect.height; |
| 104 |
|
| 105 |
|
| 106 |
if (targetArea) { |
| 107 |
|
| 108 |
|
| 109 |
this.intersectionRatio = Number((intersectionArea / targetArea).toFixed(4)); |
| 110 |
} else { |
| 111 |
|
| 112 |
this.intersectionRatio = this.isIntersecting ? 1 : 0; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
function IntersectionObserver(callback, opt_options) { |
| 127 |
|
| 128 |
var options = opt_options || {}; |
| 129 |
|
| 130 |
if (typeof callback != 'function') { |
| 131 |
throw new Error('callback must be a function'); |
| 132 |
} |
| 133 |
|
| 134 |
if ( |
| 135 |
options.root && |
| 136 |
options.root.nodeType != 1 && |
| 137 |
options.root.nodeType != 9 |
| 138 |
) { |
| 139 |
throw new Error('root must be a Document or Element'); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
this._checkForIntersections = throttle( |
| 144 |
this._checkForIntersections.bind(this), this.THROTTLE_TIMEOUT); |
| 145 |
|
| 146 |
|
| 147 |
this._callback = callback; |
| 148 |
this._observationTargets = []; |
| 149 |
this._queuedEntries = []; |
| 150 |
this._rootMarginValues = this._parseRootMargin(options.rootMargin); |
| 151 |
|
| 152 |
|
| 153 |
this.thresholds = this._initThresholds(options.threshold); |
| 154 |
this.root = options.root || null; |
| 155 |
this.rootMargin = this._rootMarginValues.map(function(margin) { |
| 156 |
return margin.value + margin.unit; |
| 157 |
}).join(' '); |
| 158 |
|
| 159 |
|
| 160 |
this._monitoringDocuments = []; |
| 161 |
|
| 162 |
this._monitoringUnsubscribes = []; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
IntersectionObserver.prototype.THROTTLE_TIMEOUT = 100; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
IntersectionObserver.prototype.POLL_INTERVAL = null; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
IntersectionObserver.prototype.USE_MUTATION_OBSERVER = true; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
IntersectionObserver._setupCrossOriginUpdater = function() { |
| 197 |
if (!crossOriginUpdater) { |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
crossOriginUpdater = function(boundingClientRect, intersectionRect) { |
| 203 |
if (!boundingClientRect || !intersectionRect) { |
| 204 |
crossOriginRect = getEmptyRect(); |
| 205 |
} else { |
| 206 |
crossOriginRect = convertFromParentRect(boundingClientRect, intersectionRect); |
| 207 |
} |
| 208 |
registry.forEach(function(observer) { |
| 209 |
observer._checkForIntersections(); |
| 210 |
}); |
| 211 |
}; |
| 212 |
} |
| 213 |
return crossOriginUpdater; |
| 214 |
}; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
IntersectionObserver._resetCrossOriginUpdater = function() { |
| 221 |
crossOriginUpdater = null; |
| 222 |
crossOriginRect = null; |
| 223 |
}; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
IntersectionObserver.prototype.observe = function(target) { |
| 232 |
var isTargetAlreadyObserved = this._observationTargets.some(function(item) { |
| 233 |
return item.element == target; |
| 234 |
}); |
| 235 |
|
| 236 |
if (isTargetAlreadyObserved) { |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
if (!(target && target.nodeType == 1)) { |
| 241 |
throw new Error('target must be an Element'); |
| 242 |
} |
| 243 |
|
| 244 |
this._registerInstance(); |
| 245 |
this._observationTargets.push({element: target, entry: null}); |
| 246 |
this._monitorIntersections(target.ownerDocument); |
| 247 |
this._checkForIntersections(); |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
IntersectionObserver.prototype.unobserve = function(target) { |
| 256 |
this._observationTargets = |
| 257 |
this._observationTargets.filter(function(item) { |
| 258 |
return item.element != target; |
| 259 |
}); |
| 260 |
this._unmonitorIntersections(target.ownerDocument); |
| 261 |
if (this._observationTargets.length == 0) { |
| 262 |
this._unregisterInstance(); |
| 263 |
} |
| 264 |
}; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
IntersectionObserver.prototype.disconnect = function() { |
| 271 |
this._observationTargets = []; |
| 272 |
this._unmonitorAllIntersections(); |
| 273 |
this._unregisterInstance(); |
| 274 |
}; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
IntersectionObserver.prototype.takeRecords = function() { |
| 284 |
var records = this._queuedEntries.slice(); |
| 285 |
this._queuedEntries = []; |
| 286 |
return records; |
| 287 |
}; |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
IntersectionObserver.prototype._initThresholds = function(opt_threshold) { |
| 300 |
var threshold = opt_threshold || [0]; |
| 301 |
if (!Array.isArray(threshold)) threshold = [threshold]; |
| 302 |
|
| 303 |
return threshold.sort().filter(function(t, i, a) { |
| 304 |
if (typeof t != 'number' || isNaN(t) || t < 0 || t > 1) { |
| 305 |
throw new Error('threshold must be a number between 0 and 1 inclusively'); |
| 306 |
} |
| 307 |
return t !== a[i - 1]; |
| 308 |
}); |
| 309 |
}; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
IntersectionObserver.prototype._parseRootMargin = function(opt_rootMargin) { |
| 324 |
var marginString = opt_rootMargin || '0px'; |
| 325 |
var margins = marginString.split(/\s+/).map(function(margin) { |
| 326 |
var parts = /^(-?\d*\.?\d+)(px|%)$/.exec(margin); |
| 327 |
if (!parts) { |
| 328 |
throw new Error('rootMargin must be specified in pixels or percent'); |
| 329 |
} |
| 330 |
return {value: parseFloat(parts[1]), unit: parts[2]}; |
| 331 |
}); |
| 332 |
|
| 333 |
|
| 334 |
margins[1] = margins[1] || margins[0]; |
| 335 |
margins[2] = margins[2] || margins[0]; |
| 336 |
margins[3] = margins[3] || margins[1]; |
| 337 |
|
| 338 |
return margins; |
| 339 |
}; |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
IntersectionObserver.prototype._monitorIntersections = function(doc) { |
| 349 |
var win = doc.defaultView; |
| 350 |
if (!win) { |
| 351 |
|
| 352 |
return; |
| 353 |
} |
| 354 |
if (this._monitoringDocuments.indexOf(doc) != -1) { |
| 355 |
|
| 356 |
return; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
var callback = this._checkForIntersections; |
| 361 |
var monitoringInterval = null; |
| 362 |
var domObserver = null; |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
if (this.POLL_INTERVAL) { |
| 367 |
monitoringInterval = win.setInterval(callback, this.POLL_INTERVAL); |
| 368 |
} else { |
| 369 |
addEvent(win, 'resize', callback, true); |
| 370 |
addEvent(doc, 'scroll', callback, true); |
| 371 |
if (this.USE_MUTATION_OBSERVER && 'MutationObserver' in win) { |
| 372 |
domObserver = new win.MutationObserver(callback); |
| 373 |
domObserver.observe(doc, { |
| 374 |
attributes: true, |
| 375 |
childList: true, |
| 376 |
characterData: true, |
| 377 |
subtree: true |
| 378 |
}); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
this._monitoringDocuments.push(doc); |
| 383 |
this._monitoringUnsubscribes.push(function() { |
| 384 |
|
| 385 |
|
| 386 |
var win = doc.defaultView; |
| 387 |
|
| 388 |
if (win) { |
| 389 |
if (monitoringInterval) { |
| 390 |
win.clearInterval(monitoringInterval); |
| 391 |
} |
| 392 |
removeEvent(win, 'resize', callback, true); |
| 393 |
} |
| 394 |
|
| 395 |
removeEvent(doc, 'scroll', callback, true); |
| 396 |
if (domObserver) { |
| 397 |
domObserver.disconnect(); |
| 398 |
} |
| 399 |
}); |
| 400 |
|
| 401 |
|
| 402 |
var rootDoc = |
| 403 |
(this.root && (this.root.ownerDocument || this.root)) || document; |
| 404 |
if (doc != rootDoc) { |
| 405 |
var frame = getFrameElement(doc); |
| 406 |
if (frame) { |
| 407 |
this._monitorIntersections(frame.ownerDocument); |
| 408 |
} |
| 409 |
} |
| 410 |
}; |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
IntersectionObserver.prototype._unmonitorIntersections = function(doc) { |
| 419 |
var index = this._monitoringDocuments.indexOf(doc); |
| 420 |
if (index == -1) { |
| 421 |
return; |
| 422 |
} |
| 423 |
|
| 424 |
var rootDoc = |
| 425 |
(this.root && (this.root.ownerDocument || this.root)) || document; |
| 426 |
|
| 427 |
|
| 428 |
var hasDependentTargets = |
| 429 |
this._observationTargets.some(function(item) { |
| 430 |
var itemDoc = item.element.ownerDocument; |
| 431 |
|
| 432 |
if (itemDoc == doc) { |
| 433 |
return true; |
| 434 |
} |
| 435 |
|
| 436 |
while (itemDoc && itemDoc != rootDoc) { |
| 437 |
var frame = getFrameElement(itemDoc); |
| 438 |
itemDoc = frame && frame.ownerDocument; |
| 439 |
if (itemDoc == doc) { |
| 440 |
return true; |
| 441 |
} |
| 442 |
} |
| 443 |
return false; |
| 444 |
}); |
| 445 |
if (hasDependentTargets) { |
| 446 |
return; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
var unsubscribe = this._monitoringUnsubscribes[index]; |
| 451 |
this._monitoringDocuments.splice(index, 1); |
| 452 |
this._monitoringUnsubscribes.splice(index, 1); |
| 453 |
unsubscribe(); |
| 454 |
|
| 455 |
|
| 456 |
if (doc != rootDoc) { |
| 457 |
var frame = getFrameElement(doc); |
| 458 |
if (frame) { |
| 459 |
this._unmonitorIntersections(frame.ownerDocument); |
| 460 |
} |
| 461 |
} |
| 462 |
}; |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
IntersectionObserver.prototype._unmonitorAllIntersections = function() { |
| 471 |
var unsubscribes = this._monitoringUnsubscribes.slice(0); |
| 472 |
this._monitoringDocuments.length = 0; |
| 473 |
this._monitoringUnsubscribes.length = 0; |
| 474 |
for (var i = 0; i < unsubscribes.length; i++) { |
| 475 |
unsubscribes[i](); |
| 476 |
} |
| 477 |
}; |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
IntersectionObserver.prototype._checkForIntersections = function() { |
| 487 |
if (!this.root && crossOriginUpdater && !crossOriginRect) { |
| 488 |
|
| 489 |
return; |
| 490 |
} |
| 491 |
|
| 492 |
var rootIsInDom = this._rootIsInDom(); |
| 493 |
var rootRect = rootIsInDom ? this._getRootRect() : getEmptyRect(); |
| 494 |
|
| 495 |
this._observationTargets.forEach(function(item) { |
| 496 |
var target = item.element; |
| 497 |
var targetRect = getBoundingClientRect(target); |
| 498 |
var rootContainsTarget = this._rootContainsTarget(target); |
| 499 |
var oldEntry = item.entry; |
| 500 |
var intersectionRect = rootIsInDom && rootContainsTarget && |
| 501 |
this._computeTargetAndRootIntersection(target, targetRect, rootRect); |
| 502 |
|
| 503 |
var rootBounds = null; |
| 504 |
if (!this._rootContainsTarget(target)) { |
| 505 |
rootBounds = getEmptyRect(); |
| 506 |
} else if (!crossOriginUpdater || this.root) { |
| 507 |
rootBounds = rootRect; |
| 508 |
} |
| 509 |
|
| 510 |
var newEntry = item.entry = new IntersectionObserverEntry({ |
| 511 |
time: now(), |
| 512 |
target: target, |
| 513 |
boundingClientRect: targetRect, |
| 514 |
rootBounds: rootBounds, |
| 515 |
intersectionRect: intersectionRect |
| 516 |
}); |
| 517 |
|
| 518 |
if (!oldEntry) { |
| 519 |
this._queuedEntries.push(newEntry); |
| 520 |
} else if (rootIsInDom && rootContainsTarget) { |
| 521 |
|
| 522 |
|
| 523 |
if (this._hasCrossedThreshold(oldEntry, newEntry)) { |
| 524 |
this._queuedEntries.push(newEntry); |
| 525 |
} |
| 526 |
} else { |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
if (oldEntry && oldEntry.isIntersecting) { |
| 531 |
this._queuedEntries.push(newEntry); |
| 532 |
} |
| 533 |
} |
| 534 |
}, this); |
| 535 |
|
| 536 |
if (this._queuedEntries.length) { |
| 537 |
this._callback(this.takeRecords(), this); |
| 538 |
} |
| 539 |
}; |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
IntersectionObserver.prototype._computeTargetAndRootIntersection = |
| 556 |
function(target, targetRect, rootRect) { |
| 557 |
|
| 558 |
if (window.getComputedStyle(target).display == 'none') return; |
| 559 |
|
| 560 |
var intersectionRect = targetRect; |
| 561 |
var parent = getParentNode(target); |
| 562 |
var atRoot = false; |
| 563 |
|
| 564 |
while (!atRoot && parent) { |
| 565 |
var parentRect = null; |
| 566 |
var parentComputedStyle = parent.nodeType == 1 ? |
| 567 |
window.getComputedStyle(parent) : {}; |
| 568 |
|
| 569 |
|
| 570 |
if (parentComputedStyle.display == 'none') return null; |
| 571 |
|
| 572 |
if (parent == this.root || parent.nodeType == 9) { |
| 573 |
atRoot = true; |
| 574 |
if (parent == this.root || parent == document) { |
| 575 |
if (crossOriginUpdater && !this.root) { |
| 576 |
if (!crossOriginRect || |
| 577 |
crossOriginRect.width == 0 && crossOriginRect.height == 0) { |
| 578 |
|
| 579 |
parent = null; |
| 580 |
parentRect = null; |
| 581 |
intersectionRect = null; |
| 582 |
} else { |
| 583 |
parentRect = crossOriginRect; |
| 584 |
} |
| 585 |
} else { |
| 586 |
parentRect = rootRect; |
| 587 |
} |
| 588 |
} else { |
| 589 |
|
| 590 |
var frame = getParentNode(parent); |
| 591 |
var frameRect = frame && getBoundingClientRect(frame); |
| 592 |
var frameIntersect = |
| 593 |
frame && |
| 594 |
this._computeTargetAndRootIntersection(frame, frameRect, rootRect); |
| 595 |
if (frameRect && frameIntersect) { |
| 596 |
parent = frame; |
| 597 |
parentRect = convertFromParentRect(frameRect, frameIntersect); |
| 598 |
} else { |
| 599 |
parent = null; |
| 600 |
intersectionRect = null; |
| 601 |
} |
| 602 |
} |
| 603 |
} else { |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
var doc = parent.ownerDocument; |
| 609 |
if (parent != doc.body && |
| 610 |
parent != doc.documentElement && |
| 611 |
parentComputedStyle.overflow != 'visible') { |
| 612 |
parentRect = getBoundingClientRect(parent); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
if (parentRect) { |
| 619 |
intersectionRect = computeRectIntersection(parentRect, intersectionRect); |
| 620 |
} |
| 621 |
if (!intersectionRect) break; |
| 622 |
parent = parent && getParentNode(parent); |
| 623 |
} |
| 624 |
return intersectionRect; |
| 625 |
}; |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
IntersectionObserver.prototype._getRootRect = function() { |
| 634 |
var rootRect; |
| 635 |
if (this.root && !isDoc(this.root)) { |
| 636 |
rootRect = getBoundingClientRect(this.root); |
| 637 |
} else { |
| 638 |
|
| 639 |
var doc = isDoc(this.root) ? this.root : document; |
| 640 |
var html = doc.documentElement; |
| 641 |
var body = doc.body; |
| 642 |
rootRect = { |
| 643 |
top: 0, |
| 644 |
left: 0, |
| 645 |
right: html.clientWidth || body.clientWidth, |
| 646 |
width: html.clientWidth || body.clientWidth, |
| 647 |
bottom: html.clientHeight || body.clientHeight, |
| 648 |
height: html.clientHeight || body.clientHeight |
| 649 |
}; |
| 650 |
} |
| 651 |
return this._expandRectByRootMargin(rootRect); |
| 652 |
}; |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
IntersectionObserver.prototype._expandRectByRootMargin = function(rect) { |
| 662 |
var margins = this._rootMarginValues.map(function(margin, i) { |
| 663 |
return margin.unit == 'px' ? margin.value : |
| 664 |
margin.value * (i % 2 ? rect.width : rect.height) / 100; |
| 665 |
}); |
| 666 |
var newRect = { |
| 667 |
top: rect.top - margins[0], |
| 668 |
right: rect.right + margins[1], |
| 669 |
bottom: rect.bottom + margins[2], |
| 670 |
left: rect.left - margins[3] |
| 671 |
}; |
| 672 |
newRect.width = newRect.right - newRect.left; |
| 673 |
newRect.height = newRect.bottom - newRect.top; |
| 674 |
|
| 675 |
return newRect; |
| 676 |
}; |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
IntersectionObserver.prototype._hasCrossedThreshold = |
| 690 |
function(oldEntry, newEntry) { |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
var oldRatio = oldEntry && oldEntry.isIntersecting ? |
| 695 |
oldEntry.intersectionRatio || 0 : -1; |
| 696 |
var newRatio = newEntry.isIntersecting ? |
| 697 |
newEntry.intersectionRatio || 0 : -1; |
| 698 |
|
| 699 |
|
| 700 |
if (oldRatio === newRatio) return; |
| 701 |
|
| 702 |
for (var i = 0; i < this.thresholds.length; i++) { |
| 703 |
var threshold = this.thresholds[i]; |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
if (threshold == oldRatio || threshold == newRatio || |
| 708 |
threshold < oldRatio !== threshold < newRatio) { |
| 709 |
return true; |
| 710 |
} |
| 711 |
} |
| 712 |
}; |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
IntersectionObserver.prototype._rootIsInDom = function() { |
| 721 |
return !this.root || containsDeep(document, this.root); |
| 722 |
}; |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
IntersectionObserver.prototype._rootContainsTarget = function(target) { |
| 732 |
var rootDoc = |
| 733 |
(this.root && (this.root.ownerDocument || this.root)) || document; |
| 734 |
return ( |
| 735 |
containsDeep(rootDoc, target) && |
| 736 |
(!this.root || rootDoc == target.ownerDocument) |
| 737 |
); |
| 738 |
}; |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
IntersectionObserver.prototype._registerInstance = function() { |
| 747 |
if (registry.indexOf(this) < 0) { |
| 748 |
registry.push(this); |
| 749 |
} |
| 750 |
}; |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
IntersectionObserver.prototype._unregisterInstance = function() { |
| 758 |
var index = registry.indexOf(this); |
| 759 |
if (index != -1) registry.splice(index, 1); |
| 760 |
}; |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
function now() { |
| 769 |
return window.performance && performance.now && performance.now(); |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
function throttle(fn, timeout) { |
| 782 |
var timer = null; |
| 783 |
return function () { |
| 784 |
if (!timer) { |
| 785 |
timer = setTimeout(function() { |
| 786 |
fn(); |
| 787 |
timer = null; |
| 788 |
}, timeout); |
| 789 |
} |
| 790 |
}; |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
function addEvent(node, event, fn, opt_useCapture) { |
| 803 |
if (typeof node.addEventListener == 'function') { |
| 804 |
node.addEventListener(event, fn, opt_useCapture || false); |
| 805 |
} |
| 806 |
else if (typeof node.attachEvent == 'function') { |
| 807 |
node.attachEvent('on' + event, fn); |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
function removeEvent(node, event, fn, opt_useCapture) { |
| 821 |
if (typeof node.removeEventListener == 'function') { |
| 822 |
node.removeEventListener(event, fn, opt_useCapture || false); |
| 823 |
} |
| 824 |
else if (typeof node.detatchEvent == 'function') { |
| 825 |
node.detatchEvent('on' + event, fn); |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
function computeRectIntersection(rect1, rect2) { |
| 838 |
var top = Math.max(rect1.top, rect2.top); |
| 839 |
var bottom = Math.min(rect1.bottom, rect2.bottom); |
| 840 |
var left = Math.max(rect1.left, rect2.left); |
| 841 |
var right = Math.min(rect1.right, rect2.right); |
| 842 |
var width = right - left; |
| 843 |
var height = bottom - top; |
| 844 |
|
| 845 |
return (width >= 0 && height >= 0) && { |
| 846 |
top: top, |
| 847 |
bottom: bottom, |
| 848 |
left: left, |
| 849 |
right: right, |
| 850 |
width: width, |
| 851 |
height: height |
| 852 |
} || null; |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
function getBoundingClientRect(el) { |
| 862 |
var rect; |
| 863 |
|
| 864 |
try { |
| 865 |
rect = el.getBoundingClientRect(); |
| 866 |
} catch (err) { |
| 867 |
|
| 868 |
|
| 869 |
} |
| 870 |
|
| 871 |
if (!rect) return getEmptyRect(); |
| 872 |
|
| 873 |
|
| 874 |
if (!(rect.width && rect.height)) { |
| 875 |
rect = { |
| 876 |
top: rect.top, |
| 877 |
right: rect.right, |
| 878 |
bottom: rect.bottom, |
| 879 |
left: rect.left, |
| 880 |
width: rect.right - rect.left, |
| 881 |
height: rect.bottom - rect.top |
| 882 |
}; |
| 883 |
} |
| 884 |
return rect; |
| 885 |
} |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
function getEmptyRect() { |
| 894 |
return { |
| 895 |
top: 0, |
| 896 |
bottom: 0, |
| 897 |
left: 0, |
| 898 |
right: 0, |
| 899 |
width: 0, |
| 900 |
height: 0 |
| 901 |
}; |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
function ensureDOMRect(rect) { |
| 913 |
|
| 914 |
if (!rect || 'x' in rect) { |
| 915 |
return rect; |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
return { |
| 922 |
top: rect.top, |
| 923 |
y: rect.top, |
| 924 |
bottom: rect.bottom, |
| 925 |
left: rect.left, |
| 926 |
x: rect.left, |
| 927 |
right: rect.right, |
| 928 |
width: rect.width, |
| 929 |
height: rect.height |
| 930 |
}; |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
function convertFromParentRect(parentBoundingRect, parentIntersectionRect) { |
| 942 |
var top = parentIntersectionRect.top - parentBoundingRect.top; |
| 943 |
var left = parentIntersectionRect.left - parentBoundingRect.left; |
| 944 |
return { |
| 945 |
top: top, |
| 946 |
left: left, |
| 947 |
height: parentIntersectionRect.height, |
| 948 |
width: parentIntersectionRect.width, |
| 949 |
bottom: top + parentIntersectionRect.height, |
| 950 |
right: left + parentIntersectionRect.width |
| 951 |
}; |
| 952 |
} |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
function containsDeep(parent, child) { |
| 963 |
var node = child; |
| 964 |
while (node) { |
| 965 |
if (node == parent) return true; |
| 966 |
|
| 967 |
node = getParentNode(node); |
| 968 |
} |
| 969 |
return false; |
| 970 |
} |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
function getParentNode(node) { |
| 980 |
var parent = node.parentNode; |
| 981 |
|
| 982 |
if (node.nodeType == 9 && node != document) { |
| 983 |
|
| 984 |
return getFrameElement(node); |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
if (parent && parent.assignedSlot) { |
| 989 |
parent = parent.assignedSlot.parentNode |
| 990 |
} |
| 991 |
|
| 992 |
if (parent && parent.nodeType == 11 && parent.host) { |
| 993 |
|
| 994 |
return parent.host; |
| 995 |
} |
| 996 |
|
| 997 |
return parent; |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
function isDoc(node) { |
| 1006 |
return node && node.nodeType === 9; |
| 1007 |
} |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
window.IntersectionObserver = IntersectionObserver; |
| 1012 |
window.IntersectionObserverEntry = IntersectionObserverEntry; |
| 1013 |
|
| 1014 |
}()); |