diff --git a/src/core/element.js b/src/core/element.js index 86a41e01aa87d7139e3f320c2f2d0740cb3c8a4e..4f964d1db426c56e2f94ee093be08a587472ce83 100644 --- a/src/core/element.js +++ b/src/core/element.js @@ -79,12 +79,21 @@ export default class Element { return; } - const scrollIntoViewOptions = this.options.scrollIntoViewOptions || { - behavior: 'smooth', - block: 'center', - }; - - this.node.scrollIntoView(scrollIntoViewOptions); + // If browser supports scrollIntoView, use that otherwise center it manually + if (this.node.scrollIntoView) { + const scrollIntoViewOptions = this.options.scrollIntoViewOptions || { + behavior: 'smooth', + block: 'center', + }; + + this.node.scrollIntoView(scrollIntoViewOptions); + } else { + const elementRect = this.node.getBoundingClientRect(); + const absoluteElementTop = elementRect.top + this.window.pageYOffset; + const middle = absoluteElementTop - (this.window.innerHeight / 2); + + this.window.scrollTo(0, middle); + } } /** diff --git a/src/index.js b/src/index.js index 75a409293e94f08b6dafa2ffd7b39c74ea22ae6a..a9bf18cf4d4b9fbaa2dabb5f3701070d2162160a 100644 --- a/src/index.js +++ b/src/index.js @@ -27,6 +27,7 @@ export default class Driver { animate: OVERLAY_ANIMATE, // Whether to animate or not opacity: OVERLAY_OPACITY, // Overlay opacity padding: OVERLAY_PADDING, // Spacing around the element from the overlay + scrollIntoViewOptions: null, // Options to be passed to `scrollIntoView` onHighlightStarted: () => { // When element is about to be highlighted }, onHighlighted: () => { // When element has been highlighted