draggable div 应该避免 x 和 y 重叠
draggable div should be avoid x and y overlap
我对重叠屏幕区域的可拖动元素有疑问。这是代码示例:
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;
}
}
我真正想要的是当 element
到达屏幕时它应该是样式 left: 0;
或等等。
这是一个stack。我究竟做错了什么 ?
您可以将这些行添加到您的 onDrag
函数中:
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`;
}
我对重叠屏幕区域的可拖动元素有疑问。这是代码示例:
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;
}
}
我真正想要的是当 element
到达屏幕时它应该是样式 left: 0;
或等等。
这是一个stack。我究竟做错了什么 ?
您可以将这些行添加到您的 onDrag
函数中:
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`;
}