Skip to content

Commit 683fa3e

Browse files
committed
fix: fix bug of tooltip first show position
1 parent ffe81b7 commit 683fa3e

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

packages/vchart/src/plugin/components/tooltip-handler/dom-tooltip-handler.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ export class DomTooltipHandler extends BaseTooltipHandler {
9393
const tooltipSpec = this._component.getSpec() as ITooltipSpec;
9494
const tooltipElement = document.createElement('div');
9595
const themeFontFamily = this._chartOption?.getTheme('fontFamily');
96-
9796
setStyleToDom(tooltipElement, {
9897
left: '0',
9998
top: '0',
@@ -111,14 +110,8 @@ export class DomTooltipHandler extends BaseTooltipHandler {
111110
maxWidth: '100wh',
112111
maxHeight: '100vh',
113112
visibility: 'hidden',
114-
...this._domStyle.panel,
115-
transitionDuration: '0s'
113+
...this._domStyle.panel
116114
} as CSSStyleDeclaration);
117-
// 初次有动画时间可能导致tooltip展示时从不期望的位置飘过来
118-
isValid(this._domStyle.panel.transitionDuration) &&
119-
setTimeout(() => {
120-
tooltipElement.style.transitionDuration = this._domStyle.panel.transitionDuration ?? '0s';
121-
}, 0);
122115
tooltipElement.classList.add(tooltipSpec.className);
123116
tooltipElement.setAttribute('vchart-tooltip-id', `${this.id}`);
124117
this._container.appendChild(tooltipElement);
@@ -162,35 +155,33 @@ export class DomTooltipHandler extends BaseTooltipHandler {
162155
if (!params.changePositionOnly) {
163156
this._tooltipActual = activeTooltipSpec;
164157
}
165-
const currentVisible = this.getVisibility();
166-
167158
// 位置
168159
const el = this._rootDom;
169160
if (el) {
170161
const { x = 0, y = 0 } = activeTooltipSpec.position ?? {};
162+
let position = { x, y };
163+
const currentVisible = this.getVisibility();
171164
if (tooltipSpec.updateElement) {
172165
// 此处先设定一次位置,防止页面暂时出现滚动条(优先设置上次的位置)
173166
this._updatePosition(this._cacheCustomTooltipPosition ?? { x, y });
174167
// 更新 tooltip dom
175168
tooltipSpec.updateElement(el, activeTooltipSpec, params);
176169
// 重新计算 tooltip 位置
177-
const position = this._getActualTooltipPosition(activeTooltipSpec, params, {
170+
position = this._getActualTooltipPosition(activeTooltipSpec, params, {
178171
width: el.offsetWidth,
179172
height: el.offsetHeight
180173
});
181-
// 更新位置
182-
this._updatePosition(position);
183174
// 更新缓存
184175
this._cacheCustomTooltipPosition = position;
185-
} else {
186-
if (!currentVisible) {
187-
// 当从隐藏切换到显示的时候,需要先设置一次 transition 为 0ms,防止出现从一个非常远的初始位置进行动画
188-
this._rootDom.style.transitionDuration = '0ms';
189-
} else {
190-
this._rootDom.style.transitionDuration = this._domStyle.panel.transitionDuration ?? 'initial';
191-
}
192-
this._updatePosition({ x, y });
193176
}
177+
// 首次从false展示需要先设置一次位置,防止出现从一个非常远的初始位置进行动画
178+
if (!currentVisible && visible) {
179+
this._rootDom.style.transition = 'none';
180+
this._updatePosition(position, false);
181+
this._rootDom.getBoundingClientRect();
182+
}
183+
// 更新位置
184+
this._updatePosition(position);
194185
}
195186
this.setVisibility(visible);
196187
}
@@ -423,10 +414,14 @@ export class DomTooltipHandler extends BaseTooltipHandler {
423414
}
424415
}
425416

426-
protected _updatePosition({ x, y }: ITooltipPositionActual) {
417+
protected _updatePosition({ x, y }: ITooltipPositionActual, resetTransition = true) {
427418
if (this._rootDom) {
428419
// translate3d 性能较好:https://stackoverflow.com/questions/22111256/translate3d-vs-translate-performance
429420
this._rootDom.style.transform = `translate3d(${x}px, ${y}px, 0)`;
421+
if (resetTransition && this._rootDom.style.transition !== '') {
422+
this._rootDom.style.transition = '';
423+
Object.assign(this._rootDom.style, this._domStyle.panel);
424+
}
430425
}
431426
}
432427
}

0 commit comments

Comments
 (0)