Javascript:Tippy.js无法用于动态内容

时间:2018-09-18 08:16:25

标签: javascript jquery tippyjs

我有以下技巧,将鼠标悬停在Ajax调用上并获取数据,创建内容并显示。但这不适用于动态内容,因为在页面上加载

<span class="more-tags otherPostTags" data-postId="{{$post->id}}">...</span>

在页面上变为静态,但在选项卡中也变为动态。

因此以下代码适用于静态

<span class="more-tags otherPostTags" data-postId="{{$post->id}}">...</span>

但不适用于动态。

<div id="template" style="display: none;">
    Loading a new image...
</div>

<span class="more-tags otherPostTags" data-postId="{{$post->id}}">...</span>

Tippy jQuery:

const template = document.querySelector('#template');
const initialText = template.textContent;

const tip = tippy('.otherPostTags', {
    animation: 'shift-toward',
    arrow: true,
    html: '#template',
    onShow() {
        const content = this.querySelector('.tippy-content')
        if (tip.loading || content.innerHTML !== initialText) return
        tip.loading = true
        node = document.querySelectorAll('[data-tippy]');
        let id = node[0].dataset.postid;
        $.ajax({
            url: '/get/post/'+id+'/tags',
            type: 'GET',
            success: function(res){
                let preparedMarkup = '';
                res.tags.map(function(item) {
                    preparedMarkup +=
                        '<span class="orange-tag" style="background-color: '+item.color+'">'+
                            item.name +
                        '</span>';
                });
                content.innerHTML = preparedMarkup;
                tip.loading = false
            },
            error: function(error) {
                console.log(error);
                content.innerHTML = 'Loading failed';
                tip.loading = false
            },
        });
    },
    onHidden() {
        const content = this.querySelector('.tippy-content');
        content.innerHTML = initialText;

    },
    popperOptions: {
        modifiers: {
            preventOverflow: {
                enabled: false
            },
            hide: {
                enabled: false
            }
        }
    }
});

我在这里想念什么?

2 个答案:

答案 0 :(得分:1)

如果您希望Tippy在新元素上激活,则需要使用事件委托。 Tippy documentation涵盖了这一点(令人沮丧的是,没有链接的锚点;搜索“事件委托”)。您使用父容器元素,然后告诉Tippy使用什么选择器来匹配子元素。文档中的示例为:

tippy('#parent', {
  target: '.child'
})

...因此,在您的示例中,使用所有.otherPostTags元素都位于的容器(在最坏的情况下为document.body),并使用.otherPostTags作为target

tippy('selectorForParentElement', {
  target: '.otherPostTags'
});

实时示例:

tippy('#container', {
  target: '.otherPostTags'
});
var counter = 0;
var timer = setInterval(function() {
  ++counter;
  var tag = document.createElement("span");
  tag.title = "Tip for span #" + counter;
  tag.className = "otherPostTags";
  tag.innerHTML = "Span #" + counter;
  document.getElementById("container").appendChild(tag);
  if (counter === 6) {
    clearInterval(timer);
  }
}, 250);
.otherPostTags {
  color: white;
  background-color: #2020FF;
  border: 1px solid black;
  padding: 2px;
  margin-left: 2px;
  margin-right: 2px;
  border-radius: 4px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/tippy.js/2.5.4/tippy.css" rel="stylesheet"/>

<div id="container"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/tippy.js/2.5.4/tippy.min.js"></script>

答案 1 :(得分:0)

这在newer Tippy Version中发生了变化。

您需要从tippy.js导入import { delegate } from 'tippy.js'; 方法:

target

比起在根元素上使用委托方法来初始化技巧性的工具提示,并通过delegate( '#root', { target: '[data-tippy-content]' } ); 属性设置将具有实际工具提示的元素的选择器:

#root

请确保body元素确实存在于您的应用中,或使用诸如data-tippy-content之类的其他内容。然后确保为实际元素提供target属性,或相应地更改import numpy import numpy as np import cv2 from pupil_apriltags import Detector import mathutils from google.colab.patches import cv2_imshow numpy.set_printoptions(linewidth=500) camera_matrix = numpy.array([[645.00185337, 0, 304.75823489], [0, 646.18621837, 253.75317465], [0, 0, 1]]) fx = camera_matrix[0][0] fy = camera_matrix[1][1] cx = camera_matrix[0][2] cy = camera_matrix[1][2] camera_intrinsics_vector = [fx, fy, cx, cy] def rot_matrix_to_euler(R): y_rot = numpy.arcsin(R[2][0]) x_rot = numpy.arccos(R[2][2]/numpy.cos(y_rot)) z_rot = numpy.arccos(R[0][0]/numpy.cos(y_rot)) y_rot_angle = y_rot *(180/numpy.pi) x_rot_angle = x_rot *(180/numpy.pi) z_rot_angle = z_rot *(180/numpy.pi) return (x_rot_angle,y_rot_angle,z_rot_angle) image = cv2.imread("apriltag.png") bw_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) detector = Detector() detections = detector.detect(bw_image, estimate_tag_pose=True, camera_params=camera_intrinsics_vector, tag_size=0.076) for detection in detections: center = detection.center.astype(numpy.int32) cv2.circle(image, (center[0], center[1]), 3, (0, 255, 0), -1) corners = detection.corners.astype(numpy.int32) cv2.polylines(image, [corners], True, (0, 255, 0), thickness = 2) orientation = numpy.array([50, 0, 0]) orientation = numpy.transpose(orientation) # endpoint = numpy.matmul(detection.pose_R, orientation).astype(numpy.int32) * 20 orientation_line = numpy.matmul(detection.pose_R, orientation) euler_angles = rot_matrix_to_euler(detection.pose_R) # endpoint = numpy.array( [center[0] + int(orientation_line[0]), center[1] - int(orientation_line[1]) ] ) endpoint = numpy.array( [center[0] + int(numpy.cos(euler_angles[0]) * 100), center[1] - int(numpy.sin(euler_angles[0]) * 100) ] ) cv2.line(image, (center[0], center[1]), (endpoint[0], endpoint[1]), (0, 0, 255), 2) # cv2.line(image, (center[0], center[1]), (center[0] + endpoint[0], center[1] + endpoint[1]), (0, 0, 255), 2) # print(detection.pose_R.shape) # print(detection.pose_t) # print(orientation.shape) # print( numpy.matmul(detection.pose_R, orientation) ) # print(rot_matrix_to_euler(detection.pose_R)) cv2_imshow( image )``` 选择器。