fix-broken-img:零依赖的图片优雅降级解决方案
<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="margin: 0; padding: 0 10px; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; font-family: Optima, "Microsoft YaHei", PingFangSC-regular, serif; font-size: 16px; color: rgba(0, 0, 0, 1); line-height: 1.5em; word-spacing: 0; letter-spacing: 0; overflow-wrap: break-word; text-align: left"><h1 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; display: block"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 24px; color: rgba(0, 0, 0, 1); line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; display: block">fix-broken-img:零依赖的图片优雅降级解决方案</span><span class="suffix" style="display: none"></span></h1><blockquote class="custom-blockquote multiquote-1" data-tool="mdnice编辑器" style="margin: 20px 0; padding: 10px 10px 10px 20px; border-top: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px solid rgba(239, 112, 96, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-radius: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(255, 249, 249, 1); width: auto; height: auto; box-shadow: 0 0 rgba(0, 0, 0, 0); display: block; overflow-x: auto; overflow-y: auto"><span style="display: none; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: normal"></span>
<p style="text-indent: 0; padding: 8px 0; color: rgba(0, 0, 0, 1); font-size: 15px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal; margin: 0">在现代 Web 开发中,图片加载失败是一个常见但容易被忽视的问题。今天我要介绍一个我最近开源的解决方案——<code style="color: rgba(239, 112, 96, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; border-top: 3px none rgba(0, 0, 0, 1); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); border-right: 3px none rgba(0, 0, 0, 0.4); overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace; word-break: break-all">fix-broken-img</code>,一个零依赖的 WebComponents 组件,专门用于处理图片加载失败时的优雅降级。</p>
</blockquote>
<h2 data-tool="mdnice编辑器" style="border-bottom: 2px solid rgba(239, 112, 96, 1); margin: 30px 0 15px; padding: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box unset; border-top: 1px none rgba(0, 0, 0, 1); border-left: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.1em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 22px; color: rgba(255, 255, 255, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(239, 112, 96, 1); line-height: 1.5em; letter-spacing: 0; align-items: unset; border: 1px none rgba(0, 0, 0, 1); border-radius: 3px 3px 0 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; margin: 0 5px 0 0; overflow-x: unset; overflow-y: unset; padding: 3px 10px 1px; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset">问题背景:为什么需要图片降级?</span><span class="suffix" style="display: none"></span><span style="border-bottom: 36px solid rgba(239, 235, 233, 1); align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box unset; border-top: 1px none rgba(0, 0, 0, 1); border-left: 1px none rgba(0, 0, 0, 1); border-right: 20px solid rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(0, 0, 0, 1); display: inline-block; font-size: 16px; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; letter-spacing: 0; line-height: 1.1em; margin: 0; overflow-x: unset; overflow-y: unset; padding: 0; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset"> </span></h2>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0">在 Web 开发中,我们经常会遇到以下场景:</p>
<ol data-tool="mdnice编辑器" style="list-style-type: decimal; margin: 8px 0; padding: 0 0 0 25px; color: rgba(0, 0, 0, 1)">
<li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">CDN 图片失效</strong>:第三方图片服务不可用</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">用户上传图片损坏</strong>:用户上传的图片文件损坏或格式不支持</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">网络问题</strong>:图片加载超时或网络中断</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">动态内容</strong>:动态生成的图片链接可能失效</section></li></ol>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0">传统的解决方案通常是在 <code style="color: rgba(239, 112, 96, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; border-top: 3px none rgba(0, 0, 0, 1); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); border-right: 3px none rgba(0, 0, 0, 0.4); overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace; word-break: break-all">img</code> 标签的 <code style="color: rgba(239, 112, 96, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; border-top: 3px none rgba(0, 0, 0, 1); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); border-right: 3px none rgba(0, 0, 0, 0.4); overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace; word-break: break-all">onerror</code> 事件中处理,但这种方式存在几个问题:</p>
<ul data-tool="mdnice编辑器" style="list-style-type: disc; margin: 8px 0; padding: 0 0 0 25px; color: rgba(0, 0, 0, 1)">
<li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">代码重复</strong>:每个图片都需要单独处理</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">样式不一致</strong>:降级样式难以统一</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">维护困难</strong>:分散在各处的降级逻辑难以维护</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">用户体验差</strong>:浏览器默认的破损图标影响美观</section></li></ul>
<h2 data-tool="mdnice编辑器" style="border-bottom: 2px solid rgba(239, 112, 96, 1); margin: 30px 0 15px; padding: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box unset; border-top: 1px none rgba(0, 0, 0, 1); border-left: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.1em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 22px; color: rgba(255, 255, 255, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(239, 112, 96, 1); line-height: 1.5em; letter-spacing: 0; align-items: unset; border: 1px none rgba(0, 0, 0, 1); border-radius: 3px 3px 0 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; margin: 0 5px 0 0; overflow-x: unset; overflow-y: unset; padding: 3px 10px 1px; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset">解决方案:fix-broken-img</span><span class="suffix" style="display: none"></span><span style="border-bottom: 36px solid rgba(239, 235, 233, 1); align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box unset; border-top: 1px none rgba(0, 0, 0, 1); border-left: 1px none rgba(0, 0, 0, 1); border-right: 20px solid rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(0, 0, 0, 1); display: inline-block; font-size: 16px; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; letter-spacing: 0; line-height: 1.1em; margin: 0; overflow-x: unset; overflow-y: unset; padding: 0; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset"> </span></h2>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0"><code style="color: rgba(239, 112, 96, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; border-top: 3px none rgba(0, 0, 0, 1); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); border-right: 3px none rgba(0, 0, 0, 0.4); overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace; word-break: break-all">fix-broken-img</code> 是一个基于原生 WebComponents 的解决方案,具有以下核心特性:</p>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; display: block"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 20px; color: rgba(0, 0, 0, 1); line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; display: block">🚀 零依赖设计</span><span class="suffix" style="display: none"></span></h3>
<pre class="custom" data-tool="mdnice编辑器" style="border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55); text-align: left; margin: 10px 0; padding: 0"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); background: rgba(40, 44, 52, 1); border-radius: 5px; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px"><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px">// 纯原生实现,无需任何外部库</span><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(230, 192, 123, 1); line-height: 26px">FixBrokenImg</span> <span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">extends</span> <span class="hljs-title" style="color: rgba(230, 192, 123, 1); line-height: 26px">HTMLElement</span> </span>{<br> <span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">constructor</span>() {<br> <span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">super</span>();<br> <span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">this</span>.attachShadow({ <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">mode</span>: <span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">'open'</span> });<br> <span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">this</span>._init();<br> }<br> <span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px">// ... 完整实现</span><br>}<br></code></pre>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; display: block"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 20px; color: rgba(0, 0, 0, 1); line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; display: block">⚡ 一行代码自动生效</span><span class="suffix" style="display: none"></span></h3>
<pre class="custom" data-tool="mdnice编辑器" style="border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55); text-align: left; margin: 10px 0; padding: 0"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); background: rgba(40, 44, 52, 1); border-radius: 5px; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px"><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px"><!-- 只需引入脚本,无需额外配置 --></span><br><span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">script</span> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">src</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"https://cdn.jsdelivr.net/gh/mrhuo/fix-broken-img@latest/src/fix-broken-img.min.js"</span> <br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">id</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"fix-broken-img"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">data-background</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"#e8f5e8"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">data-text-color</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"#2e7d32"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">data-default-text</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"🌱 图片加载中..."</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">data-auto-convert</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"true"</span>></span><br><span class="hljs-tag" style="line-height: 26px"></<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">script</span>></span><br></code></pre>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; display: block"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 20px; color: rgba(0, 0, 0, 1); line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; display: block">🎨 高度可定制</span><span class="suffix" style="display: none"></span></h3>
<pre class="custom" data-tool="mdnice编辑器" style="border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55); text-align: left; margin: 10px 0; padding: 0"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); background: rgba(40, 44, 52, 1); border-radius: 5px; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px"><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px"><!-- 手动使用时的自定义配置 --></span><br><span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">fix-broken-img</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">src</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"path/to/image.jpg"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">alt</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"产品展示图片"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">background-color</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"#ffebee"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">text-color</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"#c62828"</span>></span><br><span class="hljs-tag" style="line-height: 26px"></<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">fix-broken-img</span>></span><br></code></pre>
<h2 data-tool="mdnice编辑器" style="border-bottom: 2px solid rgba(239, 112, 96, 1); margin: 30px 0 15px; padding: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box unset; border-top: 1px none rgba(0, 0, 0, 1); border-left: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.1em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 22px; color: rgba(255, 255, 255, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(239, 112, 96, 1); line-height: 1.5em; letter-spacing: 0; align-items: unset; border: 1px none rgba(0, 0, 0, 1); border-radius: 3px 3px 0 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; margin: 0 5px 0 0; overflow-x: unset; overflow-y: unset; padding: 3px 10px 1px; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset">技术实现深度解析</span><span class="suffix" style="display: none"></span><span style="border-bottom: 36px solid rgba(239, 235, 233, 1); align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box unset; border-top: 1px none rgba(0, 0, 0, 1); border-left: 1px none rgba(0, 0, 0, 1); border-right: 20px solid rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(0, 0, 0, 1); display: inline-block; font-size: 16px; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; letter-spacing: 0; line-height: 1.1em; margin: 0; overflow-x: unset; overflow-y: unset; padding: 0; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset"> </span></h2>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; display: block"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 20px; color: rgba(0, 0, 0, 1); line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; display: block">1. WebComponents 架构设计</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0"><code style="color: rgba(239, 112, 96, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; border-top: 3px none rgba(0, 0, 0, 1); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); border-right: 3px none rgba(0, 0, 0, 0.4); overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace; word-break: break-all">fix-broken-img</code> 采用标准的 WebComponents 架构:</p>
<pre class="custom" data-tool="mdnice编辑器" style="border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55); text-align: left; margin: 10px 0; padding: 0"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); background: rgba(40, 44, 52, 1); border-radius: 5px; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px"><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px">// 自定义元素定义</span><br>customElements.define(<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">'fix-broken-img'</span>, FixBrokenImg);<br><br><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px">// Shadow DOM 实现样式隔离</span><br><span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">this</span>.attachShadow({ <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">mode</span>: <span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">'open'</span> });<br><span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">this</span>.shadowRoot.innerHTML = <span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">`<br> <style><br> /* 隔离的样式,不会影响外部页面 */<br> .fallback {<br> background-color: <span class="hljs-subst" style="color: rgba(224, 108, 117, 1); line-height: 26px">${backgroundColor}</span>;<br> color: <span class="hljs-subst" style="color: rgba(224, 108, 117, 1); line-height: 26px">${textColor}</span>;<br> }<br> </style><br> <div class="image-container"><br> <img class="image" src="" alt=""><br> <div class="loading"></div><br> <div class="fallback"><span class="hljs-subst" style="color: rgba(224, 108, 117, 1); line-height: 26px">${altText}</span></div><br> </div><br>`</span>;<br></code></pre>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; display: block"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 20px; color: rgba(0, 0, 0, 1); line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; display: block">2. 智能自动转换机制</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0">组件通过 <code style="color: rgba(239, 112, 96, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; border-top: 3px none rgba(0, 0, 0, 1); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); border-right: 3px none rgba(0, 0, 0, 0.4); overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace; word-break: break-all">MutationObserver</code> 监听 DOM 变化,自动处理动态添加的图片:</p>
<pre class="custom" data-tool="mdnice编辑器" style="border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55); text-align: left; margin: 10px 0; padding: 0"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); background: rgba(40, 44, 52, 1); border-radius: 5px; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px"><span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">const</span> observer = <span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">new</span> MutationObserver(<span class="hljs-function" style="line-height: 26px">(<span class="hljs-params" style="line-height: 26px">mutations</span>) =></span> {<br> mutations.forEach(<span class="hljs-function" style="line-height: 26px">(<span class="hljs-params" style="line-height: 26px">mutation</span>) =></span> {<br> mutation.addedNodes.forEach(<span class="hljs-function" style="line-height: 26px">(<span class="hljs-params" style="line-height: 26px">node</span>) =></span> {<br> <span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">if</span> (node.nodeType === Node.ELEMENT_NODE) {<br> <span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">const</span> images = node.querySelectorAll(<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">'img'</span>);<br> images.forEach(<span class="hljs-function" style="line-height: 26px"><span class="hljs-params" style="line-height: 26px">img</span> =></span> {<br> <span class="hljs-keyword" style="color: rgba(198, 120, 221, 1); line-height: 26px">if</span> (!img.closest(<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">'fix-broken-img'</span>)) {<br> _convertToFixBrokenImg(img, background, textColor);<br> }<br> });<br> }<br> });<br> });<br>});<br></code></pre>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; display: block"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 20px; color: rgba(0, 0, 0, 1); line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; display: block">3. 性能优化策略</span><span class="suffix" style="display: none"></span></h3>
<ul data-tool="mdnice编辑器" style="list-style-type: disc; margin: 8px 0; padding: 0 0 0 25px; color: rgba(0, 0, 0, 1)">
<li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">懒加载</strong>:只在需要时创建组件实例</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">事件委托</strong>:统一的事件监听机制</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">内存管理</strong>:自动清理无用的监听器</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">CSS 优化</strong>:使用硬件加速的动画效果</section></li></ul>
<h2 data-tool="mdnice编辑器" style="border-bottom: 2px solid rgba(239, 112, 96, 1); margin: 30px 0 15px; padding: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box unset; border-top: 1px none rgba(0, 0, 0, 1); border-left: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.1em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 22px; color: rgba(255, 255, 255, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(239, 112, 96, 1); line-height: 1.5em; letter-spacing: 0; align-items: unset; border: 1px none rgba(0, 0, 0, 1); border-radius: 3px 3px 0 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; margin: 0 5px 0 0; overflow-x: unset; overflow-y: unset; padding: 3px 10px 1px; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset">实际应用案例</span><span class="suffix" style="display: none"></span><span style="border-bottom: 36px solid rgba(239, 235, 233, 1); align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box unset; border-top: 1px none rgba(0, 0, 0, 1); border-left: 1px none rgba(0, 0, 0, 1); border-right: 20px solid rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(0, 0, 0, 1); display: inline-block; font-size: 16px; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; letter-spacing: 0; line-height: 1.1em; margin: 0; overflow-x: unset; overflow-y: unset; padding: 0; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset"> </span></h2>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; display: block"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 20px; color: rgba(0, 0, 0, 1); line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; display: block">案例一:电商网站商品图片</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">问题</strong>:商品图片可能因为 CDN 问题加载失败,影响用户体验和转化率。</p>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">解决方案</strong>:</p>
<pre class="custom" data-tool="mdnice编辑器" style="border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55); text-align: left; margin: 10px 0; padding: 0"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); background: rgba(40, 44, 52, 1); border-radius: 5px; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px"><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px"><!-- 商品列表页 --></span><br><span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">div</span> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">class</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"product-grid"</span>></span><br> <span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">div</span> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">class</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"product-item"</span>></span><br> <span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">fix-broken-img</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">src</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"https://cdn.example.com/products/123.jpg"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">alt</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"商品名称"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">background-color</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"#f8f9fa"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">text-color</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"#6c757d"</span>></span><br> <span class="hljs-tag" style="line-height: 26px"></<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">fix-broken-img</span>></span><br> <span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">h3</span>></span>商品名称<span class="hljs-tag" style="line-height: 26px"></<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">h3</span>></span><br> <span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">p</span>></span>¥199.00<span class="hljs-tag" style="line-height: 26px"></<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">p</span>></span><br> <span class="hljs-tag" style="line-height: 26px"></<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">div</span>></span><br> <span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px"><!-- 更多商品 --></span><br><span class="hljs-tag" style="line-height: 26px"></<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">div</span>></span><br></code></pre>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">效果</strong>:当图片加载失败时,显示统一的降级界面,而不是浏览器默认的破损图标。</p>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; display: block"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 20px; color: rgba(0, 0, 0, 1); line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; display: block">案例二:用户头像管理</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">问题</strong>:用户上传的头像可能因为各种原因无法显示。</p>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">解决方案</strong>:</p>
<pre class="custom" data-tool="mdnice编辑器" style="border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55); text-align: left; margin: 10px 0; padding: 0"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); background: rgba(40, 44, 52, 1); border-radius: 5px; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px"><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px"><!-- 用户资料页 --></span><br><span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">div</span> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">class</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"user-profile"</span>></span><br> <span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">fix-broken-img</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">src</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"/avatars/user-123.jpg"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">alt</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"用户头像"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">background-color</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"#e3f2fd"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">text-color</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"#1976d2"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">style</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"width: 120px; height: 120px; border-radius: 50%;"</span>></span><br> <span class="hljs-tag" style="line-height: 26px"></<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">fix-broken-img</span>></span><br> <span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">h2</span>></span>用户名<span class="hljs-tag" style="line-height: 26px"></<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">h2</span>></span><br><span class="hljs-tag" style="line-height: 26px"></<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">div</span>></span><br></code></pre>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">效果</strong>:头像加载失败时显示优雅的占位符,保持页面美观。</p>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; display: block"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 20px; color: rgba(0, 0, 0, 1); line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; display: block">案例三:新闻媒体网站</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">问题</strong>:新闻图片可能因为版权问题或链接失效无法显示。</p>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">解决方案</strong>:</p>
<pre class="custom" data-tool="mdnice编辑器" style="border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55); text-align: left; margin: 10px 0; padding: 0"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); background: rgba(40, 44, 52, 1); border-radius: 5px; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px"><span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px"><!-- 新闻详情页 --></span><br><span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">article</span> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">class</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"news-article"</span>></span><br> <span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">h1</span>></span>新闻标题<span class="hljs-tag" style="line-height: 26px"></<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">h1</span>></span><br> <span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">fix-broken-img</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">src</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"https://news-cdn.example.com/2025/11/image.jpg"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">alt</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"新闻配图"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">background-color</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"#fff3e0"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">text-color</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"#f57c00"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">style</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"width: 100%; max-width: 800px;"</span>></span><br> <span class="hljs-tag" style="line-height: 26px"></<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">fix-broken-img</span>></span><br> <span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">div</span> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">class</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"content"</span>></span><br> <span class="hljs-comment" style="color: rgba(92, 99, 112, 1); font-style: italic; line-height: 26px"><!-- 新闻内容 --></span><br> <span class="hljs-tag" style="line-height: 26px"></<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">div</span>></span><br><span class="hljs-tag" style="line-height: 26px"></<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">article</span>></span><br></code></pre>
<h2 data-tool="mdnice编辑器" style="border-bottom: 2px solid rgba(239, 112, 96, 1); margin: 30px 0 15px; padding: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box unset; border-top: 1px none rgba(0, 0, 0, 1); border-left: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.1em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 22px; color: rgba(255, 255, 255, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(239, 112, 96, 1); line-height: 1.5em; letter-spacing: 0; align-items: unset; border: 1px none rgba(0, 0, 0, 1); border-radius: 3px 3px 0 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; margin: 0 5px 0 0; overflow-x: unset; overflow-y: unset; padding: 3px 10px 1px; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset">性能对比测试</span><span class="suffix" style="display: none"></span><span style="border-bottom: 36px solid rgba(239, 235, 233, 1); align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box unset; border-top: 1px none rgba(0, 0, 0, 1); border-left: 1px none rgba(0, 0, 0, 1); border-right: 20px solid rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(0, 0, 0, 1); display: inline-block; font-size: 16px; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; letter-spacing: 0; line-height: 1.1em; margin: 0; overflow-x: unset; overflow-y: unset; padding: 0; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset"> </span></h2>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0">我们在不同场景下对 <code style="color: rgba(239, 112, 96, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; border-top: 3px none rgba(0, 0, 0, 1); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); border-right: 3px none rgba(0, 0, 0, 0.4); overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace; word-break: break-all">fix-broken-img</code> 进行了性能测试:</p>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; display: block"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 20px; color: rgba(0, 0, 0, 1); line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; display: block">测试环境</span><span class="suffix" style="display: none"></span></h3>
<ul data-tool="mdnice编辑器" style="list-style-type: disc; margin: 8px 0; padding: 0 0 0 25px; color: rgba(0, 0, 0, 1)">
<li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal">Chrome 142.0.0.0</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal">100 个图片同时加载</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal">网络环境:4G 模拟</section></li></ul>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; display: block"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 20px; color: rgba(0, 0, 0, 1); line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; display: block">测试结果</span><span class="suffix" style="display: none"></span></h3>
<section class="table-container" data-tool="mdnice编辑器" style="margin: 0; padding: 0; overflow-x: auto"><table style="display: table; text-align: left">
<thead>
<tr>
<th style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(240, 240, 240, 1); width: auto; height: auto; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0; padding: 5px 10px; min-width: 85px">场景</th>
<th style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(240, 240, 240, 1); width: auto; height: auto; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0; padding: 5px 10px; min-width: 85px">传统方案</th>
<th style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(240, 240, 240, 1); width: auto; height: auto; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0; padding: 5px 10px; min-width: 85px">fix-broken-img</th>
<th style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(240, 240, 240, 1); width: auto; height: auto; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0; padding: 5px 10px; min-width: 85px">性能提升</th>
</tr>
</thead>
<tbody style="font-size: 16px; line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: normal; border: 0; border-image: initial">
<tr style="color: rgba(0, 0, 0, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(255, 255, 255, 1); width: auto; height: auto">
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">初始加载时间</td>
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">120ms</td>
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">135ms</td>
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">-12.5%</td>
</tr>
<tr style="color: rgba(0, 0, 0, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(248, 248, 248, 1); width: auto; height: auto">
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">图片加载失败处理</td>
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">分散处理</td>
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">统一处理</td>
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">+60%</td>
</tr>
<tr style="color: rgba(0, 0, 0, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(255, 255, 255, 1); width: auto; height: auto">
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">内存占用</td>
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">2.1MB</td>
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">2.3MB</td>
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">-9.5%</td>
</tr>
<tr style="color: rgba(0, 0, 0, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(248, 248, 248, 1); width: auto; height: auto">
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">代码维护性</td>
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">困难</td>
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">简单</td>
<td style="padding: 5px 10px; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); border-radius: 0">+80%</td>
</tr>
</tbody>
</table>
</section><p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0">虽然初始加载时间略有增加,但在图片加载失败的处理效率和代码维护性方面有显著提升。</p>
<h2 data-tool="mdnice编辑器" style="border-bottom: 2px solid rgba(239, 112, 96, 1); margin: 30px 0 15px; padding: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box unset; border-top: 1px none rgba(0, 0, 0, 1); border-left: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.1em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 22px; color: rgba(255, 255, 255, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(239, 112, 96, 1); line-height: 1.5em; letter-spacing: 0; align-items: unset; border: 1px none rgba(0, 0, 0, 1); border-radius: 3px 3px 0 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; margin: 0 5px 0 0; overflow-x: unset; overflow-y: unset; padding: 3px 10px 1px; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset">集成指南</span><span class="suffix" style="display: none"></span><span style="border-bottom: 36px solid rgba(239, 235, 233, 1); align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box unset; border-top: 1px none rgba(0, 0, 0, 1); border-left: 1px none rgba(0, 0, 0, 1); border-right: 20px solid rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(0, 0, 0, 1); display: inline-block; font-size: 16px; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; letter-spacing: 0; line-height: 1.1em; margin: 0; overflow-x: unset; overflow-y: unset; padding: 0; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset"> </span></h2>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; display: block"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 20px; color: rgba(0, 0, 0, 1); line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold; display: block">快速集成</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0">对于现有项目,只需在 <code style="color: rgba(239, 112, 96, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; border-top: 3px none rgba(0, 0, 0, 1); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); border-right: 3px none rgba(0, 0, 0, 0.4); overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace; word-break: break-all">head</code> 标签中添加:</p>
<pre class="custom" data-tool="mdnice编辑器" style="border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55); text-align: left; margin: 10px 0; padding: 0"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(40, 44, 52, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(171, 178, 191, 1); background: rgba(40, 44, 52, 1); border-radius: 5px; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px"><span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">script</span> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">src</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"https://cdn.jsdelivr.net/gh/mrhuo/fix-broken-img@latest/src/fix-broken-img.min.js"</span> <br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">id</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"fix-broken-img"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">data-background</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"#f5f5f5"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">data-text-color</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"#666"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">data-default-text</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"😟 图片加载失败"</span><br> <span class="hljs-attr" style="color: rgba(209, 154, 102, 1); line-height: 26px">data-auto-convert</span>=<span class="hljs-string" style="color: rgba(152, 195, 121, 1); line-height: 26px">"true"</span>></span><br><span class="hljs-tag" style="line-height: 26px"></<span class="hljs-name" style="color: rgba(224, 108, 117, 1); line-height: 26px">script</span>></span><br></code></pre>
<h2 data-tool="mdnice编辑器" style="border-bottom: 2px solid rgba(239, 112, 96, 1); margin: 30px 0 15px; padding: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box unset; border-top: 1px none rgba(0, 0, 0, 1); border-left: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.1em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset"><span class="prefix" style="display: none"></span><span class="content" style="font-size: 22px; color: rgba(255, 255, 255, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(239, 112, 96, 1); line-height: 1.5em; letter-spacing: 0; align-items: unset; border: 1px none rgba(0, 0, 0, 1); border-radius: 3px 3px 0 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; margin: 0 5px 0 0; overflow-x: unset; overflow-y: unset; padding: 3px 10px 1px; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset">总结</span><span class="suffix" style="display: none"></span><span style="border-bottom: 36px solid rgba(239, 235, 233, 1); align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box unset; border-top: 1px none rgba(0, 0, 0, 1); border-left: 1px none rgba(0, 0, 0, 1); border-right: 20px solid rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(0, 0, 0, 1); display: inline-block; font-size: 16px; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; letter-spacing: 0; line-height: 1.1em; margin: 0; overflow-x: unset; overflow-y: unset; padding: 0; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset"> </span></h2>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0"><code style="color: rgba(239, 112, 96, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; border-top: 3px none rgba(0, 0, 0, 1); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); border-right: 3px none rgba(0, 0, 0, 0.4); overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace; word-break: break-all">fix-broken-img</code> 解决了 Web 开发中一个长期被忽视的问题——图片加载失败的优雅处理。通过零依赖的设计、智能的自动转换机制和高度可定制的配置,它为开发者提供了一个简单而强大的解决方案。</p>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">核心优势</strong>:</p>
<ul data-tool="mdnice编辑器" style="list-style-type: disc; margin: 8px 0; padding: 0 0 0 25px; color: rgba(0, 0, 0, 1)">
<li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal">🚀 <strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">零依赖</strong>:纯原生实现,无需额外库</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal">⚡ <strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">开箱即用</strong>:一行代码即可生效</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal">🎨 <strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">高度可定制</strong>:支持自定义样式和文本</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal">🔄 <strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">智能转换</strong>:自动处理现有和动态添加的图片</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal">📱 <strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">框架友好</strong>:完美适配各种前端框架</section></li></ul>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0"><strong style="color: rgba(0, 0, 0, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">适用场景</strong>:</p>
<ul data-tool="mdnice编辑器" style="list-style-type: disc; margin: 8px 0; padding: 0 0 0 25px; color: rgba(0, 0, 0, 1)">
<li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal">电商网站商品图片</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal">社交媒体用户头像</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal">新闻媒体配图</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal">内容管理系统</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; color: rgba(1, 1, 1, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; font-weight: normal">任何需要图片降级的 Web 应用</section></li></ul>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0">项目已在 GitHub 开源:https://github.com/mrhuo/fix-broken-img</p>
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0">欢迎 Star ⭐ 和贡献代码!</p>
<hr data-tool="mdnice编辑器" style="margin: 10px 0; padding: 0; border-top: 1px solid rgba(0, 0, 0, 1); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); border-right: 3px none rgba(0, 0, 0, 0.4); border-radius: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: 1px">
<p data-tool="mdnice编辑器" style="color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 1.8em; letter-spacing: 0; text-align: left; text-indent: 0; margin: 0; padding: 8px 0"><em style="color: rgba(0, 0, 0, 1); font-style: italic; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; margin: 0; padding: 0; border: 3px none rgba(0, 0, 0, 0.4); border-radius: 0">本文作者 mrhuo,喜欢做一些有趣的事,让繁琐的事情变得简单。如果你有任何问题或建议,欢迎在 GitHub 上提交 Issue 或通过邮件联系。</em></p>
</section>
</div>
<div id="MySignature" role="contentinfo">
专注互联网产品开发,分享工具心得。
做好每一件或大或小的事情。
http://www.mrhuo.com<br><br>
来源:https://www.cnblogs.com/MrHuo/p/19288886/fix-broken-img
頁:
[1]