-
Notifications
You must be signed in to change notification settings - Fork 0
/
to.js
51 lines (44 loc) · 1.16 KB
/
to.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { to } from './engine';
import { getScrollableEl } from './util';
export default function scrollTo(el, options, cb) {
el = getScrollableEl(el);
const target = {};
let toEl = options.to;
const direction = options.direction || 'vertical';
if (typeof toEl === 'string') {
toEl = el.querySelector(toEl);
if (!toEl) {
return false;
}
}
if (toEl instanceof Element) {
const pos = toEl.getBoundingClientRect();
toEl = {
top: pos.top,
left: pos.left
};
} else {
toEl.top -= el.scrollTop;
toEl.left -= el.scrollLeft;
}
if (direction === 'vertical' || direction === 'both') {
const topChange = toEl.top + (options.shiftTop || 0);
if (topChange !== 0) {
target.scrollTop = topChange;
}
}
if (direction === 'horizontal' || direction === 'both') {
const leftChange = toEl.left + (options.shiftLeft || 0);
if (leftChange !== 0) {
target.scrollLeft = leftChange;
}
}
if (Object.keys(target).length) {
return to(el, {
a: target,
duration: options.duration === undefined ? 1000 : options.duration,
ease: options.ease || 'linear',
cb: cb
});
}
};