如果你只读每个人都在读的书,你也只能想到每个人都能想到的事。——村上春树
首先贴代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> .ruben { width: 400px; height: 300px; background-color: #e8e8e8 }
.ruben img { width: 100%; height: 100%; } </style> </head> <body>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3059_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3058_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3057_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3055_3.jpg"></div> <div class="ruben"><img src="https://VampireAchao.github.io/imgs/preview/3056_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3054_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3053_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3052_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3051_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3050_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3049_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3048_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3047_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3046_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3045_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3044_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3043_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3042_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3041_3.jpg"></div> <div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3040_3.jpg"></div>
<script type="text/javascript"> let lazyLoadImgs = document.querySelectorAll("img")
function lazyLoad() { let clientHeight = document.documentElement.clientHeight lazyLoadImgs.forEach(e => { if (!e.src) { let rect = e.getBoundingClientRect() if (rect.top < clientHeight) { e.src = e.dataset.src } } }) window.onscroll ??= lazyLoad } lazyLoad() </script> </body> </html>
|
在你们自己的代码中使用方式也很简单:
首先复制这串js
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| let lazyLoadImgs = document.querySelectorAll("img")
function lazyLoad() { let clientHeight = document.documentElement.clientHeight lazyLoadImgs.forEach(e => { if (!e.src) { let rect = e.getBoundingClientRect() if (rect.top < clientHeight) { e.src = e.dataset.src } } }) window.onscroll ??= lazyLoad } lazyLoad()
|
然后在你们页面中使用,这里需要将你们原来页面上img
标签的src
改为data-src
,这样在元素处于可视区域,则将data-src
中的值放入src
,然后达到懒加载的效果