【问题标题】:Create tooltips on Cytoscape Nodes Label using popper and tippy使用 popper 和 tippy 在 Cytoscape 节点标签上创建工具提示
【发布时间】:2021-06-22 17:55:50
【问题描述】:

我正在尝试将 cytoscape 与tippy 一起使用,但它没有显示任何工具提示。它会抛出 ele.popperRef 不是函数的错误。

这里是 stackblitz 链接:https://stackblitz.com/edit/dagre-tippy?file=src%2Fapp%2Fapp.component.ts

我已经添加了所有需要的包,包括popper.js,tippy.js,但它仍然不起作用

【问题讨论】:

    标签: cytoscape.js cytoscape popper.js tippyjs


    【解决方案1】:

    查看https://stackblitz.com/edit/dagre-tippy-wgg8zz

    您不只是正确地导入库并注册 cytoscape.js 扩展。

    你应该用cytoscape.use(popper);注册popper扩展

    您可以将tippy.js 与类似的函数一起使用

    function makePopperWithTippy(node) {
      let ref = node.popperRef(); // used only for positioning
    
      // A dummy element must be passed as tippy only accepts dom element(s) as the target
      // https://atomiks.github.io/tippyjs/v6/constructor/#target-types
      let dummyDomEle = document.createElement("div");
    
      let tip = tippy(dummyDomEle, {
        // tippy props:
        getReferenceClientRect: ref.getBoundingClientRect, // https://atomiks.github.io/tippyjs/v6/all-props/#getreferenceclientrect
        trigger: "manual", // mandatory, we cause the tippy to show programmatically.
    
        // your own custom props
        // content prop can be used when the target is a single element https://atomiks.github.io/tippyjs/v6/constructor/#prop
        content: () => {
          let content = document.createElement("div");
    
          content.innerHTML = "Tippy content";
    
          return content;
        }
      });
    
      tip.show();
    }
    

    另外,请注意,您不必使用 tipp.js。其实只要 popper.js 就够了。

    function makePopper(ele) {
      // create a basic popper on the first node
      let popper1 = ele.popper({
        content: () => {
          let div = document.createElement("div");
    
          div.innerHTML = "Popper content";
    
          document.body.appendChild(div);
    
          return div;
        },
        popper: {} // my popper options here
      });
    }
    

    您可以在下面应用这些功能并查看工具提示。在此之后,基于事件的显示和关闭就很简单了。

    cy.elements().forEach(function(ele) {
      makePopperWithTippy(ele);
      // makePopper(ele);
    });
    

    【讨论】:

    • 我正在尝试在悬停时获取工具提示。您发送的 stackblitz 有 100 个控制台错误,并且无法将鼠标悬停在每个元素/节点上。
    • 另外,我尝试使用 cytoscape.use(popper) 但我收到控制台错误 - 无法为 core 注册 popper,因为 popper 已经存在于原型中并且不能被覆盖stackblitz.com/edit/dagre-tippy-test-q8ndc8?file=src/app/…你的回答和我想做的完全不一样
    • 我的回答向您展示了如何显示tippy.js 徽章。之后,您可以解决如何处理通过悬停事件显示徽章
    • 如果你看到我发给你的 stackblitz 链接,我什至不能使用 cytoscape.use(popper) 因为它给了我一个错误,即 popper 已经存在于原型中。 tippy.js 徽章仅在您创建新的tippy 变量时才有效,但如果您想将tippy 添加到节点引用中,则它不起作用。这就是重点。看起来 cytoscape popper 有问题
    • @LearnAspNet 我怀疑“无法为核心注册 popper,因为原型中已经存在 popper”错误是由于 stackblitz 环境多次调用相同的东西。您不能多次注册分机。在本地主机上尝试。我也看到了那个错误,但我忽略了它。扩展必须注册
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多