[0]ホーム
// or element, update the intersection rect. // Note: and cannot be clipped to a rect that's not also // the document rect, so no need to compute a new intersection. var doc = parent.ownerDocument; if (parent != doc.body && parent != doc.documentElement && parentComputedStyle.overflow != 'visible') { parentRect = getBoundingClientRect(parent); } } // If either of the above conditionals set a new parentRect, // calculate new intersection data. if (parentRect) { intersectionRect = computeRectIntersection(parentRect, intersectionRect); } if (!intersectionRect) break; parent = parent && getParentNode(parent); } return intersectionRect;};/** * Returns the root rect after being expanded by the rootMargin value. * @return {ClientRect} The expanded root rect. * @private */IntersectionObserver.prototype._getRootRect = function() { var rootRect; if (this.root && !isDoc(this.root)) { rootRect = getBoundingClientRect(this.root); } else { // Use/ instead of window since scroll bars affect size. var doc = isDoc(this.root) ? this.root : document; var html = doc.documentElement; var body = doc.body; rootRect = { top: 0, left: 0, right: html.clientWidth || body.clientWidth, width: html.clientWidth || body.clientWidth, bottom: html.clientHeight || body.clientHeight, height: html.clientHeight || body.clientHeight }; } return this._expandRectByRootMargin(rootRect);};/** * Accepts a rect and expands it by the rootMargin value. * @param {DOMRect|ClientRect} rect The rect object to expand. * @return {ClientRect} The expanded rect. * @private */IntersectionObserver.prototype._expandRectByRootMargin = function(rect) { var margins = this._rootMarginValues.map(function(margin, i) { return margin.unit == 'px' ? margin.value : margin.value * (i % 2 ? rect.width : rect.height) / 100; }); var newRect = { top: rect.top - margins[0], right: rect.right + margins[1], bottom: rect.bottom + margins[2], left: rect.left - margins[3] }; newRect.width = newRect.right - newRect.left; newRect.height = newRect.bottom - newRect.top; return newRect;};/** * Accepts an old and new entry and returns true if at least one of the * threshold values has been crossed. * @param {?IntersectionObserverEntry} oldEntry The previous entry for a * particular target element or null if no previous entry exists. * @param {IntersectionObserverEntry} newEntry The current entry for a * particular target element. * @return {boolean} Returns true if a any threshold has been crossed. * @private */IntersectionObserver.prototype._hasCrossedThreshold = function(oldEntry, newEntry) { // To make comparing easier, an entry that has a ratio of 0 // but does not actually intersect is given a value of -1 var oldRatio = oldEntry && oldEntry.isIntersecting ? oldEntry.intersectionRatio || 0 : -1; var newRatio = newEntry.isIntersecting ? newEntry.intersectionRatio || 0 : -1; // Ignore unchanged ratios if (oldRatio === newRatio) return; for (var i = 0; i< this.thresholds.length; i++) { var threshold = this.thresholds[i]; // Return true if an entry matches a threshold or if the new ratio // and the old ratio are on the opposite sides of a threshold. if (threshold == oldRatio || threshold == newRatio || threshold< oldRatio !== threshold< newRatio) { return true; } }};/** * Returns whether or not the root element is an element and is in the DOM. * @return {boolean} True if the root element is an element and is in the DOM. * @private */IntersectionObserver.prototype._rootIsInDom = function() { return !this.root || containsDeep(document, this.root);};/** * Returns whether or not the target element is a child of root. * @param {Element} target The target element to check. * @return {boolean} True if the target element is a child of root. * @private */IntersectionObserver.prototype._rootContainsTarget = function(target) { var rootDoc = (this.root && (this.root.ownerDocument || this.root)) || document; return ( containsDeep(rootDoc, target) && (!this.root || rootDoc == target.ownerDocument) );};/** * Adds the instance to the global IntersectionObserver registry if it isn't * already present. * @private */IntersectionObserver.prototype._registerInstance = function() { if (registry.indexOf(this)< 0) { registry.push(this); }};/** * Removes the instance from the global IntersectionObserver registry. * @private */IntersectionObserver.prototype._unregisterInstance = function() { var index = registry.indexOf(this); if (index != -1) registry.splice(index, 1);};/** * Returns the result of the performance.now() method or null in browsers * that don't support the API. * @return {number} The elapsed time since the page was requested. */function now() { return window.performance && performance.now && performance.now();}/** * Throttles a function and delays its execution, so it's only called at most * once within a given time period. * @param {Function} fn The function to throttle. * @param {number} timeout The amount of time that must pass before the * function can be called again. * @return {Function} The throttled function. */function throttle(fn, timeout) { var timer = null; return function () { if (!timer) { timer = setTimeout(function() { fn(); timer = null; }, timeout); } };}/** * Adds an event handler to a DOM node ensuring cross-browser compatibility. * @param {Node} node The DOM node to add the event handler to. * @param {string} event The event name. * @param {Function} fn The event handler to add. * @param {boolean} opt_useCapture Optionally adds the even to the capture * phase. Note: this only works in modern browsers. */function addEvent(node, event, fn, opt_useCapture) { if (typeof node.addEventListener == 'function') { node.addEventListener(event, fn, opt_useCapture || false); } else if (typeof node.attachEvent == 'function') { node.attachEvent('on' + event, fn); }}/** * Removes a previously added event handler from a DOM node. * @param {Node} node The DOM node to remove the event handler from. * @param {string} event The event name. * @param {Function} fn The event handler to remove. * @param {boolean} opt_useCapture If the event handler was added with this * flag set to true, it should be set to true here in order to remove it. */function removeEvent(node, event, fn, opt_useCapture) { if (typeof node.removeEventListener == 'function') { node.removeEventListener(event, fn, opt_useCapture || false); } else if (typeof node.detatchEvent == 'function') { node.detatchEvent('on' + event, fn); }}/** * Returns the intersection between two rect objects. * @param {Object} rect1 The first rect. * @param {Object} rect2 The second rect. * @return {?Object|?ClientRect} The intersection rect or undefined if no * intersection is found. */function computeRectIntersection(rect1, rect2) { var top = Math.max(rect1.top, rect2.top); var bottom = Math.min(rect1.bottom, rect2.bottom); var left = Math.max(rect1.left, rect2.left); var right = Math.min(rect1.right, rect2.right); var width = right - left; var height = bottom - top; return (width >= 0 && height >= 0) && { top: top, bottom: bottom, left: left, right: right, width: width, height: height } || null;}/** * Shims the native getBoundingClientRect for compatibility with older IE. * @param {Element} el The element whose bounding rect to get. * @return {DOMRect|ClientRect} The (possibly shimmed) rect of the element. */function getBoundingClientRect(el) { var rect; try { rect = el.getBoundingClientRect(); } catch (err) { // Ignore Windows 7 IE11 "Unspecified error" // https://github.com/w3c/IntersectionObserver/pull/205 } if (!rect) return getEmptyRect(); // Older IE if (!(rect.width && rect.height)) { rect = { top: rect.top, right: rect.right, bottom: rect.bottom, left: rect.left, width: rect.right - rect.left, height: rect.bottom - rect.top }; } return rect;}/** * Returns an empty rect object. An empty rect is returned when an element * is not in the DOM. * @return {ClientRect} The empty rect. */function getEmptyRect() { return { top: 0, bottom: 0, left: 0, right: 0, width: 0, height: 0 };}/** * Ensure that the result has all of the necessary fields of the DOMRect. * Specifically this ensures that `x` and `y` fields are set. * * @param {?DOMRect|?ClientRect} rect * @return {?DOMRect} */function ensureDOMRect(rect) { // A `DOMRect` object has `x` and `y` fields. if (!rect || 'x' in rect) { return rect; } // A IE's `ClientRect` type does not have `x` and `y`. The same is the case // for internally calculated Rect objects. For the purposes of // `IntersectionObserver`, it's sufficient to simply mirror `left` and `top` // for these fields. return { top: rect.top, y: rect.top, bottom: rect.bottom, left: rect.left, x: rect.left, right: rect.right, width: rect.width, height: rect.height };}/** * Inverts the intersection and bounding rect from the parent (frame) BCR to * the local BCR space. * @param {DOMRect|ClientRect} parentBoundingRect The parent's bound client rect. * @param {DOMRect|ClientRect} parentIntersectionRect The parent's own intersection rect. * @return {ClientRect} The local root bounding rect for the parent's children. */function convertFromParentRect(parentBoundingRect, parentIntersectionRect) { var top = parentIntersectionRect.top - parentBoundingRect.top; var left = parentIntersectionRect.left - parentBoundingRect.left; return { top: top, left: left, height: parentIntersectionRect.height, width: parentIntersectionRect.width, bottom: top + parentIntersectionRect.height, right: left + parentIntersectionRect.width };}/** * Checks to see if a parent element contains a child element (including inside * shadow DOM). * @param {Node} parent The parent element. * @param {Node} child The child element. * @return {boolean} True if the parent node contains the child node. */function containsDeep(parent, child) { var node = child; while (node) { if (node == parent) return true; node = getParentNode(node); } return false;}/** * Gets the parent node of an element or its host element if the parent node * is a shadow root. * @param {Node} node The node whose parent to get. * @return {Node|null} The parent node or null if no parent exists. */function getParentNode(node) { var parent = node.parentNode; if (node.nodeType == /* DOCUMENT */ 9 && node != document) { // If this node is a document node, look for the embedding frame. return getFrameElement(node); } // If the parent has element that is assigned through shadow root slot if (parent && parent.assignedSlot) { parent = parent.assignedSlot.parentNode } if (parent && parent.nodeType == 11 && parent.host) { // If the parent is a shadow root, return the host element. return parent.host; } return parent;}/** * Returns true if `node` is a Document. * @param {!Node} node * @returns {boolean} */function isDoc(node) { return node && node.nodeType === 9;}// Exposes the constructors globally.window.IntersectionObserver = IntersectionObserver;window.IntersectionObserverEntry = IntersectionObserverEntry;}());
(1863–1938), Italian novelist, playwright, and poet. He is best known for his ‘Romances of the Rose’ trilogy, including The Triumph of Death (1894), which shows the influence of Nietzsche.