[英]I would like to show HTML element by event
I'm trying to make a tooltip by clicking a shape.我正在尝试通过单击形状来制作工具提示。 I've done with adding event listener to canvas shape.
我已经将事件监听器添加到 canvas 形状。 However, I could not add tooltip such as HTML element.
但是,我无法添加诸如 HTML 元素之类的工具提示。
I tried to implement tooltip by using css display:none, block我尝试使用 css display:none, block 来实现工具提示
var toolTip = document.createElement('div');
toolTip.style.width = "50px";
toolTip.style.height = "auto";
toolTip.style.padding = "5px";
toolTip.style.border = "1px solid rgb(177, 177, 177)";
toolTip.style.position = "absolute";
toolTip.style.display = "none";
toolTip.style.display = "background: rgba(111,231,43,0.5)";
toolTip.style.zIndex = "10";
canvas.addEventListener("click", e => {
let rect = canvas.getBoundingClientRect();
this.mousePos = {
x: e.clientX - rect.left,
y: e.clientY - rect.top
};
console.log(this.mousePos.x, this.mousePos.y);
if(0 <= this.mousePos.x <300 && 100 <= this.mousePos.y <110) {
console.log("mouseClicked");
toolTip.style.display = "block";
toolTip.style.left = this.mousePos.x + this.mousePos.x * 0.02;
toolTip.style.top = this.mousePos.y + this.mousePos.y * 0.02;
toolTip.innerHTML = "<p> hello </p>";
}
});
https://codepen.io/chikichaka/pen/NWRjWyG https://codepen.io/chikichaka/pen/NWRjWyG
You create a div element to act as a tooltip but you never append the tooltip to a container.您创建一个 div 元素来充当工具提示,但您永远不会将工具提示 append 到容器中。 You must append the div to a container, such as the body
你必须把 append 的 div 放到一个容器中,比如 body
document.body.appendChild(toolTip);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.