New function: scrollToElement
Opened this issue · 0 comments
waldbaerkoch commented
Hi,
you may want to add a new public function: scrollToElement, adapted from _childFocused:
var scrollToElement = function scrollToElement(element, animationDuration) {
var offset, axis;
var focusedNodeRect = _getBoundingRect(element);
var containerRect = _getBoundingRect(_containerNode);
var edgeMap = { x: 'left', y: 'top' };
var dimensionMap = { x: 'width', y: 'height' };
// If an input is currently being tracked, ignore the focus event
if (_inputIdentifier !== false) {
return;
}
// Get a duration
var duration = animationDuration || 0;
if (duration === true) {
duration = Math.sqrt(Math.abs(_baseScrollPosition[axis] - targetPosition)) * 20;
}
for (axis in _scrollableAxes) {
if (_scrollableAxes.hasOwnProperty(axis)) {
// Set the target offset to be in the middle of the container, or as close as bounds permit
offset = -Math.round((focusedNodeRect[dimensionMap[axis]] / 2) - _lastScrollPosition[axis] + focusedNodeRect[edgeMap[axis]] - containerRect[edgeMap[axis]] - (containerRect[dimensionMap[axis]] / 2));
offset = Math.min(0, Math.max(_metrics.scrollEnd[axis], offset));
// Perform the scroll
_setAxisPosition(axis, offset, duration);
_baseScrollPosition[axis] = offset;
}
}
};
_childFocused can then call this new function.