Skip to content

Commit d158b87

Browse files
zcg1991rdkmaster
authored andcommitted
[故障] float指令弹出位置算法修正
1 parent c88e612 commit d158b87

File tree

4 files changed

+79
-28
lines changed

4 files changed

+79
-28
lines changed

src/app/demo/float/pos-reviser/demo.component.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,29 @@
4444
</ng-template>
4545
</span>
4646

47+
<br><br>
48+
49+
<div j-movable style="position: fixed" class="movable-box">
50+
<span class="fa fa-bars" jigsaw-float jigsawFloatPosition="leftTop"
51+
[jigsawFloatTarget]="jigsawFloatArea5" [jigsawFloatPosition]="pos.value"
52+
style="margin: 40px 16px 40px 0">
53+
鼠标挪过来
54+
<ng-template #jigsawFloatArea5>
55+
<div class="jigsawFloatArea" style="width: 300px; height: 150px;">
56+
<p>
57+
请注意:<br>
58+
如果选中的弹出位置超出了当前的可视区域,<br>
59+
float指令会采用内置算法调整成其他最佳弹出位置。<br>
60+
请试试把这个块拖到屏幕的4个角落,<br>
61+
再触发下拉,查看位置调整的效果
62+
</p>
63+
</div>
64+
</ng-template>
65+
</span>
66+
<span>切换位置</span>
67+
<jigsaw-select #pos value="bottomRight"
68+
[data]="['bottomLeft', 'bottomRight', 'topLeft', 'topRight',
69+
'leftTop', 'leftBottom', 'rightTop', 'rightBottom']">
70+
</jigsaw-select>
71+
</div>
72+

src/app/demo/float/pos-reviser/demo.component.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ import {FloatBasicDemo} from "../basic/demo.component";
1616
color: #fff;
1717
text-align: center;
1818
}
19-
19+
20+
.movable-box {
21+
background-color: #b5d3dc;
22+
display: inline-block;
23+
border-radius: 4px;
24+
padding: 12px;
25+
cursor: move;
26+
}
27+
2028
p {
2129
margin: 10px;
2230
}

src/app/demo/float/pos-reviser/demo.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import {NgModule} from "@angular/core";
22
import {CommonModule} from "@angular/common";
33
import {JigsawDemoDescriptionModule} from "app/demo-description/demo-description";
44
import {JigsawFloatModule} from "jigsaw/directive/float";
5+
import {JigsawMovableModule} from "jigsaw/directive/movable";
6+
import {JigsawSelectModule} from "jigsaw/component/select/select";
57
import {FloatPosReviserDemo} from "./demo.component";
68

79
@NgModule({
810
imports: [
9-
CommonModule, JigsawDemoDescriptionModule, JigsawFloatModule
11+
CommonModule, JigsawDemoDescriptionModule, JigsawFloatModule, JigsawMovableModule,
12+
JigsawSelectModule
1013
],
1114
declarations: [FloatPosReviserDemo],
1215
exports: [FloatPosReviserDemo]

src/jigsaw/directive/float/float.ts

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -361,37 +361,51 @@ export class JigsawFloat extends AbstractJigsawViewBase implements OnDestroy {
361361
private _positionReviser(pos: PopupPositionValue, popupElement: HTMLElement): PopupPositionValue {
362362
const offsetWidth = this._elementRef.nativeElement.offsetWidth;
363363
const offsetHeight = this._elementRef.nativeElement.offsetHeight;
364+
// 调整上下左右位置
364365
if (this.jigsawFloatPosition === 'topLeft' || this.jigsawFloatPosition === 'topRight' ||
365366
this.jigsawFloatPosition === 'bottomLeft' || this.jigsawFloatPosition === 'bottomRight') {
366-
// 调整上下位置
367367
const upDelta = offsetHeight + popupElement.offsetHeight;
368-
if (document.body.clientHeight <= upDelta) {
369-
// 可视区域比弹出的UI高度还小就不要调整了
370-
return pos;
371-
}
372-
const totalHeight = window.pageYOffset + document.body.clientHeight;
373-
if (pos.top < 0 && pos.top + upDelta <= totalHeight) {
374-
// 上位置不够且下方位置足够的时候才做调整
375-
pos.top += upDelta;
376-
} else if (pos.top + popupElement.offsetHeight >= totalHeight && pos.top >= upDelta) {
377-
// 下方位置不够且上方位置足够的时候才做调整
378-
pos.top -= upDelta;
379-
}
368+
pos = this._calPositionY(pos, upDelta, popupElement);
369+
const leftDelta = popupElement.offsetWidth;
370+
pos = this._calPositionX(pos, leftDelta, popupElement);
380371
} else {
381-
// 调整左右位置
372+
const upDelta = popupElement.offsetHeight;
373+
pos = this._calPositionY(pos, upDelta, popupElement);
382374
const leftDelta = popupElement.offsetWidth + offsetWidth;
383-
if (document.body.clientWidth <= leftDelta) {
384-
// 可视区域比弹出的UI高度还小就不要调整了
385-
return pos;
386-
}
387-
const totalWidth = window.pageXOffset + document.body.clientWidth;
388-
if (pos.left < 0 && pos.left + leftDelta <= totalWidth) {
389-
// 左边位置不够且右边位置足够的时候才做调整
390-
pos.left += leftDelta;
391-
} else if (pos.left + popupElement.offsetWidth >= totalWidth && pos.left >= leftDelta) {
392-
// 右边位置不够且左边位置足够的时候才做调整
393-
pos.left -= leftDelta;
394-
}
375+
pos = this._calPositionX(pos, leftDelta, popupElement);
376+
377+
}
378+
return pos;
379+
}
380+
381+
private _calPositionX(pos, leftDelta, popupElement) {
382+
if (document.body.clientWidth <= leftDelta) {
383+
// 可视区域比弹出的UI宽度还小就不要调整了
384+
return pos;
385+
}
386+
const totalWidth = window.pageXOffset + document.body.clientWidth;
387+
if (pos.left < 0 && pos.left + leftDelta + popupElement.offsetWidth <= totalWidth) {
388+
// 左边位置不够且右边位置足够的时候才做调整
389+
pos.left += leftDelta;
390+
} else if (pos.left + popupElement.offsetWidth >= totalWidth && pos.left >= leftDelta) {
391+
// 右边位置不够且左边位置足够的时候才做调整
392+
pos.left -= leftDelta;
393+
}
394+
return pos;
395+
}
396+
397+
private _calPositionY(pos, upDelta, popupElement) {
398+
if (document.body.clientHeight <= upDelta) {
399+
// 可视区域比弹出的UI高度还小就不要调整了
400+
return pos;
401+
}
402+
const totalHeight = window.pageYOffset + document.body.clientHeight;
403+
if (pos.top < 0 && pos.top + upDelta + popupElement.offsetHeight <= totalHeight) {
404+
// 上位置不够且下方位置足够的时候才做调整
405+
pos.top += upDelta;
406+
} else if (pos.top + popupElement.offsetHeight >= totalHeight && pos.top >= upDelta) {
407+
// 下方位置不够且上方位置足够的时候才做调整
408+
pos.top -= upDelta;
395409
}
396410
return pos;
397411
}

0 commit comments

Comments
 (0)