Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 7x 7x 7x 7x 7x 7x 6x 6x 3x 3x 3x 3x 248x | export function addClass(el, className) { const full = ' ' + (el.className || '') + ' '; if (!full.includes(' ' + className + ' ')) el.className = (full + className).trim(); } export function removeClass(el, className) { const full = ' ' + (el.className || '') + ' '; el.className = full.replace(' ' + className + ' ', ' ').trim(); } export function hasClass(el, className) { const full = ' ' + (el.className || '') + ' '; return full.includes(' ' + className + ' '); } export function getRect(el) { const { top, bottom, left, right, width, height, x, y } = el.getBoundingClientRect(); return { top, bottom, left, right, width, height, x, y }; } export function getPosition(el) { const rect = getRect(el); return { left: rect.left, top: rect.top }; } export function getSize(el, mode = 'outside') { if (mode === 'inside') return { width: el.clientWidth, height: el.clientHeight }; else if (mode === 'center') return { width: (el.clientWidth + el.offsetWidth) / 2, height: (el.offsetHeight + el.clientHeight) / 2 }; else if (mode === 'outside') return { width: el.offsetWidth, height: el.offsetHeight }; } export function isInRect(position, rect) { if (!position || !rect) return false; return position.left > rect.left && (position.left < rect.left + rect.width) && position.top > rect.top && (position.top < rect.top + rect.height); } export function getComputedStyle(el, property) { const computedStyle = window.getComputedStyle(el, null); return property ? computedStyle[property] : computedStyle; } export function isElementInView(el, viewEl, direction = 'both', complete = false) { if (direction === 'both') return isElementInView(el, viewEl, 'vertical', complete) && isElementInView(el, viewEl, 'horizontal', complete); else if (direction === 'vertical') { const elRect = getRect(el); const viewRect = getRect(viewEl); if (complete) return elRect.top >= viewRect.top && elRect.bottom <= viewRect.bottom; else // @TODO: border? return elRect.bottom > viewRect.top && elRect.top < viewRect.bottom; } else if (direction === 'horizontal') { const elRect = getRect(el); const viewRect = getRect(viewEl); if (complete) return elRect.left >= viewRect.left && elRect.right <= viewRect.right; else // @TODO: border? return elRect.right > viewRect.left && elRect.left < viewRect.right; } else throw new TypeError(`Unknown direction ${direction} of isElementInView param!`); } export const getDimension = function (elem, mode) { return Object.assign(getSize(elem, mode), getPosition(elem)); }; const SPECIAL_CHARS_REGEXP = /([:\-_]+(.))/g; const MOZ_HACK_REGEXP = /^moz([A-Z])/; export const camelCase = function (name) { return name.replace(SPECIAL_CHARS_REGEXP, (_, separator, letter, offset) => offset ? letter.toUpperCase() : letter).replace(MOZ_HACK_REGEXP, 'Moz$1'); }; export const getStyle = function (element, styleName) { if (!element || !styleName) return null; styleName = camelCase(styleName); if (styleName === 'float') styleName = 'cssFloat'; try { const computed = document.defaultView.getComputedStyle(element, ''); return element.style[styleName] || computed ? computed[styleName] : null; } catch (e) { return element.style[styleName]; } }; export const getScrollSize = function () { const inner = document.createElement('div'); inner.style.width = '100%'; inner.style.height = '200px'; const outer = document.createElement('div'); const outerStyle = outer.style; outerStyle.position = 'absolute'; outerStyle.top = 0; outerStyle.left = 0; outerStyle.pointerEvents = 'none'; outerStyle.visibility = 'hidden'; outerStyle.width = '200px'; outerStyle.height = '150px'; outerStyle.overflow = 'hidden'; outer.appendChild(inner); document.body.appendChild(outer); const widthContained = inner.offsetWidth; outer.style.overflow = 'scroll'; let widthScroll = inner.offsetWidth; if (widthContained === widthScroll) widthScroll = outer.clientWidth; document.body.removeChild(outer); return widthContained - widthScroll; }; export function checkIntoView(elm, scrollParentEl) { const rect = elm.getBoundingClientRect(); const viewHeight = getSize(scrollParentEl).height; const parentTop = scrollParentEl.getBoundingClientRect().top; const top = rect.top - parentTop; const bottom = rect.bottom - parentTop; return bottom >= 0 && top - viewHeight < 0; } // scrollTop animation export function scrollTo(el, options, endCallback) { if (!window.requestAnimationFrame) { window.requestAnimationFrame = ( window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame || function (cb) { return window.setTimeout(cb, 1000 / 60); } ); } const startX = el.scrollLeft; const startY = el.scrollTop; if (options.left === undefined) options.left = startX; if (options.top === undefined) options.top = startY; if (options.duration === undefined) options.duration = 400; const diffX = Math.abs(options.left - startX); const diffY = Math.abs(options.top - startY); const stepX = Math.ceil(diffX / options.duration * 50); const stepY = Math.ceil(diffY / options.duration * 50); function scroll(startX, endX, startY, endY) { if (startX === endX && startY === endY) { return endCallback && endCallback(); } let posX = (startX + stepX > endX) ? endX : startX + stepX; if (startX > endX) posX = (startX - stepX < endX) ? endX : startX - stepX; let posY = (startY + stepY > endY) ? endY : startY + stepY; if (startY > endY) posY = (startY - stepY < endY) ? endY : startY - stepY; if (el === window) window.scrollTo(posX, posY); else { el.scrollLeft = posX; el.scrollTop = posY; } window.requestAnimationFrame(() => scroll(posX, endX, posY, endY)); } scroll(startX, options.left, startY, options.top); } export function findScrollParent(el) { el = el.parentElement; if (!el) return window; const styles = window.getComputedStyle(el); Iif (styles.overflowY === 'auto' || styles.overflowY === 'scroll') { return el; } else { return findScrollParent(el); } } export function findXScrollParent(el) { el = el.parentElement; if (!el) return window; const styles = window.getComputedStyle(el); if (styles.overflowX === 'auto' || styles.overflowX === 'scroll') { return el; } else { return findXScrollParent(el); } } export function isIE() { return !!window.ActiveXObject || 'ActiveXObject' in window; } export * from './resize-event'; |