scroll-into-view.js
1.03 KB
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
'use strict';
exports.__esModule = true;
exports.default = scrollIntoView;
var _vue = require('vue');
var _vue2 = _interopRequireDefault(_vue);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function scrollIntoView(container, selected) {
if (_vue2.default.prototype.$isServer) return;
if (!selected) {
container.scrollTop = 0;
return;
}
var offsetParents = [];
var pointer = selected.offsetParent;
while (pointer && container !== pointer && container.contains(pointer)) {
offsetParents.push(pointer);
pointer = pointer.offsetParent;
}
var top = selected.offsetTop + offsetParents.reduce(function (prev, curr) {
return prev + curr.offsetTop;
}, 0);
var bottom = top + selected.offsetHeight;
var viewRectTop = container.scrollTop;
var viewRectBottom = viewRectTop + container.clientHeight;
if (top < viewRectTop) {
container.scrollTop = top;
} else if (bottom > viewRectBottom) {
container.scrollTop = bottom - container.clientHeight;
}
}