fix/simplify scrollElementIntoView
This commit is contained in:
parent
27c713e11e
commit
5838779ff2
|
@ -43,7 +43,7 @@ async function InjectionOrder(show = true) {
|
||||||
|
|
||||||
function makeList([type, styles]) {
|
function makeList([type, styles]) {
|
||||||
const ids = groups[type] = styles.map(s => s._id);
|
const ids = groups[type] = styles.map(s => s._id);
|
||||||
const ol = ols[type] = $create('ol');
|
const ol = ols[type] = $create('ol.scroller');
|
||||||
let maxTranslateY;
|
let maxTranslateY;
|
||||||
ol.append(...styles.map(makeEntry));
|
ol.append(...styles.map(makeEntry));
|
||||||
ol.on('d:dragstart', ({detail: d}) => {
|
ol.on('d:dragstart', ({detail: d}) => {
|
||||||
|
|
14
js/dom.js
14
js/dom.js
|
@ -278,16 +278,22 @@ function onDOMready() {
|
||||||
: new Promise(resolve => document.on('DOMContentLoaded', resolve, {once: true}));
|
: new Promise(resolve => document.on('DOMContentLoaded', resolve, {once: true}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scrolls `window` or the closest parent with `class="scroller"` if the element is not visible,
|
||||||
|
* centering the element in the view
|
||||||
|
* @param {HTMLElement} element
|
||||||
|
* @param {number} [invalidMarginRatio] - for example, 0.10 will center the element if it's in the top/bottom 10% of the scroller
|
||||||
|
*/
|
||||||
function scrollElementIntoView(element, {invalidMarginRatio = 0} = {}) {
|
function scrollElementIntoView(element, {invalidMarginRatio = 0} = {}) {
|
||||||
// align to the top/bottom of the visible area if wasn't visible
|
// align to the top/bottom of the visible area if wasn't visible
|
||||||
const parent = element.parentNode;
|
if (!element.parentNode) return;
|
||||||
if (!parent) return;
|
|
||||||
const {top, height} = element.getBoundingClientRect();
|
const {top, height} = element.getBoundingClientRect();
|
||||||
const {top: parentTop, bottom: parentBottom} = parent.getBoundingClientRect();
|
const {top: parentTop, bottom: parentBottom} = element.parentNode.getBoundingClientRect();
|
||||||
const windowHeight = window.innerHeight;
|
const windowHeight = window.innerHeight;
|
||||||
if (top < Math.max(parentTop, windowHeight * invalidMarginRatio) ||
|
if (top < Math.max(parentTop, windowHeight * invalidMarginRatio) ||
|
||||||
top > Math.min(parentBottom, windowHeight) - height - windowHeight * invalidMarginRatio) {
|
top > Math.min(parentBottom, windowHeight) - height - windowHeight * invalidMarginRatio) {
|
||||||
parent.scrollBy(0, top - windowHeight / 2 + height);
|
const scroller = element.closest('.scroller');
|
||||||
|
scroller.scrollBy(0, top - (scroller ? scroller.clientHeight : windowHeight) / 2 + height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user