scroll.js 712 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = scroll;
  6. /**
  7. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  8. *
  9. * This source code is licensed under the MIT license found in the
  10. * LICENSE file in the root directory of this source tree.
  11. */
  12. function scroll(size, {offset, max}) {
  13. let start = 0;
  14. let index = Math.min(offset, size);
  15. const halfScreen = max / 2;
  16. if (index <= halfScreen) {
  17. start = 0;
  18. } else {
  19. if (size >= max) {
  20. start = Math.min(index - halfScreen - 1, size - max);
  21. }
  22. index = Math.min(index - start, size);
  23. }
  24. return {
  25. end: Math.min(size, start + max),
  26. index,
  27. start
  28. };
  29. }