🎯 Improve scroll-to-result logic for centering elements within containers
This commit is contained in:
@@ -764,16 +764,14 @@ function scrollToResult() {
|
|||||||
// Scroll only within the container, not the whole page
|
// Scroll only within the container, not the whole page
|
||||||
const container = lineElement.closest('.scroller, .lines-container');
|
const container = lineElement.closest('.scroller, .lines-container');
|
||||||
if (container) {
|
if (container) {
|
||||||
const containerRect = container.getBoundingClientRect();
|
// Get element's position relative to the container
|
||||||
const elementRect = lineElement.getBoundingClientRect();
|
const elementOffsetTop = lineElement.offsetTop;
|
||||||
|
const containerHeight = container.clientHeight;
|
||||||
|
const elementHeight = lineElement.offsetHeight;
|
||||||
|
|
||||||
// Calculate scroll position to center element in container
|
// Calculate scroll position to center element in container
|
||||||
const scrollOffset =
|
const scrollTo = elementOffsetTop - (containerHeight / 2) + (elementHeight / 2);
|
||||||
elementRect.top -
|
container.scrollTo({ top: scrollTo, behavior: 'smooth' });
|
||||||
containerRect.top -
|
|
||||||
containerRect.height / 2 +
|
|
||||||
elementRect.height / 2;
|
|
||||||
container.scrollBy({ top: scrollOffset, behavior: 'smooth' });
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} else if (attempt < 3) {
|
} else if (attempt < 3) {
|
||||||
|
|||||||
Reference in New Issue
Block a user