Javascript JS工具提示定位闪烁
编写一个纯JS工具提示库,并使其非常非常好地工作。没有大铃铛和口哨,只是一个超轻超实用的易于定制的工具提示。也就是说,我在更新职位方面遇到了问题;特别是当检测到工具提示超出范围时 而且它有效!是的。问题是,当我将鼠标悬停在目标元素上时,该元素紧靠页面右侧,导致工具提示超出范围,它将在默认的右下角位置和正确的左下角位置之间闪烁。进一步检查后,我移动光标时,它似乎只会在每一根头发上注册一次。例如,当我输入元素时,它将正确显示;将光标向任何方向移动一根头发,它似乎会重置为右下角,而不会看到它超出范围。再向前移动一根头发,它就会发现自己又出界了,并将自己纠正到左下角。我不确定这里要做什么假设,但我会让你们来判断Javascript JS工具提示定位闪烁,javascript,position,tooltip,flicker,screen-positioning,Javascript,Position,Tooltip,Flicker,Screen Positioning,编写一个纯JS工具提示库,并使其非常非常好地工作。没有大铃铛和口哨,只是一个超轻超实用的易于定制的工具提示。也就是说,我在更新职位方面遇到了问题;特别是当检测到工具提示超出范围时 而且它有效!是的。问题是,当我将鼠标悬停在目标元素上时,该元素紧靠页面右侧,导致工具提示超出范围,它将在默认的右下角位置和正确的左下角位置之间闪烁。进一步检查后,我移动光标时,它似乎只会在每一根头发上注册一次。例如,当我输入元素时,它将正确显示;将光标向任何方向移动一根头发,它似乎会重置为右下角,而不会看到它超出范围。
updateTooltip(event) {
const mouseX = event.pageX,
mouseY = event.pageY;
const adjustment = 15;
let position = (this.element.getAttribute(this.selector + '-pos') || '').split(' ');
if (position.length !== 2) position = [ 'bottom', 'right' ];
var isOut = this.checkBounds();
if (isOut.top) { position[0] = 'bottom'; }
if (isOut.left) { position[1] = 'right'; }
if (isOut.bottom) { position[0] = 'top'; }
if (isOut.right) { position[1] = 'left'; }
let vertical, horizontal;
switch (position[0]) {
case 'top':
vertical = -adjustment - this.tooltip.offsetHeight;
break;
case 'center':
vertical = 0 - (this.tooltip.offsetHeight / 2);
break;
default:
vertical = adjustment;
}
switch (position[1]) {
case 'left':
horizontal = -adjustment - this.tooltip.offsetWidth;
break;
case 'center':
horizontal = 0 - (this.tooltip.offsetWidth / 2);
break;
default:
horizontal = adjustment;
}
this.tooltip.style.top = mouseY + vertical + 'px';
this.tooltip.style.left = mouseX + horizontal + 'px';
}
checkBounds(){
如果(!this.tooltip)返回;
const bounds=this.tooltip.getBoundingClientRect();
放出={};
out.top=bounds.top<0;
out.left=bounds.left<0;
out.bottom=bounds.bottom>(window.innerHeight | | document.documentElement.clientHeight);
out.right=bounds.right>(window.innerWidth | | document.documentElement.clientWidth);
out.any=out.top | | out.left | | | out.bottom | | out.right;
out.all=out.top&&out.left&&out.bottom&&out.right;
返回;
}
checkBounds() {
if (!this.tooltip) return;
const bounds = this.tooltip.getBoundingClientRect();
let out = {};
out.top = bounds.top < 0;
out.left = bounds.left < 0;
out.bottom = bounds.bottom > (window.innerHeight || document.documentElement.clientHeight);
out.right = bounds.right > (window.innerWidth || document.documentElement.clientWidth);
out.any = out.top || out.left || out.bottom || out.right;
out.all = out.top && out.left && out.bottom && out.right;
return out;
}