Javascript 如何在不通过 HTTP 加载图像的情况下在 HTML 中显示“重新加载”符号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13474472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:10:37  来源:igfitidea点击:

How can I display a 'Reload' symbol in HTML without loading an image via HTTP?

javascripthtmlcssimageunicode

提问by John Fowler

I would like to display a 'refresh' symbol in an HTML/JavaScript app I'm creating, but I do not want to make any HTTP requests to load an image. How can I do this reliably across all major browsers?

我想在我创建的 HTML/JavaScript 应用程序中显示“刷新”符号,但我不想发出任何 HTTP 请求来加载图像。我怎样才能在所有主要浏览器中可靠地做到这一点?

The closest Unicode value I could find is: ↺ (↺) But the arrow is pointing the wrong way.

我能找到的最接近的 Unicode 值是: ↺ (↺) 但是箭头指向错误的方向。

Is there another Unicode value that works?

是否有另一个有效的 Unicode 值?

Is there a Webdings font that works? (They don't usually work in Opera and Firefox.)

是否有有效的 Webdings 字体?(它们通常不适用于 Opera 和 Firefox。)

Is there a way to create an image in JS using a base64 image source provided by a String from the JavaScript?

有没有办法使用 JavaScript 中的字符串提供的 base64 图像源在 JS 中创建图像?

EDIT:

编辑:

The reason for this is NOT overhead. The app being created must be self-contained in a single javascript file, without any other dependencies. It is a development utility that can be added to any project, without the need for a network.

这样做的原因不是开销。正在创建的应用程序必须自包含在单个 javascript 文件中,没有任何其他依赖项。它是一种无需网络即可添加到任何项目的开发实用程序。

采纳答案by fardjad

If the browser supports loading image data from a Base64 string, you can use imgand set its srcto the following format:

如果浏览器支持从 Base64 字符串加载图像数据,您可以使用img并将其设置src为以下格式:

data:[<mediatype>][;base64],<data>

Example:

例子:

var img = document.getElementById("yourImage");
img.src = "data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub//ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcppV0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7";
<img id="yourImage" />

回答by Jukka K. Korpela

If you find the character &#8635;(?), i.e. U+21BB CLOCKWISE OPEN CIRCLE ARROW, acceptable for your needs (its arrow might not be placed the same way as in common reload icons), then you can use it rather safely:

如果您发现字符&#8635;(?),即 U+21BB CLOCKWISE OPEN CIRCLE ARROW,可以满足您的需要(它的箭头可能与常见的重新加载图标的放置方式不同),那么您可以相当安全地使用它:

.reload {
  font-family: Lucida Sans Unicode
}
<span class=reload>&#x21bb;</span>

The Lucida Sans Unicode font contains the symbol, and it is available in virtually any Windows system you can find these days. According to MS info, it was included even in Windows 98 (and Windows 95 plus).

Lucida Sans Unicode 字体包含该符号,现在几乎可以在任何 Windows 系统中找到它。根据MS 信息,它甚至包含在 Windows 98(和 Windows 95 plus)中。

Systems other than Windows can reasonably be expected to have the symbol in somefont and to have browsers that know how to pick it up from some of them. And fonts containing ithave rather similar shapes for it. (But if the symbol needs to match stylistically the text of your document, you need to study the situation in more detail and try to make sure that all fonts in your font list, or “font stack”, contain it.)

可以合理地期望 Windows 以外的系统具有某种字体的符号,并且浏览器知道如何从其中一些系统中提取它。而包含它的字体有它,而类似的形状。(但如果该符号需要在风格上与您的文档文本相匹配,您需要更详细地研究这种情况,并尝试确保您的字体列表或“字体堆栈”中的所有字体都包含它。)

回答by Basil Bourque

Unicode Character

Unicode 字符

There are at least two Unicode characters that might serve as Refreshor Reloadicons:

至少有两个 Unicode 字符可以用作刷新重新加载图标:

  • CLOCKWISE GAPPED CIRCLE ARROW
    U+27F3
    ?
  • CLOCKWISE OPEN CIRCLE ARROW
    U+21BB
    ?
  • 顺时针缺口圆形箭头
    U+27F3
  • 顺时针开口圆箭头
    U+21BB
    ?

The first does not appear in iOS 7 or 8 or 9 in either Safari or Chrome. So given that, and given the answer by Paul D. White, I recommend using the second one, CLOCKWISE OPEN CIRCLE ARROW.

第一个不会出现在 iOS 7 或 8 或 9 的 Safari 或 Chrome 中。因此,鉴于Paul D. White 的回答,我建议使用第二个CLOCKWISE OPEN CIRCLE ARROW

Tip: If you are a Mac user, obtain the free-of-cost app UnicodeChecker. You can search for characters by name, and you can see a list of fonts (on your particular Mac) containing a glyph for that character.

提示:如果您是 Mac 用户,请获取免费应用程序UnicodeChecker。您可以按名称搜索字符,并且可以看到包含该字符字形的字体列表(在您的特定 Mac 上)。

Icon Font

图标字体

Some fonts have been created with scalable vector-based icons in place of regular character glyphs.

某些字体是使用可缩放的基于矢量的图标代替常规字符字形创建的。

回答by Paul D. Waite

The clockwise equivalent to your Unicode character is &#8635;, ? (although, as David C. Bishopnotes in the comments, Chromebooks use #27f2, ?).

与您的 Unicode 字符等价的顺时针方向是&#8635;, ? (尽管,正如David C. Bishop在评论中指出的,Chromebook 使用#27f2, ?)。

Using a text character is an option, but Windows XP might not display the character (depending on the character you use, and the font you display it in).@Jukka K. Korpela notes that this character is included in the Lucida Sans Unicode font, so it should work fine even in Windows XP.

使用文本字符是一种选择,但 Windows XP 可能不会显示该字符(取决于您使用的字符以及显示它的字体)。@Jukka K. Korpela 指出,该字符包含在 Lucida Sans Unicode 字体中,因此即使在 Windows XP 中它也应该可以正常工作。

As far as Wingdings fonts go, to avoid an HTTP request you'd need to be confident that the font in question in going to be installed on end user's computers. I don't think there's a common cross-platform Wingdings font (I could be entirely wrong though).

就 Wingdings 字体而言,为了避免 HTTP 请求,您需要确信有问题的字体将安装在最终用户的计算机上。我认为没有通用的跨平台 Wingdings 字体(不过我可能完全错了)。

You can indeed create an image from a string that contains the base64-encoded representation of the image using JavaScript. You could do so like this:

您确实可以使用 JavaScript 从包含图像的 base64 编码表示的字符串创建图像。你可以这样做:

var image = new Image();
image.src = 'data:image/gif;base64,' + BASE64_ENCODED_STRING; // Replace gif with whatever image format it actually is

The data-uri might not work great in Internet Explorer 8 and earlier though, which Windows XP users may be stuck on. (And you don't actually need JavaScript for this: you can just put an <img>tag in your page with that srcattribute.)

但是,数据 uri 在 Internet Explorer 8 及更早版本中可能无法正常工作,Windows XP 用户可能会坚持使用。(您实际上并不需要为此使用 JavaScript:您只需<img>在您的页面中放置一个具有该src属性的标签即可。)

回答by Leia

Unicode 6 Character

Unicode 6 字符

'CLOCKWISE DOWNWARDS AND UPWARDS OPEN CIRCLE ARROWS' (U+1F503)

'顺时针向下和向上开圆箭头' (U+1F503)

Sadly font support for the glyph is not the best, so "all major browsers" might be a stretch... :-/

可悲的是,对字形的字体支持并不是最好的,所以“所有主要浏览器”可能是一个延伸...... :-/

回答by Alex

.refresh {
  display: inline-block;
  transform: rotate(90deg);
}
<span>?</span>
<hr>
<span class="refresh">?</span>

回答by David Hellsing

You mention "all major browsers", so let's assume that:

您提到了“所有主要浏览器”,让我们假设:

  1. not everyone have javascript
  2. not everyone can embed fonts (blackberry etc)
  3. not everyone supports all UNCODE representations (windows XP)
  4. not everyone supports inline data uri's (IE7-)
  1. 不是每个人都有 javascript
  2. 不是每个人都可以嵌入字体(黑莓等)
  3. 不是每个人都支持所有的 UNCODE 表示(Windows XP)
  4. 不是每个人都支持内联数据 uri (IE7-)

So what's left? I would suggest a HTML1-compliant IMGelement, that will also be cached after first load:

那么还剩下什么?我会建议一个符合 HTML1 的IMG元素,它也将在第一次加载后缓存:

<img src="/refresh.gif" alt="refresh">

It will work in every single browser I know of, except LYNX and other pure text-based browsers. But they will show "refresh" instead, so that's fine.

除了 LYNX 和其他纯文本浏览器之外,它可以在我所知道的每个浏览器中运行。但是他们会改为显示“刷新”,所以没关系。

If you really want to avoid that extra 2bytes request for "modern" browsers, you can use conditional comments for IE7-:

如果你真的想避免对“现代”浏览器的额外 2 字节请求,你可以使用 IE7 的条件注释:

<!--[if lte IE 7]>
<img src="refresh.gif" alt="refresh">
<![endif]-->

And something like this for the rest:

剩下的就是这样的:

<img src="data:image/gif;base64,..." alt="refresh">

If you're up to something new for the newest, you can use CSS box shadows (demo):

如果您想要最新的东西,您可以使用 CSS 框阴影(演示):

.refresh{
    border-radius: 0;
    display: inline-block;
    width: 1px;
    height: 1px;
    box-shadow: 0px 0px rgba(0,0,0,0),0px 1px rgba(0,0,0,0),0px 2px rgba(0,0,0,0),0px 3px rgba(0,0,0,0),0px 4px rgba(0,0,0,0),0px 5px rgba(0,0,0,0),0px 6px rgba(0,0,0,0),0px 7px rgba(0,0,0,0),0px 8px rgba(0,0,0,0),0px 9px rgba(0,0,0,0),0px 10px rgba(0,0,0,0),0px 11px rgba(0,0,0,0),0px 12px rgba(0,0,0,0),0px 13px rgba(0,0,0,0),0px 14px rgba(0,0,0,0),0px 15px rgba(0,0,0,0),1px 0px rgba(0,0,0,0),1px 1px rgba(0,0,0,0),1px 2px rgba(0,0,0,0),1px 3px rgba(0,0,0,0),1px 4px rgba(0,0,0,0),1px 5px rgba(0,0,0,0),1px 6px rgba(0,0,0,0),1px 7px rgba(0,0,0,0),1px 8px rgba(0,0,0,0),1px 9px rgba(0,0,0,0),1px 10px rgba(0,0,0,0),1px 11px rgba(0,0,0,0),1px 12px rgba(0,0,0,0),1px 13px rgba(0,0,0,0),1px 14px rgba(0,0,0,0),1px 15px rgba(0,0,0,0),2px 0px rgba(0,0,0,0),2px 1px rgba(0,0,0,0),2px 2px rgba(0,0,0,0),2px 3px rgba(0,0,0,0),2px 4px rgba(0,0,0,0),2px 5px rgba(0,0,0,0.06),2px 6px rgba(0,0,0,0.55),2px 7px rgba(0,0,0,0.79),2px 8px rgba(0,0,0,0.86),2px 9px rgba(0,0,0,0.86),2px 10px rgba(0,0,0,0.65),2px 11px rgba(0,0,0,0.31),2px 12px rgba(0,0,0,0),2px 13px rgba(0,0,0,0),2px 14px rgba(0,0,0,0),2px 15px rgba(0,0,0,0),3px 0px rgba(0,0,0,0),3px 1px rgba(0,0,0,0),3px 2px rgba(0,0,0,0),3px 3px rgba(0,0,0,0),3px 4px rgba(0,0,0,0.25),3px 5px rgba(0,0,0,0.83),3px 6px rgba(0,0,0,0.86),3px 7px rgba(0,0,0,0.83),3px 8px rgba(0,0,0,0.69),3px 9px rgba(0,0,0,0.74),3px 10px rgba(0,0,0,0.86),3px 11px rgba(0,0,0,0.86),3px 12px rgba(0,0,0,0.6),3px 13px rgba(0,0,0,0.06),3px 14px rgba(0,0,0,0),3px 15px rgba(0,0,0,0),4px 0px rgba(0,0,0,0),4px 1px rgba(0,0,0,0),4px 2px rgba(0,0,0,0),4px 3px rgba(0,0,0,0.25),4px 4px rgba(0,0,0,0.83),4px 5px rgba(0,0,0,0.83),4px 6px rgba(0,0,0,0.43),4px 7px rgba(0,0,0,0),4px 8px rgba(0,0,0,0),4px 9px rgba(0,0,0,0),4px 10px rgba(0,0,0,0.13),4px 11px rgba(0,0,0,0.69),4px 12px rgba(0,0,0,0.86),4px 13px rgba(0,0,0,0.6),4px 14px rgba(0,0,0,0),4px 15px rgba(0,0,0,0),5px 0px rgba(0,0,0,0),5px 1px rgba(0,0,0,0),5px 2px rgba(0,0,0,0),5px 3px rgba(0,0,0,0.69),5px 4px rgba(0,0,0,0.86),5px 5px rgba(0,0,0,0.25),5px 6px rgba(0,0,0,0),5px 7px rgba(0,0,0,0),5px 8px rgba(0,0,0,0),5px 9px rgba(0,0,0,0),5px 10px rgba(0,0,0,0),5px 11px rgba(0,0,0,0),5px 12px rgba(0,0,0,0.69),5px 13px rgba(0,0,0,0.86),5px 14px rgba(0,0,0,0.25),5px 15px rgba(0,0,0,0),6px 0px rgba(0,0,0,0),6px 1px rgba(0,0,0,0),6px 2px rgba(0,0,0,0.13),6px 3px rgba(0,0,0,0.86),6px 4px rgba(0,0,0,0.6),6px 5px rgba(0,0,0,0),6px 6px rgba(0,0,0,0),6px 7px rgba(0,0,0,0),6px 8px rgba(0,0,0,0),6px 9px rgba(0,0,0,0),6px 10px rgba(0,0,0,0),6px 11px rgba(0,0,0,0),6px 12px rgba(0,0,0,0.13),6px 13px rgba(0,0,0,0.86),6px 14px rgba(0,0,0,0.65),6px 15px rgba(0,0,0,0),7px 0px rgba(0,0,0,0),7px 1px rgba(0,0,0,0),7px 2px rgba(0,0,0,0.43),7px 3px rgba(0,0,0,0.86),7px 4px rgba(0,0,0,0.31),7px 5px rgba(0,0,0,0),7px 6px rgba(0,0,0,0),7px 7px rgba(0,0,0,0),7px 8px rgba(0,0,0,0),7px 9px rgba(0,0,0,0),7px 10px rgba(0,0,0,0),7px 11px rgba(0,0,0,0),7px 12px rgba(0,0,0,0),7px 13px rgba(0,0,0,0.75),7px 14px rgba(0,0,0,0.83),7px 15px rgba(0,0,0,0),8px 0px rgba(0,0,0,0),8px 1px rgba(0,0,0,0.43),8px 2px rgba(0,0,0,0.69),8px 3px rgba(0,0,0,0.86),8px 4px rgba(0,0,0,0.6),8px 5px rgba(0,0,0,0.31),8px 6px rgba(0,0,0,0),8px 7px rgba(0,0,0,0),8px 8px rgba(0,0,0,0),8px 9px rgba(0,0,0,0),8px 10px rgba(0,0,0,0),8px 11px rgba(0,0,0,0),8px 12px rgba(0,0,0,0),8px 13px rgba(0,0,0,0.69),8px 14px rgba(0,0,0,0.86),8px 15px rgba(0,0,0,0),9px 0px rgba(0,0,0,0),9px 1px rgba(0,0,0,0.48),9px 2px rgba(0,0,0,0.86),9px 3px rgba(0,0,0,0.86),9px 4px rgba(0,0,0,0.86),9px 5px rgba(0,0,0,0.13),9px 6px rgba(0,0,0,0),9px 7px rgba(0,0,0,0),9px 8px rgba(0,0,0,0),9px 9px rgba(0,0,0,0),9px 10px rgba(0,0,0,0),9px 11px rgba(0,0,0,0),9px 12px rgba(0,0,0,0),9px 13px rgba(0,0,0,0.75),9px 14px rgba(0,0,0,0.83),9px 15px rgba(0,0,0,0),10px 0px rgba(0,0,0,0),10px 1px rgba(0,0,0,0),10px 2px rgba(0,0,0,0.79),10px 3px rgba(0,0,0,0.86),10px 4px rgba(0,0,0,0.6),10px 5px rgba(0,0,0,0),10px 6px rgba(0,0,0,0),10px 7px rgba(0,0,0,0),10px 8px rgba(0,0,0,0),10px 9px rgba(0,0,0,0),10px 10px rgba(0,0,0,0),10px 11px rgba(0,0,0,0),10px 12px rgba(0,0,0,0.13),10px 13px rgba(0,0,0,0.86),10px 14px rgba(0,0,0,0.65),10px 15px rgba(0,0,0,0),11px 0px rgba(0,0,0,0),11px 1px rgba(0,0,0,0),11px 2px rgba(0,0,0,0.38),11px 3px rgba(0,0,0,0.83),11px 4px rgba(0,0,0,0.06),11px 5px rgba(0,0,0,0),11px 6px rgba(0,0,0,0),11px 7px rgba(0,0,0,0),11px 8px rgba(0,0,0,0),11px 9px rgba(0,0,0,0),11px 10px rgba(0,0,0,0),11px 11px rgba(0,0,0,0),11px 12px rgba(0,0,0,0.69),11px 13px rgba(0,0,0,0.86),11px 14px rgba(0,0,0,0.25),11px 15px rgba(0,0,0,0),12px 0px rgba(0,0,0,0),12px 1px rgba(0,0,0,0),12px 2px rgba(0,0,0,0),12px 3px rgba(0,0,0,0.31),12px 4px rgba(0,0,0,0),12px 5px rgba(0,0,0,0),12px 6px rgba(0,0,0,0.25),12px 7px rgba(0,0,0,0),12px 8px rgba(0,0,0,0),12px 9px rgba(0,0,0,0),12px 10px rgba(0,0,0,0.13),12px 11px rgba(0,0,0,0.69),12px 12px rgba(0,0,0,0.86),12px 13px rgba(0,0,0,0.6),12px 14px rgba(0,0,0,0),12px 15px rgba(0,0,0,0),13px 0px rgba(0,0,0,0),13px 1px rgba(0,0,0,0),13px 2px rgba(0,0,0,0),13px 3px rgba(0,0,0,0),13px 4px rgba(0,0,0,0),13px 5px rgba(0,0,0,0.06),13px 6px rgba(0,0,0,0.83),13px 7px rgba(0,0,0,0.83),13px 8px rgba(0,0,0,0.69),13px 9px rgba(0,0,0,0.75),13px 10px rgba(0,0,0,0.86),13px 11px rgba(0,0,0,0.86),13px 12px rgba(0,0,0,0.6),13px 13px rgba(0,0,0,0.06),13px 14px rgba(0,0,0,0),13px 15px rgba(0,0,0,0),14px 0px rgba(0,0,0,0),14px 1px rgba(0,0,0,0),14px 2px rgba(0,0,0,0),14px 3px rgba(0,0,0,0),14px 4px rgba(0,0,0,0),14px 5px rgba(0,0,0,0.13),14px 6px rgba(0,0,0,0.55),14px 7px rgba(0,0,0,0.79),14px 8px rgba(0,0,0,0.86),14px 9px rgba(0,0,0,0.86),14px 10px rgba(0,0,0,0.65),14px 11px rgba(0,0,0,0.31),14px 12px rgba(0,0,0,0),14px 13px rgba(0,0,0,0),14px 14px rgba(0,0,0,0),14px 15px rgba(0,0,0,0),15px 0px rgba(0,0,0,0),15px 1px rgba(0,0,0,0),15px 2px rgba(0,0,0,0),15px 3px rgba(0,0,0,0),15px 4px rgba(0,0,0,0),15px 5px rgba(0,0,0,0),15px 6px rgba(0,0,0,0),15px 7px rgba(0,0,0,0),15px 8px rgba(0,0,0,0),15px 9px rgba(0,0,0,0),15px 10px rgba(0,0,0,0),15px 11px rgba(0,0,0,0),15px 12px rgba(0,0,0,0),15px 13px rgba(0,0,0,0),15px 14px rgba(0,0,0,0),15px 15px rgba(0,0,0,0);
}