tooltip.js popper.js usage example

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-02 04:26:07

问题


As not very proficient in javascript I don't understand the tooltip.js documentation at all. Why do the not include an example for people like me?

Ho do I have to install this library in order to work correctly?

  1. I add tooltip.js to webpack (installed via npm)
  2. Then I do import tooltip from 'tooltip.js';
  3. Then what?

I tried to use the code from boostrap :

<p data-toggle="tooltip" data-placement="top" title="Tooltip on top">
  Tooltip on top
</p>
$(function () {
  $('[data-toggle="tooltip"]').tooltip()
})

But I don't actually use bootstrap so the error is: TypeError:

$(...).tooltip is not a function

There is some example code on their example page which doesn't really help:

new Tooltip(referenceElement, {
    placement: 'top', // or bottom, left, right, and variations
    title: "Top"
});

What is referenceElement ? Is it the class of the element I whish to trigger?

I imagine something like this:

<p title="xyz" data-toggle="tooltip">hello</p>

And then write the javascript somewhat like this???

new Tooltip('[data-toggle="tooltip"]', {
    placement: 'top',
    trigger: 'hover'
});

That certainly does not work. It returns the error:

TypeError: reference.addEventListener is not a function

How? Why? A little Codepen: https://codepen.io/Sepp/pen/ZowqdM


回答1:


Based on documentation you must call tooltips like this

new Tooltip(referenceElement, {
    placement: 'top', // or bottom, left, right, and variations
    title: "Top"
});

so, if you want make all element with [data-toggle="tooltip"] call tooltips js you can do like this:

$( document ).ready(function() {
  $( '[data-toggle="tooltip"]' ).each(function() {
    new Tooltip($(this), {
      placement: 'top',
    });
  });
});



回答2:


Try with below code:

document.addEventListener('DOMContentLoaded',function(){
    var trigger = document.getElementsByClassName("is-success")[0];
    var instance = new Tooltip(trigger,{
        title: trigger.getAttribute('data-tooltip'),
        trigger: "hover",
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/popper.js"></script>
<script src="https://unpkg.com/tooltip.js"></script>
<button class="button is-success" data-tooltip="Click Here">Hover Me</button>


来源:https://stackoverflow.com/questions/50446140/tooltip-js-popper-js-usage-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!