// Прокрутить к элементу только его ленту (ближайший прокручиваемый контейнер). // element.scrollIntoView двигает ещё и все внешние контейнеры и видимую область браузера: // при открытой клавиатуре это уводило шапку за верхний край экрана – в поиске по переписке // пропадали «раньше/позже», после ответа в обсуждении – заголовок. function findScrollContainer(node) { for (let el = node?.parentElement; el; el = el.parentElement) { const { overflowY } = getComputedStyle(el); if ((overflowY === 'auto' || overflowY === 'scroll') && el.scrollHeight > el.clientHeight) return el; } return null; } /** * @param {Element} node * @param {{ container?: Element|null, block?: 'start'|'center'|'end', behavior?: 'auto'|'smooth' }} [opts] */ export function scrollNodeIntoContainer(node, { container = null, block = 'center', behavior = 'auto' } = {}) { if (!node) return; const host = container || findScrollContainer(node); if (!host) return; const hostRect = host.getBoundingClientRect(); const nodeRect = node.getBoundingClientRect(); let delta; if (block === 'start') delta = nodeRect.top - hostRect.top; else if (block === 'end') delta = nodeRect.bottom - hostRect.bottom; else delta = nodeRect.top - hostRect.top - (host.clientHeight - nodeRect.height) / 2; const top = Math.max(0, Math.min(host.scrollHeight - host.clientHeight, host.scrollTop + delta)); if (behavior === 'smooth' && typeof host.scrollTo === 'function') host.scrollTo({ top, behavior: 'smooth' }); else host.scrollTop = top; }