Blogger Information
Blog 100
fans 8
comment 2
visits 150104
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
懒加载案例(javascript原生代码)
lilove的博客
Original
984 people have browsed it

JS原生代码实现懒加载

HTML

  1. <!DOCTYPE html>
  2. <html lang="zh-cn">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>懒加载案例</title>
  7. </head>
  8. <style>
  9. .images>img {
  10. height: 200px;
  11. }
  12. </style>
  13. <body>
  14. <div class="images">
  15. <img src="img/temp.jpg" alt="" data-src="img/apic26676.jpg">
  16. <img src="img/temp.jpg" alt="" data-src="img/apic26703.jpg">
  17. <img src="img/temp.jpg" alt="" data-src="img/apic26775.jpg">
  18. <img src="img/temp.jpg" alt="" data-src="img/apic26841.jpg">
  19. <img src="img/temp.jpg" alt="" data-src="img/apic26867.jpg">
  20. <img src="img/temp.jpg" alt="" data-src="img/apic26870.jpg">
  21. <img src="img/temp.jpg" alt="" data-src="img/apic26871.jpg">
  22. <img src="img/temp.jpg" alt="" data-src="img/apic26888.jpg">
  23. <img src="img/temp.jpg" alt="" data-src="img/apic27129.jpg">
  24. <img src="img/temp.jpg" alt="" data-src="img/apic27135.jpg">
  25. <img src="img/temp.jpg" alt="" data-src="img/apic27147.jpg">
  26. <img src="img/temp.jpg" alt="" data-src="img/apic27158.jpg">
  27. <img src="img/temp.jpg" alt="" data-src="img/apic27167.jpg">
  28. <img src="img/temp.jpg" alt="" data-src="img/apic27173.jpg">
  29. <img src="img/temp.jpg" alt="" data-src="img/apic27188.jpg">
  30. <img src="img/temp.jpg" alt="" data-src="img/apic27195.jpg">
  31. <img src="img/temp.jpg" alt="" data-src="img/apic27201.jpg">
  32. <img src="img/temp.jpg" alt="" data-src="img/apic27248.jpg">
  33. <img src="img/temp.jpg" alt="" data-src="img/apic27270.jpg">
  34. <img src="img/temp.jpg" alt="" data-src="img/apic27290.jpg">
  35. <img src="img/temp.jpg" alt="" data-src="img/apic27302.jpg">
  36. <img src="img/temp.jpg" alt="" data-src="img/apic27350.jpg">
  37. <img src="img/temp.jpg" alt="" data-src="img/apic27387.jpg">
  38. <img src="img/temp.jpg" alt="" data-src="img/apic27417.jpg">
  39. <img src="img/temp.jpg" alt="" data-src="img/apic27435.jpg">
  40. <img src="img/temp.jpg" alt="" data-src="img/apic27438.jpg">
  41. <img src="img/temp.jpg" alt="" data-src="img/apic27449.jpg">
  42. <img src="img/temp.jpg" alt="" data-src="img/apic27455.jpg">
  43. <img src="img/temp.jpg" alt="" data-src="img/apic27477.jpg">
  44. <img src="img/temp.jpg" alt="" data-src="img/apic27494.jpg">
  45. <img src="img/temp.jpg" alt="" data-src="img/apic27509.jpg">
  46. <img src="img/temp.jpg" alt="" data-src="img/apic27512.jpg">
  47. <img src="img/temp.jpg" alt="" data-src="img/apic27519.jpg">
  48. <img src="img/temp.jpg" alt="" data-src="img/apic27606.jpg">
  49. <img src="img/temp.jpg" alt="" data-src="img/apic27625.jpg">
  50. <img src="img/temp.jpg" alt="" data-src="img/apic27692.jpg">
  51. <img src="img/temp.jpg" alt="" data-src="img/apic27821.jpg">
  52. <img src="img/temp.jpg" alt="" data-src="img/apic27830.jpg">
  53. <img src="img/temp.jpg" alt="" data-src="img/apic27849.jpg">
  54. <img src="img/temp.jpg" alt="" data-src="img/hpic2692.jpg">
  55. <img src="img/temp.jpg" alt="" data-src="img/hpic2735.jpg">
  56. <img src="img/temp.jpg" alt="" data-src="img/hpic2829.jpg">
  57. <img src="img/temp.jpg" alt="" data-src="img/hpic2843.jpg">
  58. </div>
  59. </body>
  60. <script>
  61. // 1.获取所有图片
  62. var imgs = document.querySelectorAll(".images>img");
  63. // 2.获取文档高度
  64. var clientHeight = document.documentElement.clientHeight;
  65. // 3.监听滚动事件
  66. window.addEventListener("scroll", function () {
  67. lazyload(imgs, clientHeight);
  68. }, false);
  69. // 4.懒加载函数
  70. function lazyload(imgs, clientHeight) {
  71. // 获取html文档的滚动大小
  72. var scrollTop = document.documentElement.scrollTop;
  73. // 遍历图片,判断是否进入可视区
  74. imgs.forEach(function (img) {
  75. if (img.offsetTop <= clientHeight + scrollTop) {
  76. img.src = img.dataset.src;
  77. }
  78. });
  79. }
  80. </script>
  81. </html>

实现效果

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:懒加载有多种实现方案, 可以试试其它种
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post