diff options
Diffstat (limited to 'src/utils.mjs')
-rw-r--r-- | src/utils.mjs | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/src/utils.mjs b/src/utils.mjs index ffd8978..4eedf82 100644 --- a/src/utils.mjs +++ b/src/utils.mjs | |||
@@ -72,10 +72,10 @@ export const animateRectTransition = (element, rect, options = {}) => { | |||
72 | * @param {Number} delay milliseconds | 72 | * @param {Number} delay milliseconds |
73 | * @returns {Any} return value of function call, or null if throttled | 73 | * @returns {Any} return value of function call, or null if throttled |
74 | */ | 74 | */ |
75 | export function throttle (func, delay) { | 75 | export function throttle(func, delay) { |
76 | let timerFlag = null | 76 | let timerFlag = null |
77 | 77 | ||
78 | return function (...args) { | 78 | return function(...args) { |
79 | const context = this | 79 | const context = this |
80 | if (timerFlag !== null) return null | 80 | if (timerFlag !== null) return null |
81 | 81 | ||
@@ -99,3 +99,37 @@ export const shiftByWindow = element => { | |||
99 | const offsetY = window.innerHeight - rect.top - rect.height | 99 | const offsetY = window.innerHeight - rect.top - rect.height |
100 | element.style.transform = `translate(${offsetX < 0 ? offsetX : 0}px, ${offsetY < 0 ? offsetY : 0}px)` | 100 | element.style.transform = `translate(${offsetX < 0 ? offsetX : 0}px, ${offsetY < 0 ? offsetY : 0}px)` |
101 | } | 101 | } |
102 | |||
103 | /** | ||
104 | * insideWindow. check DOMRect is inside window | ||
105 | * | ||
106 | * @param {HTMLElement} element | ||
107 | */ | ||
108 | export const insideWindow = element => { | ||
109 | const rect = element.getBoundingClientRect() | ||
110 | return ( | ||
111 | rect.left > 0 && | ||
112 | rect.right < window.innerWidth + rect.width && | ||
113 | rect.top > 0 && | ||
114 | rect.bottom < window.innerHeight + rect.height | ||
115 | ) | ||
116 | } | ||
117 | |||
118 | /** | ||
119 | * insideParent. check children element is inside DOMRect of parent element | ||
120 | * | ||
121 | * @param {HTMLElement} childElement | ||
122 | * @param {HTMLElement} parentElement | ||
123 | */ | ||
124 | export const insideParent = (childElement, parentElement) => { | ||
125 | const childRect = childElement.getBoundingClientRect() | ||
126 | const parentRect = parentElement.getBoundingClientRect() | ||
127 | const offset = 20 | ||
128 | |||
129 | return ( | ||
130 | childRect.left > parentRect.left + offset && | ||
131 | childRect.right < parentRect.right - offset && | ||
132 | childRect.top > parentRect.top + offset && | ||
133 | childRect.bottom < parentRect.bottom - offset | ||
134 | ) | ||
135 | } | ||