简体   繁体   English

将 svg 显示为来自外部 url 的图像

[英]Display svg as image from external url

I have a Firebase storage bucket with svg files in it.我有一个 Firebase 存储桶,里面有 svg 文件。 The urls for each file are stored in Firestore.每个文件的 url 都存储在 Firestore 中。 I am fetching the urls from firestore so I can display the svgs as images in html.我正在从 firestore 获取 url,因此我可以在 html 中将 svg 显示为图像。 I am using vanilla HTML, CSS, and JS and using UIKit for layout.我正在使用香草 HTML、CSS 和 JS 并使用 UIKit 进行布局。

What I mean by "display the svgs as images" is:我所说的“将 svgs 显示为图像”的意思是:

  • I want the user to be able to right click, and click "Save image as".我希望用户能够右键单击,然后单击“将图像另存为”。
  • I do NOT want my CSS to affect the svgs in any way.我不希望我的 CSS 以任何方式影响 svg。
  • I do not need to manipulate the SVGs programmatically.我不需要以编程方式操作 SVG。

I am using UIKit and have tried both of their documented methods of displaying svgs:我正在使用 UIKit 并尝试了他们记录的两种显示 svg 的方法:

Method 1: as an inline svg image方法一:作为内联 svg 镜像

var source = logo.imageURL;

var div = document.createElement("div");
var img = document.createElement("img");
img.width = 160;
img.src = source;
img.setAttribute("uk-svg", "");
div.appendChild(img)
parent.appendChild(div);

This results in the ability to see the svgs on the page, but:这导致能够查看页面上的 svg,但是:

  • You can't right click them to save them您不能右键单击它们来保存它们
  • UIKit's CSS is affecting the colors (all fills are gray). UIKit 的 CSS 正在影响 colors(所有填充均为灰色)。

Method 2: lazy loading using Image component方法二:使用 Image 组件延迟加载

var source = logo.imageURL;

var div = document.createElement("div");
var img = document.createElement("img");
img.width = 160;
img.src = source;
img.setAttribute("uk-img", "data-src:" + source);
img.setAttribute("uk-svg", "");
div.appendChild(img)
parent.appendChild(div);

This results in the svgs showing up as blank white squares.这导致 svg 显示为空白的白色方块。

Other tests I have done:我做过的其他测试:

I grabbed one of the svg files and added it to my frontend /images folder and hardcoded the source variable to use the local file.我抓取了一个 svg 文件并将其添加到我的前端/images文件夹中,并对source变量进行硬编码以使用本地文件。 The SVGs showed up fine, and met all my criteria above, so I assume there's nothing wrong with the file itself. SVG 显示得很好,并且符合我上面的所有标准,所以我认为文件本身没有问题。

Any help or advice with this would be great.对此的任何帮助或建议都会很棒。

With vanilla HTML5, if your want CSS isolation you need to place it in shadowDOM (supported in all modern browsers)使用 vanilla HTML5,如果您想要 CSS 隔离,您需要将其放置在shadowDOM中(所有现代浏览器都支持)

Either attach shadowDOM to your own <span> element;将 shadowDOM 附加到您自己的<span>元素;

Or take it one step further and create your own re-usable HTML Element <shadow-svg> (or any name you want)或者更进一步,创建您自己的可重复使用的 HTML 元素<shadow-svg> (或您想要的任何名称)

Note: the SVG being an IMG src should be enough to not be affected by global CSS;注意:作为 IMG src 的 SVG 应该足以不受全局 CSS 的影响; so shadowDOM not strictly required.所以 shadowDOM 不是严格要求的。 It ensures the IMG can not be styled by global CSS in this example.在此示例中,它确保 IMG 不能被全局 CSS 设置样式。

 <span id=SVG></span> <shadow-svg src="https://svgshare.com/i/U7z.svg"></shadow-svg> <shadow-svg src="https://svgshare.com/i/U7o.svg"></shadow-svg> <shadow-svg src="https://svgshare.com/i/U7p.svg"></shadow-svg> <style> #SVG { height: 130px; background: pink } </style> <script> SVG.attachShadow({ mode: "open" }).innerHTML = `<style>:host{display:inline-block}img{height:130px;width:130px}</style>` + `<img src="https://svgshare.com/i/U7L.svg">`; // OR customElements.define("shadow-svg", class extends HTMLElement { constructor() { super().attachShadow({ mode: "open" }).innerHTML = `<style>:host{display:inline-block}img{height:130px;width:130px}</style>` + `<img src="${this.getAttribute("src")}">`; } }); </script>

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

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