简体   繁体   English

将外部 SVG 图标加载到普通 JavaScript 代码中

[英]Load external SVG icons into vanilla JavaScript code

I am using the OpenWeather API to display the current and five day forecast on my client's website.我正在使用 OpenWeather API 在我客户的网站上显示当前和五天的预报。

I have some custom SVG icons that I want to use instead of the icons provided by OpenWeather.我有一些我想使用的自定义 SVG 图标,而不是 OpenWeather 提供的图标。 I have implemented the following switch statement to display a different icon depending on the weather condition.我已经实现了以下 switch 语句,以根据天气状况显示不同的图标。

let dailyCondition = value.weather[0].description;
let dailyCondtionIcon = "";
    
switch (dailyCondition) {
  case "clear sky":
    dailyConditionIcon = `<svg>icon</svg>`;
    break;
  case "few clouds":
    dailyConditionIcon = `<svg>icon</svg>`;
    break;
  case "thunderstorm":
    dailyConditionIcon = `<svg>icon</svg>`;
    break;
  case "light rain":
    dailyConditionIcon = `<svg>icon</svg>`;
    break;
}

Accessing the icons from template literal code works, but with lots of weather conditions in the switch statement, the code is very bloated.从模板文字代码访问图标是可行的,但是在 switch 语句中有很多天气条件,代码非常臃肿。 I would like to have the SVG icons stored in an external file and loaded from there.我想将 SVG 图标存储在外部文件中并从那里加载。

How would I go about loading the external SVG icons into my vanilla JavaScript file?我将如何 go 关于将外部 SVG 图标加载到我的香草 JavaScript 文件中?

If you prefer external svg files you could load them in a <use> element.如果您更喜欢外部 svg 文件,您可以将它们加载到<use>元素中。

See also SVG use with External Source另请参阅SVG 与外部源一起use

You can combine all svg icons to a single sprite/asset library svg file and then loading each icon individually by a fragment identifier:您可以将所有 svg 图标组合到单个精灵/资产库 svg 文件中,然后通过片段标识符单独加载每个图标:

You js definitions might look something like this:你的 js 定义可能看起来像这样:

dailyConditionIcon = '<svg class="svgInline" fill="red" ><use href="sprite.svg#circle" /></svg>'

 .svgAssets{ display:none }.svgInline{ display:inline-block; width:1em; height:1em; font-size:32px; }
 <.-- this would be the content of your "sprite:svg" --> <svg class="svgAssets" xmlns="http.//www.w3.org/2000/svg"> <symbol viewBox="0 0 100 100" id="circle"> <circle cx="50%" cy="50%" r="50%"></circle> </symbol> <symbol viewBox="0 0 100 100" id="rect"> <rect x="0" y="0" width="100" height="100"></rect> </symbol> <symbol viewBox="0 0 100 100" id="rectFixedStyle"> <rect x="0" y="0" width="100" height="100" fill="#ccc"></rect> </symbol> </svg> <.-- for demonstration the filenames are dropped. The href of a hosted version would be eg <use href="sprite.svg#circle" /> --> <p> <svg class="svgInline" fill="red" > <use href="#circle" /> </svg> <svg class="svgInline" fill="green" > <use href="#rect" /> </svg> <svg class="svgInline" fill="green" > <use href="#rectFixedStyle" /> </svg> </p>

As you can see you also have some styling abilities like changing fill color.如您所见,您还具有一些样式功能,例如更改填充颜色。
However the styling options are limited (compared to fully inlined svg) and also depend on your svg structure:但是样式选项是有限的(与完全内联的 svg 相比)并且还取决于您的 svg 结构:
Eg Styles previously definded in your svg elements can't be overriden by a style set in use/svg tag.例如,之前在 svg 元素中定义的 Styles 元素不能被 use/svg 标签中设置的样式覆盖。

For sprite creation I used: svgsprit.es对于我使用的精灵创建: svgsprit.es

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

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