示例工程 13-images

13-images/demo-02-loading/index.html

html · 1.9 KB

运行:加载与尺寸演示

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>加载与尺寸演示</title>
    <style>
      body { margin: 0; padding: 32px; background: #f8fafc; color: #0f172a; font-family: system-ui, sans-serif; line-height: 1.75; }
      .box { max-width: 720px; margin-bottom: 16px; padding: 18px; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
      h1 { margin: 0 0 16px; font-size: 28px; }
      h2 { margin: 0 0 8px; font-size: 17px; }
      p { margin: 0 0 10px; color: #334155; }
      img { max-width: 100%; height: auto; border-radius: 8px; }
      .spacer { height: 240px; display: grid; place-items: center; color: #94a3b8; border: 1px dashed #cbd5e1; border-radius: 8px; }
      code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, monospace; }
      pre { margin: 8px 0 0; padding: 12px; overflow-x: auto; border-radius: 8px; background: #0f172a; color: #e2e8f0; font-size: 12px; }
    </style>
  </head>
  <body>
    <h1>加载与尺寸演示</h1>

    <div class="box">
      <h2>首屏主图:不懒加载</h2>
      <img src="assets/hero.svg" alt="图片加载流程:请求、解码、渲染" width="800" height="450" fetchpriority="high" />
    </div>

    <div class="box">
      <h2>下方图片:懒加载</h2>
      <div class="spacer">向下滚动触发加载</div>
      <img src="assets/chart.svg" alt="季度访问量柱状图" width="640" height="360" loading="lazy" />
    </div>

    <div class="box">
      <h2>写法</h2>
      <pre>&lt;img src="hero.svg" alt="…" width="800" height="450" fetchpriority="high"&gt;
&lt;img src="chart.svg" alt="…" width="640" height="360" loading="lazy"&gt;</pre>
      <p>宽高用于预留空间,<code>loading</code> 控制加载时机。</p>
    </div>
  </body>
</html>