简体   繁体   English

用于处理 SVG object 文件中元素的工具提示插件

[英]Tooltipster plugin to work on elements in an SVG object file

I've been using the Tooltipster plugin for various elements on my website (recommend this plugin btw) and I've decided that I'd like to use it on elements within an SVG object file but this is turning out to be a lot more difficult than first thought.我一直在为我网站上的各种元素使用Tooltipster插件(顺便说一句,推荐这个插件),我决定我想在 SVG object 文件中的元素上使用它,但事实证明这是更多比最初想象的要难。

So on my html page, I have the object:所以在我的 html 页面上,我有 object:

<object style="width:100%; height:auto;" class="navmap" type="image/svg+xml" data="location.svg">
Stop using that old-ass Internet Explorer XD!!
</object>

A simple SVG mockup for the sake of this question (my svgs are huge):为了这个问题,一个简单的 SVG 模型(我的 svg 很大):

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" 
xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
 width="377.7px" height="377.7px" viewBox="0 0 377.7 377.7" enable-
background="new 0 0 377.7 377.7" xml:space="preserve">


<g>
    <circle fill="#898989" cx="188.85" cy="188.85" r="188.85"/>
</g>


<a xlink:href="#" target="_top" title="Give that circle a tooltip" 
class="tooltip">
    <g>
        <circle fill="#231F20" cx="186.2" cy="192.01" r="82.5"/>
    </g>
</a>

</svg> 

How Tooltipster works:工具提示如何工作:

For those of you who aren't familiar with tooltipster, all you have to do is assign a class or id to the tooltipster plugin, like so:对于那些不熟悉 tooltipster 的人,您所要做的就是为 tooltipster 插件分配一个 class 或 id,如下所示:

<img src="my-image.png" class="tooltip" title="This is my image's 
tooltip message!" />

$(document).ready(function() {
        $('.tooltip').tooltipster();
});

and Tooltipster will use the title attribute of an element for the tooltip as shown above OR you can use javascript to generate the tooltip message: Tooltipster 将使用工具提示元素的 title 属性,如上所示,或者您可以使用 javascript 生成工具提示消息:

<img src="my-image.png" class="tooltip" />

$('.tooltip').tooltipster({ content: 'This is my image's 
tooltip message!' });

Dead simple!!死简单!

Back to my svg issue回到我的 svg issue

So I'm trying to get the tooltipster plugin to work on the a xlink element in my svg BUT it turns out that using those general methods above doesn't work for elements within an SVG object .所以我试图让工具提示插件在我的 svg 中的 xlink元素上工作,但事实证明,使用上面的那些通用方法不适用于SVG object 中的元素

Why is this?为什么是这样?

First off, do I NEED to link my javascript file in the SVG file like so (like you would do with an external style sheet)...首先,我是否需要像这样将我的 javascript 文件链接到 SVG 文件中(就像您使用外部样式表一样)...

<script xlink:href="js/scripts.js" />

... in order for it to make use of javascript (and tooltipster plugin)? ...为了让它使用 javascript(和工具提示插件)? I've tried this already however and it doesn't work but since nothing has worked yet so far, I'd like to know if linking to my external js file is required?但是我已经尝试过了,但它不起作用,但由于到目前为止还没有任何效果,我想知道是否需要链接到我的外部 js文件? NOTE: I've put the tooltipster plugin into my external js file being - scripts.js注意:我已将工具提示插件放入我的外部 js 文件中 - scripts.js

Nothing I do is working.我所做的一切都没有用。 I can only assume that this...我只能假设这...

$(document).ready(function() {
        $('.tooltip').tooltipster();
});

... cannot find the element in the SVG file. ... 在 SVG 文件中找不到元素。

I've put together a simple FIDDLE - please take a look.我整理了一个简单的FIDDLE - 请看一看。

I've done my research online and there aren't any posts related to Tooltips for elements in an SVG object using the-Tooltipster plugin and "solutions" for other peoples queries ain't working for my case.我已经在线完成了我的研究,并且没有任何与SVG object 中使用工具提示插件的元素的工具提示相关的帖子,其他人查询的“解决方案”不适用于我的案例。

I'd appreciate your help and guidance感谢您的帮助和指导

UPDATE:更新:

I've tried this technique found online but I don't know if I'm doing this correct otherwise I'm really clutching straws at the moment:我已经尝试过在网上找到的这种技术,但我不知道我这样做是否正确,否则我现在真的很抓紧救命稻草:

window.onload=function() {
// Get the Object by ID
var svg = document.getElementById("circle-svg");
// Get the SVG document inside the Object tag
var svgDoc = svg.contentDocument;
// Get one of the SVG items by ID;
var circle = svgDoc.getElementById("tooltip");
// Set the colour to something else
circle.tooltipster();
};

Fiddle using code above.小提琴使用上面的代码。

*LATEST UPDATE *最新更新

Ah!!!啊!!! (Thanks to Evan) I made such an amateur mistake and forgot to include jquery library in my fiddle!! (感谢 Evan)我犯了一个业余的错误,忘了在我的小提琴中包含 jquery 库!! But this still hasn't solved my problem!但这仍然没有解决我的问题!

Okay so please see updated Fiddle :好的,请查看更新的 Fiddle

HTML: HTML:

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" 
xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
 width="377.7px" height="377.7px" viewBox="0 0 377.7 377.7" enable-
background="new 0 0 377.7 377.7" xml:space="preserve">


<g>
    <circle fill="#898989" cx="188.85" cy="188.85" r="188.85"/>
</g>


<a xlink:href="#" target="_top" title="Give that circle a tooltip" 
class="tooltip">
    <g>
    <circle fill="#231F20" cx="186.2" cy="192.01" r="82.5"/>
    </g>
 </a>

JS:记者:

$('.tooltip').tooltipster();

As you can see in the fiddle, the tooltip works 100% fine now (this is because the SVG code is IN the HTML. As soon as I link the SVG in my html as an OBJECT (Like I'm doing on my website) like so...正如您在小提琴中看到的那样,工具提示现在可以 100% 正常工作(这是因为 SVG 代码在HTML中。只要我将 SVG 链接到我的 html 中作为OBJECT (就像我在我的网站上所做的那样)像这样...

<object style="width:100%; height:auto;" class="navmap" 
type="image/svg+xml" data="location.svg">
Stop using that old-ass Internet Explorer XD!!
</object>

... the tooltip doesn't work? ...工具提示不起作用? What am I missing?我错过了什么? How do I get the javascript to work for the title attribute in the xlink above?如何让 javascript 为上面的 xlink 中的 title 属性工作? Do I need to put the javascript IN my SVG file or?我需要将 javascript 放入我的 SVG 文件中还是? Do I need to somehow GetelementsbyID now that it's an object?既然它是一个对象,我是否需要以某种方式 GetelementsbyID?

Help much appreciated once again再次帮助非常感谢

This seems to be a very old issue but I still couldn't find a simple solution as of Jan 2023.这似乎是一个非常古老的问题,但截至 2023 年 1 月我仍然找不到简单的解决方案。

This may not completely apply to the case above because I am making a website with Python-Flask-Bootstrap.这可能并不完全适用于上述情况,因为我正在使用 Python-Flask-Bootstrap 制作网站。 I wanted to make a simple SVG map in order to make tooltips show on a responsive image.我想制作一个简单的SVG map 以便在响应图像上显示工具提示。

I tried tippy.js but there is an issue that didn't seem very easy to fix so switched to tooltipster instead.我试过tippy.js ,但有一个问题似乎不太容易解决,所以改用tooltipster Tooltipster works for me but it seem not to be in active development unfortunately. Tooltipster对我有用,但不幸的是它似乎没有在积极开发中。

With tooltipster I was getting an error message:使用tooltipster我收到一条错误消息:

c.SVG is undefined c.SVG 未定义

So installed svg.js as recommended on tooltipster's site and it finally started working properly.所以按照tooltipster 网站上的建议安装了svg.js ,它终于开始正常工作了。

Here are the scripts in my scripts block relevant to tooltipster :以下是我的脚本块中与tooltipster相关的脚本:

<script type="text/javascript" src="{{ url_for('static', filename='node_modules/jquery/dist/jquery.min.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='node_modules/@svgdotjs/svg.js/dist/svg.min.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='node_modules/tooltipster/dist/js/tooltipster.bundle.min.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='node_modules/tooltipster/dist/js/plugins/tooltipster/SVG/tooltipster-SVG.min.js') }}"></script>

Finally, the image was not displaying on my phone (iOS 16) which was why I needed a svg map in the first place.最后,图像没有显示在我的手机(iOS 16)上,这就是为什么我首先需要一个svg map的原因。 Nothing really helped apart from adding position: absolute to the <svg> tag but that wasn't helpful because it messed up the picture size.除了将position: absolute添加到<svg>标签之外,没有任何帮助,但这没有帮助,因为它弄乱了图片大小。 What actually did the trick was changing the <div> class from class="container d-flex" to class="container d-flex flex-column" .真正的诀窍是将<div> class 从class="container d-flex"更改为class="container d-flex flex-column" I am not exactly sure why that works but it is now displaying as I intended on all devices.我不确定为什么会这样,但它现在在所有设备上都按照我的预期显示。 Here is how it looks:这是它的样子:

<div id="mainImageContainer" class="container d-flex flex-column">
  <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
    viewBox="0 0 1181 2362">
    <image id="mainImage" xlink:href="{{ url_for('static', filename='leads/figure1_mobile.png') }}"></image>
    <a xlink:href="#">
      <rect x="238" y="2003" fill="#fff" opacity="0" width="294" height="198"></rect>
    </a>
    <a href="#" id="ekgPop" class="tipster">
      <rect x="155" y="1185" fill="#fff" opacity="0" width="205" height="247"></rect>
    </a>
    <a xlink:href="#">
      <rect x="808" y="1195" fill="#fff" opacity="0" width="205" height="244"></rect>
    </a>
    <a xlink:href="#">
      <rect x="663" y="2005" fill="#fff" opacity="0" width="288" height="197"></rect>
    </a>
  </svg>
</div>

Firefox on desktop桌面上的 Firefox

iPhone 12 mini (iOS16) iPhone 12 迷你 (iOS16)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM