helpers.mjs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. export function bias(val) {
  2. const c = 0.501;
  3. const x = Math.abs(val);
  4. return Math.sign(val) * (x / ((1 / c - 2) * (1 - x) + 1));
  5. }
  6. export function calculateUpdatedOffset(_ref) {
  7. let {
  8. selectedElement,
  9. containerSize,
  10. contentSize,
  11. isRtl,
  12. currentScrollOffset,
  13. isHorizontal
  14. } = _ref;
  15. const clientSize = isHorizontal ? selectedElement.clientWidth : selectedElement.clientHeight;
  16. const offsetStart = isHorizontal ? selectedElement.offsetLeft : selectedElement.offsetTop;
  17. const adjustedOffsetStart = isRtl && isHorizontal ? contentSize - offsetStart - clientSize : offsetStart;
  18. const totalSize = containerSize + currentScrollOffset;
  19. const itemOffset = clientSize + adjustedOffsetStart;
  20. const additionalOffset = clientSize * 0.4;
  21. if (adjustedOffsetStart <= currentScrollOffset) {
  22. currentScrollOffset = Math.max(adjustedOffsetStart - additionalOffset, 0);
  23. } else if (totalSize <= itemOffset) {
  24. currentScrollOffset = Math.min(currentScrollOffset - (totalSize - itemOffset - additionalOffset), contentSize - containerSize);
  25. }
  26. return currentScrollOffset;
  27. }
  28. export function calculateCenteredOffset(_ref2) {
  29. let {
  30. selectedElement,
  31. containerSize,
  32. contentSize,
  33. isRtl,
  34. isHorizontal
  35. } = _ref2;
  36. const clientSize = isHorizontal ? selectedElement.clientWidth : selectedElement.clientHeight;
  37. const offsetStart = isHorizontal ? selectedElement.offsetLeft : selectedElement.offsetTop;
  38. const offsetCentered = isRtl && isHorizontal ? contentSize - offsetStart - clientSize / 2 - containerSize / 2 : offsetStart + clientSize / 2 - containerSize / 2;
  39. return Math.min(contentSize - containerSize, Math.max(0, offsetCentered));
  40. }
  41. //# sourceMappingURL=helpers.mjs.map