实现原理:在最开始的时候,大图是隐藏的,小图示显示的,等大图加载完成,显示大图,隐藏小图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .bigImg { width: 100%; height: 100vh; } .smallImg { width: 100%; height: 100vh; } </style> </head> <body> <img src="./big.png" alt="" class="bigImg" style="display: none" /> <img src="./samll.png" alt="" class="smallImg" style="display: block" /> </body> <script> const bigImg = document.querySelector('.bigImg') const smallImg = document.querySelector('.smallImg') bigImg.addEventListener('load', function () { console.log('大图已加载完成,小图隐藏,大图展示') bigImg.style.display = 'block' smallImg.style.display = 'none' }) </script> </html>