手动实现v-lazy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>v-lazy</title> </head> <style> .container{ width: 100%; overflow: auto; display: flex; flex-direction: column; } .img{ height: 600px; display: block; } </style> <body> <div class="container"> <img class="img" src="./img/loading.jpg" data-src='./img/1.jpg' alt="img"> <img class="img" src="./img/loading.jpg" data-src='./img/2.jpg' alt="img"> <img class="img" src="./img/loading.jpg" data-src='./img/3.jpg' alt="img"> <img class="img" src="./img/loading.jpg" data-src='./img/4.jpg' alt="img"> </div> </body> <script> let imgs=document.querySelectorAll('.img'); let observer=new IntersectionObserver((e,o)=>{ console.log(e); e.map(item=>{ if(item.intersectionRatio>0){ item.target.src=item.target.dataset.src; observer.unobserve(item.target); } }) }) imgs.forEach(item=>{ observer.observe(item); }) </script> </html>
参考实现:阮一峰老师:http://www.ruanyifeng.com/blog/2016/11/intersectionobserver_api.html