Ghost/ghost/admin/app/modifiers/scroll-into-view.js
Kevin Ansfield fccfcc3320 Added scroll-into-view behavior for highlighted gifs
refs https://github.com/TryGhost/Team/issues/1225

- added `scroll-into-view` modifier that will scroll an element into view putting it at the top or bottom of the viewport depending on which direction scroll is required
- used the `scroll-into-view` modifier to scroll the highlighted gif into view
2021-11-29 19:33:35 +00:00

22 lines
819 B
JavaScript

import getScrollParent from 'ghost-admin/utils/get-scroll-parent';
import {modifier} from 'ember-modifier';
export default modifier((element, [shouldScroll = true], {offset = 0}) => {
if (shouldScroll) {
const scrollParent = getScrollParent(element);
const isOffTop = element.offsetTop < scrollParent.scrollTop;
const isOffBottom = scrollParent.scrollTop + scrollParent.offsetHeight < element.offsetTop + element.offsetHeight;
if (isOffTop) {
const top = element.offsetTop - offset;
scrollParent.scrollTo({top, behavior: 'smooth'});
}
if (isOffBottom) {
const top = element.offsetTop - scrollParent.offsetHeight + element.offsetHeight + offset;
scrollParent.scrollTo({top, behavior: 'smooth'});
}
}
});