scroll-into-view.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.default = scrollIntoView;
  4. var _vue = require('vue');
  5. var _vue2 = _interopRequireDefault(_vue);
  6. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  7. function scrollIntoView(container, selected) {
  8. if (_vue2.default.prototype.$isServer) return;
  9. if (!selected) {
  10. container.scrollTop = 0;
  11. return;
  12. }
  13. var offsetParents = [];
  14. var pointer = selected.offsetParent;
  15. while (pointer && container !== pointer && container.contains(pointer)) {
  16. offsetParents.push(pointer);
  17. pointer = pointer.offsetParent;
  18. }
  19. var top = selected.offsetTop + offsetParents.reduce(function (prev, curr) {
  20. return prev + curr.offsetTop;
  21. }, 0);
  22. var bottom = top + selected.offsetHeight;
  23. var viewRectTop = container.scrollTop;
  24. var viewRectBottom = viewRectTop + container.clientHeight;
  25. if (top < viewRectTop) {
  26. container.scrollTop = top;
  27. } else if (bottom > viewRectBottom) {
  28. container.scrollTop = bottom - container.clientHeight;
  29. }
  30. }