How to customize the CSS in Angular material 7 drag and drop while dragging, drag preview and drag placeholder?
Would something like below accomplish your goal?
Initialize nativeElement
x and y
dragStart(e, action) {
const rect = e.source.element.nativeElement.getBoundingClientRect();
// initialize start X coord
this.startX = rect.x;
// initialize start Y coord
this.startY = rect.y;
}
Compare X offset and use rendere2
to set style on nativeElement
dragMoved(e, action) {
// record new position
this.currentX = e.event.clientX;
this.currentY = e.event.clientY;
// logic to set startX and startY
// TRYING TO CHANGE CARD BORDER COLOR IF this.endX - this.startX > some number
if(this.startX < this.currentX){
this._renderer.setStyle(e.source.element.nativeElement, 'border-style', 'solid');
this._renderer.setStyle(e.source.element.nativeElement, 'border-color', 'green');
}
else if (this.startX > this.currentX){
this._renderer.setStyle(e.source.element.nativeElement, 'border-style', 'solid');
this._renderer.setStyle(e.source.element.nativeElement, 'border-color', 'blue');
}
}
Revision:
To change color while dragging do the following.
Get reference to #cdkDropList
in the view.
@ViewChild('cdkDropList') _dropList:any;
Set index in *ngFor
*ngFor="let action of actions; let i = index"
Pass index to function
(cdkDragMoved)="dragMoved($event, action, i)"
Receive index and reach into the children of the cdkDropList
to set the style.
dragMoved(e, action, i) {
// record new position
this.currentX = e.event.clientX;
this.currentY = e.event.clientY;
// logic to set startX and startY
// TRYING TO CHANGE CARD BORDER COLOR IF this.endX - this.startX > some number
if(this.startX < this.currentX){
this._renderer.setStyle(this._dropList.nativeElement.children[i], 'border-style', 'solid');
this._renderer.setStyle(this._dropList.nativeElement.children[i], 'border-color', 'green');
}
else if (this.startX > this.currentX){
this._renderer.setStyle(this._dropList.nativeElement.children[i], 'border-style', 'solid');
this._renderer.setStyle(this._dropList.nativeElement.children[i], 'border-color', 'blue');
}
}
Stackblitz
https://stackblitz.com/edit/angular-hdskvc?embed=1&file=app/cdk-drag-drop-overview-example.ts