|
| 1 | +/*! |
| 2 | +* ScrollToSmooth |
| 3 | +* Author: Bastian Fießinger |
| 4 | +* Version: 4.0.1 |
| 5 | +*/ |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Pure DOM utility functions used by ScrollToSmooth. |
| 10 | + * |
| 11 | + * These are stateless helpers with no dependency on the ScrollToSmooth class. |
| 12 | + */ |
| 13 | + |
| 14 | +function querySelector(selector, container = document) { |
| 15 | + return container.querySelector(selector); |
| 16 | +} |
| 17 | +function querySelectorAll(selector, container = document) { |
| 18 | + return container.querySelectorAll(selector); |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Check whether a selector is valid within the given container. |
| 23 | + */ |
| 24 | +function validateSelector(selector, container = document) { |
| 25 | + try { |
| 26 | + if (typeof selector === 'string') { |
| 27 | + querySelector(selector, container); |
| 28 | + } else if (isNodeOrElement(selector)) { |
| 29 | + return container.contains(selector); |
| 30 | + } else { |
| 31 | + return false; |
| 32 | + } |
| 33 | + } catch { |
| 34 | + return false; |
| 35 | + } |
| 36 | + return true; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Runtime check for whether a value is a DOM Node or HTMLElement. |
| 41 | + */ |
| 42 | +function isNodeOrElement(obj) { |
| 43 | + return obj instanceof Node; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Current vertical scroll position. |
| 48 | + */ |
| 49 | +function getScrollingElement() { |
| 50 | + return document.scrollingElement || document.documentElement || document.body; |
| 51 | +} |
| 52 | +function getScrollPositionY() { |
| 53 | + const scrollEl = getScrollingElement(); |
| 54 | + return scrollEl.scrollTop; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Current horizontal scroll position. |
| 59 | + */ |
| 60 | +function getScrollPositionX() { |
| 61 | + const scrollEl = getScrollingElement(); |
| 62 | + return scrollEl.scrollLeft; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * High-resolution timestamp. |
| 67 | + */ |
| 68 | +function getTimestamp() { |
| 69 | + return window.performance && 'now' in window.performance ? performance.now() : new Date().getTime(); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Total scrollable document height. |
| 74 | + */ |
| 75 | +function getDocumentHeight() { |
| 76 | + const body = document.body; |
| 77 | + const docEl = document.documentElement; |
| 78 | + return Math.max(body.scrollHeight, body.offsetHeight, body.clientHeight, docEl.scrollHeight, docEl.offsetHeight, docEl.clientHeight); |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Total scrollable document width. |
| 83 | + */ |
| 84 | +function getDocumentWidth() { |
| 85 | + const body = document.body; |
| 86 | + const docEl = document.documentElement; |
| 87 | + return Math.max(body.scrollWidth, body.offsetWidth, body.clientWidth, docEl.scrollWidth, docEl.offsetWidth, docEl.clientWidth); |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Viewport height. |
| 92 | + */ |
| 93 | +function getWindowHeight() { |
| 94 | + return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Viewport width. |
| 99 | + */ |
| 100 | +function getWindowWidth() { |
| 101 | + return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; |
| 102 | +} |
| 103 | + |
| 104 | +exports.getDocumentHeight = getDocumentHeight; |
| 105 | +exports.getDocumentWidth = getDocumentWidth; |
| 106 | +exports.getScrollPositionX = getScrollPositionX; |
| 107 | +exports.getScrollPositionY = getScrollPositionY; |
| 108 | +exports.getTimestamp = getTimestamp; |
| 109 | +exports.getWindowHeight = getWindowHeight; |
| 110 | +exports.getWindowWidth = getWindowWidth; |
| 111 | +exports.isNodeOrElement = isNodeOrElement; |
| 112 | +exports.querySelector = querySelector; |
| 113 | +exports.querySelectorAll = querySelectorAll; |
| 114 | +exports.validateSelector = validateSelector; |
0 commit comments