示例工程 17-gallery-page

17-gallery-page/demo-02-picture/index.html

html · 2.3 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.7; }
      h1 { margin: 0 0 8px; font-size: 28px; }
      p { margin: 0 0 18px; color: #334155; }
      .gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; margin: 0; padding: 0; list-style: none; }
      figure { margin: 0; padding: 10px; border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; }
      img { display: block; width: 100%; height: auto; border-radius: 6px; }
      figcaption { margin-top: 6px; font-size: 13px; color: #475569; }
      a { color: #2563eb; }
    </style>
  </head>
  <body>
    <h1>图库与响应式图片</h1>
    <p>窄屏使用竖版裁剪,宽屏使用横版。</p>

    <ul class="gallery">
      <li>
        <figure>
          <a href="assets/full-forest.svg">
            <picture>
              <source media="(max-width: 600px)" srcset="assets/narrow-forest.svg" />
              <img src="assets/forest.svg" alt="晨雾森林:竖版裁剪突出树梢与雾" width="480" height="320" loading="lazy" />
            </picture>
          </a>
          <figcaption>晨雾森林 · 自然</figcaption>
        </figure>
      </li>
      <li>
        <figure>
          <a href="assets/full-coast.svg">
            <picture>
              <source media="(max-width: 600px)" srcset="assets/narrow-coast.svg" />
              <img src="assets/coast.svg" alt="海岸线:竖版裁剪保留海水与沙滩" width="480" height="320" loading="lazy" />
            </picture>
          </a>
          <figcaption>海岸线 · 自然</figcaption>
        </figure>
      </li>
      <li>
        <figure>
          <a href="assets/full-city.svg">
            <picture>
              <source media="(max-width: 600px)" srcset="assets/narrow-city.svg" />
              <img src="assets/city.svg" alt="夜行城市:竖版裁剪保留天际线" width="480" height="320" loading="lazy" />
            </picture>
          </a>
          <figcaption>夜行城市 · 城市</figcaption>
        </figure>
      </li>
    </ul>
  </body>
</html>