【发布时间】:2019-10-05 18:13:06
【问题描述】:
在我的 vis.js 网络中,我想在单击节点时在节点的位置出现一个弹出窗口。
我使用了getPositions方法,但是弹窗出现在错误的位置(左上角太多),好像坐标不正确。
network.on("click", function (params) {
// Get the node ID
var nodeId = params.nodes[0];
if (nodeId) {
// Get the node title to show in the popup
var popup = this.body.nodes[nodeId].options.title;
// Get the node coordinates
var nodePosition = network.getPositions(nodeId);
var nodeX = nodePosition[nodeId].x;
var nodeY = nodePosition[nodeId].y;
// Show the tooltip in a div
document.getElementById('popup').innerHTML = popup;
document.getElementById('popup').style.display = "block";
// Place the div
document.getElementById('popup').style.position = "absolute";
document.getElementById('popup').style.top = nodeY+'px';
document.getElementById('popup').style.left = nodeX+'px';
}
});
// Empty and hide the div when click elsewhere
network.on("deselectNode", function (params) {
document.getElementById('popup').innerHTML = null;
document.getElementById('popup').style.display = "none";
});
【问题讨论】:
-
为了更快的响应:创建一个 jsfiddle 或类似的东西,这样用户就可以用自己的眼睛快速看到问题。
-
另外,如果弹出窗口总是与顶部对齐过多并且左侧不能通过在代码中添加一些像素来解决这个问题?喜欢:style.top = 10 + nodeY + 'px'
-
我刚刚在我的帖子中放了一个链接来查看结果。我无法在我的代码中添加像素,因为每个弹出窗口的偏移量都不是完全相同的。我认为它们都有节点的形状,但看起来它们不在同一个缩放级别上,就像我的节点在缩放 1 并且 getPositions 返回的节点的位置在缩放 1.5(我不t 实际上在任何地方都有这些值,这只是它的外观示例)。
-
我现在看到了。请再次从您的问题中删除链接。以防止将来出现断开的链接。对于这样的图表,可以包含屏幕截图。
标签: javascript popup vis.js vis.js-network