1

我用tippy.js创建了多个工具提示内容但我不能制作动态内容。它仅显示第一个工具提示模板。

第二个问题是,当我放入style: display:none工具提示包装器时,工具提示不显示。当我删除style: display:none时,工具提示内容显示正常(必须隐藏)。

如何使用子模板制作工具提示?

这里还有 codepen 示例:https ://codepen.io/wpuzman/pen/mQeeRQ

$(document).ready(function() {
  $('.box').each(function(){
      tippy(this, {
          arrow: true,
          arrowType: 'round',
          size: 'large',
          duration: 500,
          animation: 'scale',
          placement: 'left',
          interactive: true,
          theme: 'google',
          content: document.querySelector('.tooltip_templates').cloneNode(true)
      });
  });
});
.wrap {
  margin: 150px;
  position: relative;
}

.box {
  float: left;
  margin-right: 10px;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/tippy.js@3/dist/tippy.all.min.js"></script>
<div class="wrap">
  <div class="box">
    <img src="https://picsum.photos/50/50" alt="">

    <div class="tooltip_templates" style="display: none;">
      <div class="tooltip-content">
        <img src="https://picsum.photos/150/150" alt="">
        <h3>Test image 1</h3>
      </div>
    </div>
  </div>

  <div class="box">
    <img src="https://picsum.photos/50/50" alt="">

    <div class="tooltip_templates" style="display: none;">
      <div class="tooltip-content">
        <img src="https://picsum.photos/150/150" alt="">
        <h3>Test image 2</h3>
      </div>
    </div>
  </div>

  <div class="box">
    <img src="https://picsum.photos/50/50" alt="">

    <div class="tooltip_templates" style="display: none;">
      <div class="tooltip-content">
        <img src="https://picsum.photos/150/150" alt="">
        <h3>Test image 3</h3>
      </div>
    </div>
  </div>

  <div class="box">
    <img src="https://picsum.photos/50/50" alt="">

    <div class="tooltip_templates" style="display: none;">
      <div class="tooltip-content">
        <img src="https://picsum.photos/150/150" alt="">
        <h3>Test image 4</h3>
      </div>
    </div>
  </div>

  <div class="box">
    <img src="https://picsum.photos/50/50" alt="">

    <div class="tooltip_templates" style="display: none;">
      <div class="tooltip-content">
        <img src="https://picsum.photos/150/150" alt="">
        <h3>Test image 5</h3>
      </div>
    </div>
  </div>

  <div class="box">
    <img src="https://picsum.photos/50/50" alt="">

    <div class="tooltip_templates" style="display: none;">
      <div class="tooltip-content">
        <img src="https://picsum.photos/150/150" alt="">
        <h3>Test image 6</h3>
      </div>
    </div>
  </div>
</div>

4

1 回答 1

2

使用当前元素选择其模板:

$(document).ready(function() {
  $('.box').each(function(){
      tippy(this, {
          arrow: true,
          arrowType: 'round',
          size: 'large',
          duration: 500,
          animation: 'scale',
          placement: 'left',
          interactive: true,
          theme: 'google',
          content: this.querySelector('.tooltip_templates')
      });
  });
});

this.querySelector('.tooltip_templates')将选择具有该类的孩子作为内容。

您还可以.each()通过使用以下函数来避免循环content

tippy('.box', {
    animateFill: false,
    arrow: true,
    arrowType: 'round',
    size: 'large',
    duration: 500,
    animation: 'scale',
    placement: 'left',
    interactive: true,
    theme: 'google',
    content: function (reference) {
        return reference.querySelector('.tooltip_templates');
    }
});
于 2018-11-25T23:36:23.153 回答