Alex
Alex

Reputation: 55

Angular 2 window.scrollTo not Working?

I am trying to have it so a my div element, set to "overflow:auto" will scroll when the user is dragging an element.

Dragging the element works, and so does retrieving the proper mouse data (such as the x/y position upon initial drag (onDragStart) and also the current x/y position when the mouse is moving.

I realize my logic for the scrolling probably is off, but I am more just trying to get the div element to scroll at all. Any insight as to what is wrong would be greatly appreciated...Material Design is also being used.

Note: I am using ng-metadata which ports Angular 1 to look like Angular 2, really any guidance in either Angular 1 or Angular 2 would be helpful.

@autobind
onDragStart({clientX, clientY}) {
  this.initY = clientY;
  if (this.isDragging) return;
  document.addEventListener('mousemove', this.dragListener = (e) => {
  e.preventDefault();
  if (Math.max(Math.abs(e.clientX - clientX), Math.abs(e.clientY - clientY)) > 3) {
    document.removeEventListener('mousemove', this.dragListener);
    document.addEventListener('mousemove', this.onDrag);
    this.dragListener = this.onDrag;
    this.fileService.dragSource = this.file;
    // onDrag needs to be called before Angular can set the proper classes
    this._element.toggleClass('dragging', this.isDragging);
    this._element.toggleClass('bulk-dragging', this.inBulkDragOp);
    this.onDragScroll(e);
    this.onDrag(e);
    this.drag.emit();
    this._scope.$applyAsync();
  }
});
}

@autobind
onDrag(e) {
  console.log("Dragging...");
  console.log(e);
  var currentY = e.clientY;
  var range = 100; // Range of 100px before scrolling begins... May need tweaking if too responsive

if (currentY > this.initY + range) {
  console.log("SCROLL DOWN");
  window.scrollTo(0, 500);
} else if (currentY < this.initY - range) {
  console.log("SCROLL UP");
}
e.preventDefault();
this._element.css('transform', `translate(${e.clientX - this._element[0].clientWidth / 2}px, ${e.clientY - this._element[0].clientHeight / 2}px)`);
}

Upvotes: 1

Views: 2862

Answers (1)

Alex
Alex

Reputation: 55

I had my container tag set to a height of 100% which seemed to break the functionality of the scroll. Removing it fixed the problem.

Upvotes: 2

Related Questions