I have a problem with draggable element which overlapping screen area. Here is example of code:
onDrag(pageX: number, pageY: number) {
if (this.isDragging) {
const deltaX = pageX - this.lastPageX;
const deltaY = pageY - this.lastPageY;
const coords = this.element.nativeElement.getBoundingClientRect();
this.element.nativeElement.style.left = `${coords.left + deltaX}px`;
this.element.nativeElement.style.top = `${coords.top + deltaY}px`;
this.lastPageX = pageX;
this.lastPageY = pageY;
}
}
What i exactly want is when element
reaches of screen it should be style left: 0;
or etc.
Here is a stack. What am i doing wrong ?
You could add these lines to your onDrag
function:
if(coords.left + deltaX > 0 && coords.right + deltaX < window.innerWidth) {
this.element.nativeElement.style.left = `${coords.left + deltaX}px`;
}
if (coords.top + deltaY > 0 && coords.bottom + deltaY < window.innerHeight) {
this.element.nativeElement.style.top = `${coords.top + deltaY}px`;
}