You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.8 KiB

1 year ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. import { setScrollingClassInstantly } from './lib/class-names';
  2. function createEvent(name) {
  3. if (typeof window.CustomEvent === 'function') {
  4. return new CustomEvent(name);
  5. } else {
  6. const evt = document.createEvent('CustomEvent');
  7. evt.initCustomEvent(name, false, false, undefined);
  8. return evt;
  9. }
  10. }
  11. export default function(
  12. i,
  13. axis,
  14. diff,
  15. useScrollingClass = true,
  16. forceFireReachEvent = false
  17. ) {
  18. let fields;
  19. if (axis === 'top') {
  20. fields = [
  21. 'contentHeight',
  22. 'containerHeight',
  23. 'scrollTop',
  24. 'y',
  25. 'up',
  26. 'down',
  27. ];
  28. } else if (axis === 'left') {
  29. fields = [
  30. 'contentWidth',
  31. 'containerWidth',
  32. 'scrollLeft',
  33. 'x',
  34. 'left',
  35. 'right',
  36. ];
  37. } else {
  38. throw new Error('A proper axis should be provided');
  39. }
  40. processScrollDiff(i, diff, fields, useScrollingClass, forceFireReachEvent);
  41. }
  42. function processScrollDiff(
  43. i,
  44. diff,
  45. [contentHeight, containerHeight, scrollTop, y, up, down],
  46. useScrollingClass = true,
  47. forceFireReachEvent = false
  48. ) {
  49. const element = i.element;
  50. // reset reach
  51. i.reach[y] = null;
  52. // 1 for subpixel rounding
  53. if (element[scrollTop] < 1) {
  54. i.reach[y] = 'start';
  55. }
  56. // 1 for subpixel rounding
  57. if (element[scrollTop] > i[contentHeight] - i[containerHeight] - 1) {
  58. i.reach[y] = 'end';
  59. }
  60. if (diff) {
  61. element.dispatchEvent(createEvent(`ps-scroll-${y}`));
  62. if (diff < 0) {
  63. element.dispatchEvent(createEvent(`ps-scroll-${up}`));
  64. } else if (diff > 0) {
  65. element.dispatchEvent(createEvent(`ps-scroll-${down}`));
  66. }
  67. if (useScrollingClass) {
  68. setScrollingClassInstantly(i, y);
  69. }
  70. }
  71. if (i.reach[y] && (diff || forceFireReachEvent)) {
  72. element.dispatchEvent(createEvent(`ps-${y}-reach-${i.reach[y]}`));
  73. }
  74. }