我将一些元素拖放到 div(包含背景图像)上并上传内容。在上传之前,我计算放置的元素顶部,左侧位置 wrt 到父 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;
}
}
}