{"id":3467,"date":"2024-11-10T16:25:07","date_gmt":"2024-11-10T08:25:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3467\/"},"modified":"2024-11-10T16:25:07","modified_gmt":"2024-11-10T08:25:07","slug":"js-%e5%a6%82%e4%bd%95%e7%bb%91%e5%ae%9a%e5%ad%97%e4%bd%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3467\/","title":{"rendered":"js \u5982\u4f55\u7ed1\u5b9a\u5b57\u4f53"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\u7ed1\u5b9a\u5b57\u4f53\u53ef\u4ee5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u5b57\u4f53\u5728 web \u9875\u9762\u4e0a\u7684\u4f7f\u7528\uff0c\u6b65\u9aa4\u5982\u4e0b\uff1a\u83b7\u53d6\u5b57\u4f53\u6587\u4ef6\uff08woff\u3001woff2\u3001otf\u3001ttf\uff09\u521b\u5efa\u5b57\u4f53\u6837\u5f0f\u5bf9\u8c61\uff08fontface \u63a5\u53e3\uff09\u5c06\u5b57\u4f53\u6837\u5f0f\u5bf9\u8c61\u6dfb\u52a0\u5230\u6587\u6863\u5b57\u4f53\u96c6\u5408\u7b49\u5f85\u5b57\u4f53\u52a0\u8f7d\u5b8c\u6210\uff08fontface.load() \u65b9\u6cd5\uff09\u4e3a html \u5143\u7d20\u6307\u5b9a\u81ea\u5b9a\u4e49\u5b57\u4f53\uff08font-family css \u5c5e\u6027\uff09\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110812575644741.jpg\" class=\"aligncenter\" title=\"js \u5982\u4f55\u7ed1\u5b9a\u5b57\u4f53\u63d2\u56fe\" alt=\"js \u5982\u4f55\u7ed1\u5b9a\u5b57\u4f53\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u7ed1\u5b9a\u5b57\u4f53<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u52a8\u6001\u5730\u7ed1\u5b9a\u5b57\u4f53\uff0c\u4ee5\u4fbf\u5728 web \u9875\u9762\u4e0a\u4f7f\u7528\u81ea\u5b9a\u4e49\u5b57\u4f53\u3002\u8fd9\u4e3a\u9875\u9762\u8bbe\u8ba1\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u521b\u5efa\u72ec\u7279\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<p><strong>\u6b65\u9aa4<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u5728 JavaScript \u4e2d\u7ed1\u5b9a\u5b57\u4f53\u7684\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u83b7\u53d6\u5b57\u4f53\u6587\u4ef6<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u83b7\u53d6\u7528\u4e8e\u7ed1\u5b9a\u7684\u5b57\u4f53\u6587\u4ef6\u3002\u5b57\u4f53\u6587\u4ef6\u53ef\u4ee5\u662f\u4ee5\u4e0b\u683c\u5f0f\uff1a<\/p>\n<ul>\n<li><strong>Web Open Font Format (WOFF)<\/strong><\/li>\n<li><strong>Web Open Font Format 2 (WOFF2)<\/strong><\/li>\n<li><strong>OpenType Font File (OTF)<\/strong><\/li>\n<li><strong>TrueType Font File (TTF)<\/strong><\/li>\n<\/ul>\n<p><strong>2. \u521b\u5efa\u5b57\u4f53\u6837\u5f0f\u5bf9\u8c61<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528 FontFace \u63a5\u53e3\u521b\u5efa\u4e00\u4e2a\u5b57\u4f53\u6837\u5f0f\u5bf9\u8c61\u3002\u8be5\u5bf9\u8c61\u5b9a\u4e49\u4e86\u5b57\u4f53\u540d\u79f0\u3001\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u548c\u5b57\u4f53\u6837\u5f0f\u3002<\/p>\n<pre>const fontFace = new FontFace('MyCustomFont', 'url(myfont.woff2)');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u6dfb\u52a0\u5b57\u4f53\u6837\u5f0f<\/strong><\/p>\n<p>\u5c06 FontFace \u5bf9\u8c61\u6dfb\u52a0\u5230 Document \u5bf9\u8c61\u7684 fonts \u96c6\u5408\u4e2d\uff0c\u4ee5\u4fbf\u6d4f\u89c8\u5668\u53ef\u4ee5\u52a0\u8f7d\u5b57\u4f53\u6587\u4ef6\u3002<\/p>\n<pre>document.fonts.add(fontFace);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u7b49\u5f85\u5b57\u4f53\u52a0\u8f7d<\/strong><\/p>\n<p>\u5b57\u4f53\u52a0\u8f7d\u662f\u4e00\u4e2a\u5f02\u6b65\u8fc7\u7a0b\u3002\u4f7f\u7528 FontFace.load() \u65b9\u6cd5\u7b49\u5f85\u5b57\u4f53\u52a0\u8f7d\u5b8c\u6210\u3002<\/p>\n<pre>fontFace.load().then(() =&gt; {\n  \/\/ \u5b57\u4f53\u5df2\u52a0\u8f7d\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>5. \u4f7f\u7528\u5b57\u4f53<\/strong><\/p>\n<p>\u5b57\u4f53\u52a0\u8f7d\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 font-family CSS \u5c5e\u6027\u4e3a HTML \u5143\u7d20\u6307\u5b9a\u81ea\u5b9a\u4e49\u5b57\u4f53\u3002<\/p>\n<pre>&lt;p style=\"font-family: 'MyCustomFont';\"&gt;\u8fd9\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5b57\u4f53\u6bb5\u843d&lt;\/p&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5176\u4ed6\u9009\u9879<\/strong><\/p>\n<p>\u9664\u4e86\u4f7f\u7528 FontFace \u63a5\u53e3\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u9009\u9879\u5728 JavaScript \u4e2d\u7ed1\u5b9a\u5b57\u4f53\uff1a<\/p>\n<ul>\n<li> <strong>CSS @font-face \u89c4\u5219<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528 CSS @font-face \u89c4\u5219\u76f4\u63a5\u5728 CSS \u6587\u4ef6\u4e2d\u7ed1\u5b9a\u5b57\u4f53\u3002<\/li>\n<li> <strong>Web \u5b57\u4f53\u670d\u52a1<\/strong>\uff1a\u6709\u8bb8\u591a web \u5b57\u4f53\u670d\u52a1\u63d0\u4f9b\u5728\u7ebf\u6258\u7ba1\u548c\u5206\u53d1\u81ea\u5b9a\u4e49\u5b57\u4f53\u3002<\/li>\n<\/ul>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\u7ed1\u5b9a\u5b57\u4f53\u4e3a web \u5f00\u53d1\u4eba\u5458\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u521b\u5efa\u89c6\u89c9\u6548\u679c\u72ec\u7279\u4e14\u5438\u5f15\u4eba\u7684\u9875\u9762\u3002\u901a\u8fc7\u9075\u5faa\u4e0a\u9762\u7684\u6b65\u9aa4\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u81ea\u5b9a\u4e49\u5b57\u4f53\u96c6\u6210\u5230 web \u9879\u76ee\u4e2d\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs \u5982\u4f55\u7ed1\u5b9a\u5b57\u4f53\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 javascript \u4e2d\u7ed1\u5b9a\u5b57\u4f53\u53ef\u4ee5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u5b57\u4f53\u5728 web \u9875\u9762\u4e0a\u7684\u4f7f\u7528\uff0c\u6b65\u9aa4\u5982\u4e0b\uff1a\u83b7\u53d6\u5b57\u4f53\u6587\u4ef6\uff08woff\u3001woff2\u3001otf\u3001ttf\uff09\u521b\u5efa\u5b57\u4f53\u6837\u5f0f\u5bf9\u8c61\uff08fontface \u63a5\u53e3\uff09\u5c06\u5b57\u4f53\u6837\u5f0f\u5bf9\u8c61\u6dfb\u52a0\u5230\u6587\u6863\u5b57\u4f53\u96c6\u5408\u7b49\u5f85\u5b57\u4f53\u52a0\u8f7d\u5b8c\u6210\uff08fontface.load() \u65b9\u6cd5\uff09\u4e3a html \u5143\u7d20\u6307\u5b9a\u81ea\u5b9a\u4e49\u5b57\u4f53\uff08font-family css \u5c5e\u6027\uff09 \u5982\u4f55\u5728 JavaScript \u4e2d\u7ed1\u5b9a\u5b57\u4f53 \u7b80\u4ecb \u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u52a8\u6001\u5730\u7ed1\u5b9a\u5b57\u4f53\uff0c\u4ee5\u4fbf\u5728 web \u9875\u9762\u4e0a\u4f7f\u7528\u81ea\u5b9a\u4e49\u5b57\u4f53\u3002\u8fd9\u4e3a\u9875\u9762\u8bbe\u8ba1\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u521b\u5efa\u72ec\u7279\u7684\u89c6\u89c9\u6548\u679c\u3002 \u6b65\u9aa4 \u4ee5\u4e0b\u662f\u5728 JavaScript \u4e2d\u7ed1\u5b9a\u5b57\u4f53\u7684\u6b65\u9aa4\uff1a 1. \u83b7\u53d6\u5b57\u4f53\u6587\u4ef6 \u9996\u5148\uff0c\u9700\u8981\u83b7\u53d6\u7528\u4e8e\u7ed1\u5b9a\u7684\u5b57\u4f53\u6587\u4ef6\u3002\u5b57\u4f53\u6587\u4ef6\u53ef\u4ee5\u662f\u4ee5\u4e0b\u683c\u5f0f\uff1a Web Open Font Format (WOFF) Web Open Font Format 2 (WOFF2) OpenType Font File (OTF) TrueType Font File (TTF) 2. \u521b\u5efa\u5b57\u4f53\u6837\u5f0f\u5bf9\u8c61 \u63a5\u4e0b\u6765\uff0c\u4f7f\u7528 FontFace \u63a5\u53e3\u521b\u5efa\u4e00\u4e2a\u5b57\u4f53\u6837\u5f0f\u5bf9\u8c61\u3002\u8be5\u5bf9\u8c61\u5b9a\u4e49\u4e86\u5b57\u4f53\u540d\u79f0\u3001\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u548c\u5b57\u4f53\u6837\u5f0f\u3002 const fontFace = new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3467","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3467"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3467\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}