news 2026/6/1 10:42:07

图片777

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
图片777
八、图片显示优化 问题:让你优化图片显示你怎么优化 // 1. 格式选择 // WebP > AVIF > JPEG/PNG // WebP比JPEG小25-35%,比PNG小80% // 2. 响应式图片 <img src="image-800.jpg" srcset=" image-400.jpg 400w, image-800.jpg 800w, image-1200.jpg 1200w " sizes=" (max-width: 500px) 400px, (max-width: 900px) 800px, 1200px " > // 3. 懒加载 <img loading="lazy" src="image.jpg"> // 4. 渐进式加载 function ProgressiveImage({ src, placeholder }) { const [currentSrc, setCurrentSrc] = useState(placeholder) useEffect(() => { const img = new Image() img.src = src img.onload = () => setCurrentSrc(src) }, [src]) return ( <img src={currentSrc} style={{ filter: currentSrc === placeholder ? 'blur(10px)' : 'none' }} /> ) } // 5. 使用CDN // 加上图片处理参数 https://cdn.example.com/image.jpg?w=800&q=75&format=webp // 6. 缓存策略 // 强缓存 + hash文件名 Cache-Control: max-age=31536000 // image-8f3c9d.jpg // 7. 预加载关键图片 <link rel="preload" as="image" href="hero.jpg"> // 8. 使用Intersection Observer const observer = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { const img = entry.target img.src = img.dataset.src observer.unobserve(img) } }) }) 九、WebP兼容性降级 问题:webp格式的图片有兼容性问题吗,怎么做降级处理 // 1. picture标签(最推荐) <picture> <source srcset="image.webp" type="image/webp"> <source srcset="image.jp2" type="image/jp2"> <img src="image.jpg" alt="fallback"> </picture> // 2. 特性检测 function supportsWebP() { const canvas = document.createElement('canvas') if (canvas.toDataURL) { return canvas.toDataURL('image/webp').indexOf('image/webp') === 0 } return false } // 使用 const src = supportsWebP() ? 'image.webp' : 'image.jpg' // 3. 服务端检测 app.get('/image', (req, res) => { const accept = req.headers['accept'] if (accept && accept.includes('image/webp')) { res.setHeader('Content-Type', 'image/webp') res.sendFile('image.webp') } else { res.sendFile('image.jpg') } }) // 4. Modernizr if (Modernizr.webp) { // 使用WebP } else { // 使用降级格式 } // 5. Next.js自动处理 <Image src="/image.jpg" /> // 自动检测Accept头,返回对应格式 // 6. 批量转换工具 // 构建时生成webp版本 const imagemin = require('imagemin') const imageminWebp = require('imagemin-webp') await imagemin(['images/*.{jpg,png}'], { destination: 'public/images', plugins: [imageminWebp({ quality: 75 })] }) // 7. 动态加载 const webpSupported = await checkWebPSupport() const imageUrl = webpSupported ? '/img.webp' : '/img.jpg' 原文链接:https://blog.csdn.net/weixin_50077637/article/details/158740098
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/1 10:36:18

ESP32 BLE Mesh配网踩坑实录:为什么你的Client例程绑定AppKey总失败?

ESP32 BLE Mesh配网深度解析&#xff1a;Client模型绑定AppKey失败的根源与解决方案在物联网设备组网技术中&#xff0c;BLE Mesh凭借其低功耗、自组网特性成为智能家居、工业控制等场景的热门选择。ESP32作为支持BLE Mesh协议的明星芯片&#xff0c;其官方提供的例程是开发者快…

作者头像 李华
网站建设 2026/6/1 10:29:36

Alexa for Startups:智能语音硬件与技能开发的实战指南

1. 项目概述&#xff1a;当智能语音助手遇见初创生态如果你是一位智能硬件创业者&#xff0c;或者正在开发一款需要语音交互的消费级应用&#xff0c;那么“Alexa for Startups”这个项目&#xff0c;很可能就是你一直在寻找的“东风”。这不仅仅是亚马逊将其广为人知的Alexa语…

作者头像 李华
网站建设 2026/6/1 10:29:34

认知科学视角下的高效决策:选择性遗忘如何提升复杂问题解决能力

1. 项目概述&#xff1a;棋手记忆的悖论 “要下得像一位象棋大师&#xff0c;你需要学会如何遗忘。” 这句话乍一听像是某种玄学&#xff0c;或者是对“忘掉过去&#xff0c;专注当下”这种心灵鸡汤的另一种表述。但如果你真的接触过职业棋手&#xff0c;或者深入钻研过任何需要…

作者头像 李华