示例工程 14-figure-picture
14-figure-picture/demo-02-picture/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>picture 演示</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; }
pre { margin: 8px 0 0; padding: 12px; overflow-x: auto; border-radius: 8px; background: #0f172a; color: #e2e8f0; font-size: 12px; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, monospace; }
</style>
</head>
<body>
<h1>picture 演示</h1>
<div class="box">
<h2>艺术方向:按屏宽换图</h2>
<p>把窗口缩到 600px 以下,会换成竖向图。</p>
<picture>
<source media="(max-width: 600px)" srcset="assets/narrow.svg" />
<img src="assets/wide.svg" alt="布局示意:宽屏横向,窄屏纵向" width="800" height="400" />
</picture>
</div>
<div class="box">
<h2>格式回退:AVIF → WebP → PNG</h2>
<pre><picture>
<source srcset="chart.avif" type="image/avif">
<source srcset="chart.webp" type="image/webp">
<img src="chart.png" alt="季度访问量柱状图" width="640" height="360">
</picture></pre>
<p>浏览器按顺序选择第一个支持的格式,最后用 <code>img</code> 回退。</p>
</div>
</body>
</html>