div (背景画像を含む) にいくつかの要素をドラッグ アンド ドロップし、コンテンツをアップロードします。アップロードする前に、ドロップされた要素の上、左の位置を親 div に対して計算します。これにより、上、左の位置を使用して、背景画像 ( png または jpeg) が渡されます。しかし問題は、要素が最初に配置された位置と比べてわずかに異なる位置に配置されることです。
アップロード前の HTML
<div id="toget"
class="dropzone"
[ngStyle]="{'width':'100%',
'background-image': 'url('+urlFloorZoneIn+')',
'background-repeat': 'no-repeat',
'background-position': 'center',
'background-size': '100% 100%',
'border':'1px solid black',
'height':'340px',
'position': 'relative'}">
<div class="box"
*ngFor="let existingZone of existingDroppedItemZoneIn"
[ngStyle] = "{'position': 'absolute' ,
'top.%':existingZone.spans[1],
'left.%':existingZone.spans[0]}"
(dragEnd)="onDragEnd($event,existingZone)">
{{ existingZone.main }}
<span>{{existingZone.spans[0]}}</span>
<span>{{existingZone.spans[1]}}</span>
</div>
</div>
上、左の位置を計算するための TS コード
onDragEnd(event,b){
const existingMovingBlockIndex = (this.existingDroppedItemZoneIn.indexOf(this.currentBox));
if(existingMovingBlockIndex>-1){
console.log(b)
console.log(`got drag end x and y ${event.clientX} ${event.clientY}`)
console.log(this.dr.onDragMove);
const container_rect = this.parentparent.nativeElement.getBoundingClientRect();
this.mouse.x = event.clientX - container_rect.left;
this.mouse.y = event.clientY - container_rect.top;
const{width,height} = this.parentparent.nativeElement.getBoundingClientRect();
const perc_x = this.mouse.x / width * 100;
const perc_y = this.mouse.y / height * 100;
this.left = perc_x-5;
this.topLeft = []
this.topLeft.push(this.left);
this.top = perc_y-5;
this.topLeft.push(this.top);
this.existingDroppedItemZoneIn[existingMovingBlockIndex].spans[0] = (perc_x);
this.existingDroppedItemZoneIn[existingMovingBlockIndex].spans[1] = (perc_y);
}
}
CSS
.box {
width: 10%;
height: 10%;
background: rgba(254, 249, 247, 1);
border: 1.5px solid #e24301;
margin: 5px;
line-height: 100px;
text-align: center;
font-size: 0.8rem;
}
.dropzone {
padding: 20px;
margin: 20px 0;
background: lightgray;
min-height: 200px;
border: 1px solid black;
}
レンダリングする前に、上記のアップロードされた HTML コンテンツの top、left、background-image の値を取得し、background-image、top、leftの値を適用して以下のコンテンツをレンダリングします
別の画面でレンダリングした後
[![ここに画像の説明を入力][2]][2]
<ul>
<li #allFloors
*ngFor="let floor of buildings.floors"
[ngStyle]="{'width': singleFloorFlagStyle ?'40%':'100%',
'background-image': 'url('+floor.urlFloorZoneIn+')',
'background-repeat': 'no-repeat',
'background-position': 'center',
'background-size': '100% 100%',
'border':'1px solid black',
'height': singleFloorFlagStyle ? '340px' : '700px',
'position': 'relative', 'max-width': '80%'}"
[ngClass]="{'width':'80% !important'}">
<span (click)="loadFloorInfo(floor._id)">{{ floor.name }}</span>
<div class="fs-heatmap-wrapper__content__box"
*ngFor="let existingZone of floor.droppeditem"
[ngStyle]="{'position':'absolute',
'top.%': existingZone.spans[1],
'left.%': existingZone.spans[0]}">
{{ existingZone.main }}
</div>
</li>
</ul>
CSS
.fs-heatmap-wrapper {
display: grid;
*min-height: 800px;
&__content {
padding: 70px 40px 0;
min-height: 300px;
ul {
margin: 0;
padding: 0;
display: flex;
flex-wrap: wrap;
justify-content: center;
li {
list-style: none;
min-width: 48%;
margin: 12px;
margin-bottom: 2.5%;
span {
cursor: pointer;
background: #fff;
text-align: left;
font-weight: 800;
font-size: 0.9rem;
letter-spacing: 0.05rem;
position: relative;
top: -30px;
}
}
}
&__box{
width: 100px;
height: 100px;
background: rgba(254, 249, 247, 1);
border: 1.5px solid #e24301;
margin: 5px;
line-height: 100px;
text-align: center;
font-size: 0.8rem;
}
}
}