如何设置默认按钮来关闭tippyjs的工具提示

3
我想为每个提示框添加一个关闭按钮,该怎么做?
注意:我不想搞乱设计,所以没有尝试过太多,我在这里寻找最佳解决方案。
以下是演示:

tippy('#t1,#t2,#t3,#t4',{
        content: "Error Message",
        delay: 100,
        arrow: true,
        arrowType: 'round',
        size: 'large',
        duration: 500,
        animation: 'scale',
        trigger:'manual',
        placement:'bottom',
        hideOnClick:false,
 });

var settings = [{id:"#t1",pos:"top"},{id:"#t2",pos:"bottom"}, 
                {id:"#t3",pos:"left"},{id:"#t4",pos:"right"}];

settings.forEach(function(sett){
    var tip = document.querySelector(sett.id);
    tippy(tip);
    tip._tippy.set({content:"click x",placement:sett.pos});
   tip._tippy.show();
});
div.tippy-dummy{
    position: relative;
    width: 220px;
    height: 30px;
    background: #333;
    color: #fff;
}

div.tippy-dummy span{
    position: absolute;
    display: inline-block;
    background: #715a5a;
    right: 0;
    top: 0;
}

.tooltip{
      display:flex;
      justify-content:space-between;
      width: 90%;
}
.btn{
   width: 90px;
      height: 30px;
   border: 2px solid #ccc;
   border-radius: 6px;
}
.btn:hover{
    cursor: pointer;
  background:#f05a27;
}    
.tooltip{
     margin-top:80px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/tippy.js/3.1.3/tippy.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tippy.js/3.1.3/tippy.min.js"></script>
<h4>Expected output:</h4>
<div class="tippy-dummy">
   Please click X to hide
   <span>X</span>
</div>


<hr/>


<div class="tooltip">
     <button id="t1" class="btn tippy" title="">top</button>
     <button id="t2" class="btn tippy" title="">bottom</button>
     <button id="t3" class="btn tippy" title="">Left</button>
     <button id="t4" class="btn tippy" title="">Right</button>
    </div>

为了更好的查看,这里是 CodePen 的链接:https://codepen.io/anon/pen/aPXKZM 请帮我,谢谢!
2个回答

6
实际上,这比Gifford N.的回答还要简单...
<button data-tippy-content="&lt;span class=&quot;closeme&quot;&gt;&amp;times;&lt;/span&gt;There are many like it but this one is mine!!!!!">
  This is my button
</button>

const tippyInstance = tippy('[data-tippy-content]', {
  allowHTML: true,
  interactive: true,
  onShow(instance) {
    instance.popper.querySelector('.closeme').addEventListener('click', () => {
      instance.hide();
    });
  },
  onHide(instance) {
    instance.popper.querySelector('.closeme').removeEventListener('click', () => {
      instance.hide();
    });
  },
});

2
你要查找的操作是 .hide()
为了使用此操作,您需要对代码进行一些调整:
  1. interactive: true 添加到 tippy 设置中。
tippy('#t1,#t2,#t3,#t4',{
   ...
   interactive: true,
   ...
});`

.hide(); 操作分配给 settings.forEach 函数中的 popper 属性:
settings.forEach(function(sett){
  ...
  tip._tippy.popper.onclick = function() {
    tip._tippy.hide();
  }
});

~

小提示:

如果你想在单击弹出窗口内的特定元素(例如X)时隐藏它,只需添加一些标记:

  1. 为所需元素添加一些标记<button class=\"hide\">X</button>

  2. 在onclick中使用该元素:

settings.forEach(function(sett){
  ...
var closeBtn = tip._tippy.popper.getElementsByClassName('hide')[0];
closeBtn.onclick = function() {
  tip._tippy.hide();
}

更新的 Codepen: https://codepen.io/gnowland/pen/wvBzjym?editors=0010


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接