blockHorizontalScrollTraverseEvent function

bool blockHorizontalScrollTraverseEvent(
  1. Element element,
  2. WheelEvent wheelEvent
)

Blocks a wheelEvent in the horizontal direction that traverses the element.

Implementation

bool blockHorizontalScrollTraverseEvent(
    Element element, WheelEvent wheelEvent) {
  var delta = -wheelEvent.deltaX;
  var left = delta > 0;

  var width = element.offset.width;
  var scrollLeft = element.scrollLeft;
  var scrollWidth = element.scrollWidth;

  var block = false;
  if (!left && -delta > scrollWidth - width - scrollLeft) {
    //Scrolling right, but this will take us past the right limit.
    element.scrollLeft = scrollWidth;
    block = true;
  } else if (left && delta > scrollLeft) {
    //Scrolling left, but this will take us past the left limit.
    element.scrollLeft = 0;
    block = true;
  }

  if (block) {
    wheelEvent.stopPropagation();
    wheelEvent.preventDefault();
  }

  return block;
}