mousewheel.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { Platform } from '../platform';
  2. export class MouseWheelHandle {
  3. constructor(target, onWheelCallback, onWheelGuard) {
  4. this.animationFrameId = 0;
  5. this.deltaX = 0;
  6. this.deltaY = 0;
  7. this.eventName = Platform.isEventSupported('wheel')
  8. ? 'wheel'
  9. : 'mousewheel';
  10. this.target = target;
  11. this.onWheelCallback = onWheelCallback;
  12. this.onWheelGuard = onWheelGuard;
  13. this.onWheel = this.onWheel.bind(this);
  14. this.didWheel = this.didWheel.bind(this);
  15. }
  16. enable() {
  17. this.target.addEventListener(this.eventName, this.onWheel, {
  18. passive: false,
  19. });
  20. }
  21. disable() {
  22. this.target.removeEventListener(this.eventName, this.onWheel);
  23. }
  24. onWheel(e) {
  25. if (this.onWheelGuard != null && !this.onWheelGuard(e)) {
  26. return;
  27. }
  28. this.deltaX += e.deltaX;
  29. this.deltaY += e.deltaY;
  30. e.preventDefault();
  31. let changed;
  32. if (this.deltaX !== 0 || this.deltaY !== 0) {
  33. e.stopPropagation();
  34. changed = true;
  35. }
  36. if (changed === true && this.animationFrameId === 0) {
  37. this.animationFrameId = requestAnimationFrame(() => {
  38. this.didWheel(e);
  39. });
  40. }
  41. }
  42. didWheel(e) {
  43. this.animationFrameId = 0;
  44. this.onWheelCallback(e, this.deltaX, this.deltaY);
  45. this.deltaX = 0;
  46. this.deltaY = 0;
  47. }
  48. }
  49. //# sourceMappingURL=mousewheel.js.map