延时加载图片
Posted jasonduanmu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了延时加载图片相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>css3</title> <style type="text/css"> *{ margin:0; padding: 0; } ul,li{ list-style: outside none none; } .box{ width: 600px; margin: 0 auto; } .box li{ margin: 40px 0; width: 100%; height: 170px; background: #ddd; } .box li img{ width: 100%; } </style> </head> <body> <div class="box"> <ul> <li><img x-src="img/1.jpg" alt=""></li> <li><img x-src="img/2.jpg" alt=""></li> <li><img x-src="img/3.jpg" alt=""></li> <li><img x-src="img/4.jpg" alt=""></li> <li><img x-src="img/5.jpg" alt=""></li> <li><img x-src="img/1.jpg" alt=""></li> <li><img x-src="img/2.jpg" alt=""></li> <li><img x-src="img/3.jpg" alt=""></li> <li><img x-src="img/4.jpg" alt=""></li> <li><img x-src="img/5.jpg" alt=""></li> <li><img x-src="img/1.jpg" alt=""></li> <li><img x-src="img/2.jpg" alt=""></li> <li><img x-src="img/3.jpg" alt=""></li> <li><img x-src="img/4.jpg" alt=""></li> <li><img x-src="img/5.jpg" alt=""></li> <li><img x-src="img/1.jpg" alt=""></li> <li><img x-src="img/2.jpg" alt=""></li> <li><img x-src="img/3.jpg" alt=""></li> <li><img x-src="img/4.jpg" alt=""></li> <li><img x-src="img/5.jpg" alt=""></li> <li><img x-src="img/1.jpg" alt=""></li> <li><img x-src="img/2.jpg" alt=""></li> <li><img x-src="img/3.jpg" alt=""></li> <li><img x-src="img/4.jpg" alt=""></li> <li><img x-src="img/5.jpg" alt=""></li> </ul> </div> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript"> $(function(){ function showImg(){ $(‘img‘).each(function(index,ele){ if($(this).offset().top<$(window).height()+$(window).scrollTop()){ var oSrc = $(this).attr(‘x-src‘); $(this).attr(‘src‘,oSrc); } }) } showImg(); $(window).scroll(function(){ showImg(); }); }); </script> </body> </html>
以上是关于延时加载图片的主要内容,如果未能解决你的问题,请参考以下文章