检测浏览器是否支持webp
<script> async function supportsWebp() { if (!self.createImageBitmap) return false; const webpData = 'data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAAAAAAfQ//73v/+BiOh/AAA='; const blob = await fetch(webpData).then(r => r.blob()); return createImageBitmap(blob).then(() => true, () => false); } (async () => { if (await supportsWebp()) { const bg = document.getElementById('header'); bg.style.backgroundImage = "url(img/bg.webp)"; } else { console.log('不支持'); } })(); </script>