admin管理员组文章数量:1290935
I've installed tippy.js to handle my tooltips, however I am struggling to get the tooltips to display the content set from the data attribute. My default tooltip is working fine, however when I initialise by a class, to be able to add a different style to the tooltip, it doesn't get the content from the tooltip.
tippy.setDefaults({
animation: 'scale',
animateFill: false,
maxWidth: 240,
duration: 0,
arrow: false,
})
tippy('.js-tippy-reviews', {
theme: 'reviews',
animation: 'scale',
animateFill: false,
maxWidth: 240,
duration: 0,
arrow: false,
})
If I add the content method to tippy it will display, however since the content of the tooltip is dynamic, i need to set it on the data attribute. I am unsure how to pass the data attribute from the element into tippy.
content: this.dataset.tippy
Any ideas what I'm doing wrong?
HTML:
<div class="js-tippy-reviews reviews-notification" data-tippy="2 Pending Reviews"></div>
I've installed tippy.js to handle my tooltips, however I am struggling to get the tooltips to display the content set from the data attribute. My default tooltip is working fine, however when I initialise by a class, to be able to add a different style to the tooltip, it doesn't get the content from the tooltip.
tippy.setDefaults({
animation: 'scale',
animateFill: false,
maxWidth: 240,
duration: 0,
arrow: false,
})
tippy('.js-tippy-reviews', {
theme: 'reviews',
animation: 'scale',
animateFill: false,
maxWidth: 240,
duration: 0,
arrow: false,
})
If I add the content method to tippy it will display, however since the content of the tooltip is dynamic, i need to set it on the data attribute. I am unsure how to pass the data attribute from the element into tippy.
content: this.dataset.tippy
Any ideas what I'm doing wrong?
HTML:
<div class="js-tippy-reviews reviews-notification" data-tippy="2 Pending Reviews"></div>
Share
Improve this question
edited Jun 21, 2019 at 15:51
marc_s
755k184 gold badges1.4k silver badges1.5k bronze badges
asked Apr 16, 2019 at 16:05
lkylky
1,1293 gold badges18 silver badges38 bronze badges
2
- Could you provide the DOM element? – Recep Karadas Commented Apr 16, 2019 at 16:18
- I've added the html to the question :) – lky Commented Apr 16, 2019 at 16:21
2 Answers
Reset to default 8You could add the onShow() function and read it from the instance and set the content from the reference dataset.
tippy.setDefaults({
animation: 'scale',
animateFill: false,
maxWidth: 240,
duration: 0,
arrow: false,
});
tippy('.js-tippy-reviews', {
theme: 'reviews',
animation: 'scale',
animateFill: false,
maxWidth: 240,
duration: 0,
arrow: false,
onShow(instance) {
instance.popper.hidden = instance.reference.dataset.tippy ? false : true;
instance.setContent(instance.reference.dataset.tippy);
}
});
$(document).ready(function(){
$("#btn-change-data").click(function()
{
$(".js-tippy-reviews").attr("data-tippy",$("#test-input").val());
})
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg./popper.js@1"></script>
<script src="https://unpkg./tippy.js@4"></script>
<div class="js-tippy-reviews reviews-notification" data-tippy="test">CONTENT</div>
<input type="text" id="test-input" />
<button id="btn-change-data">Change data-tippy</button>
This solution works
tippy('.js-tippy-reviews', {
content: (reference) => reference.dataset.tippy,
})
本文标签: javascriptTippyjs not displaying content from the data attributeStack Overflow
版权声明:本文标题:javascript - Tippy.js not displaying content from the data attribute? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741516269a2382907.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论