为ä»ä¹è¦ä½¿ç¨æå è½½
æå è½½ä¹å«å»¶è¿å è½½æçæ¯å½éè¦ç¨å°è¯¥èµæºçæ¶åæå¯¹å ¶è¿è¡å è½½ï¼æ¯ä¸ç§æåç½é¡µæ§è½çæ¹å¼ã
æå è½½å¯ä»¥æåç¨æ·ä½éªï¼æ¯å¦å¨æ们æµè§ä¸ä¸ªæ大éå¾ççç½é¡µçæ¶åå¦æä¸æ¬¡æ§å è½½å®ææçå¾çå°±ä¼çå¾ å¾é¿çæ¶é´ï¼å½±åç¨æ·çä½éªï¼è¿æ ·å¯è½å°±ä¼æµå¤±ä¸é¨å客æ·ï¼æ以为äºè§£å³è¿ä¸ªé®é¢å°±å¼å ¥äºå¾çæå è½½ï¼å½ç¨æ·è¿å ¥åºæ¯çæ¶åå å 载第ä¸å±çå¾çï¼ä¹å¯ä»¥åå¤ä¸ç¹ï¼ï¼ç¶åå½ç¨æ·åä¸æ»å¨çæ¶ååå è½½åé¢å¯¹åºçä¸é¡µä¸çå¾çï¼è¿æ ·ï¼å¦æç¨æ·çäºå 页ä¸æ³çäºï¼åè¾¹çå¾çå°±ä¸ç¨å è½½äºï¼åè½»äºæå¡å¨çååãç¸åºçé¦å±å±ç¤ºä¹æ´å¿«äºã
ä»ä¸å¯ä»¥çåºæå è½½çä¼ç¹ï¼
- 缩çé¦å±å è½½æ¶é´ï¼æåç¨æ·ä½éªã
- åè½»æå¡å¨ååï¼æ´å ·ç¨æ·éæ±å è½½ï¼ä¸ç¨å ¨é¨å è½½
æå è½½åºç¨åå广æ³ï¼æ¯å¦æ·å®åªä¼å 载第ä¸å±çå¾çï¼å¦æç¨æ·éè¦æµè§åé¢çå¾çåè¿è¡å è½½ãå½ä½ å¿«éææ»å¨æ¡æå°æåä½ ä¼åç°å¾ç并没æå è½½å®æã
æå è½½å®ç°åç
æå è½½çåºæ¬æ路就æ¯ä¸ç»img
åsrc
å±æ§ï¼æå¾çå°ååå¨data-src
ä¸ï¼å½éè¦å 载该å¾çæ¶åædata-src
ä¸çå¼åå°src
ä¸ã
å¦ä½å¤ææ¯å¦éè¦å è½½ï¼
å¤æå¾ç顶é¨è·ç¦»é¡µé¢é¡¶é¨çè·ç¦»æ¯å¦å°äºé¡µé¢çé«åº¦ï¼å³å½å¾çè¿å ¥å¯è§åºåçæ¶åå°±å 载该å¾çã
è·åå¾çè·ç¦»é¡µé¢é¡¶é¨è·ç¦»
使ç¨APIæ¥å£ï¼el.getBoundingClientRect()
ï¼å®ä¼è¿åä¸ä¸ªDOMRect
对象ï¼DOMRect
对象å
å«äºä¸ç»ç¨äºæè¿°è¾¹æ¡çåªè¯»å±æ§ââleft
ãtop
ãright
åbottom
ï¼åä½ä¸ºåç´ ãé¤äº width
å height
å¤çå±æ§é½æ¯ç¸å¯¹äºè§å£çå·¦ä¸è§ä½ç½®èè¨çã
è¿æ ·æ们就å¯ä»¥éè¿DOMRect
对象çtop
å±æ§æ¥è·åå¾çè·ç¦»é¡µé¢é¡¶é¨è·ç¦»ã
代ç å®ç°
<!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>Document</title>
<style>
.image-item {
display: block;
margin-bottom: 50px;
height: 400px;
width: 400px;
background-color: #cccccc;
}
</style>
</head>
<body>
<img src="" alt="" class="image-item" data-src="./imgs/0.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/1.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/2.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/3.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/4.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/5.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/6.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/7.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/8.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/9.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/10.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/11.jpg">
<script>
let viewHeight = document.documentElement.clientHeight || document.body.clientHeight
let imgs = document.querySelectorAll("img[data-src]")
imgs = [...imgs]
imgs.forEach(item => {
lazy(item)
})
function lazy(item) {
let itemTop = item.getBoundingClientRect().top
if (itemTop < viewHeight && itemTop > 0) {
let src = item.getAttribute("data-src")
item.setAttribute("src", src)
item.classList.add("show")
}
if (itemTop > viewHeight && item < 0) {
item.removeAttribute("src")
}
}
window.addEventListener("scroll", function () {
imgs.forEach(item => {
lazy(item)
})
})
</script>
</body>
</html>
å¤å¶ä»£ç
è¿æ ·å°±å®ç°äºä¸ä¸ªç®åçæå è½½ï¼ä½æ¯æ们åç°å¨æ»å¨æ»å¨æ¡çæ¶ååªè¦å¾çè¿å ¥å¯è§åºåå°±ç«å³å è½½äºï¼æ们å¸æå¯ä»¥å¨æ们åæ¢æ»å¨ä¸æ®µæ¶é´åæå è½½ï¼æ以è¿éæ们éè¦é²æå½æ°æ¥æ§å¶ã
<!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>Document</title>
<style>
.image-item {
display: block;
margin-bottom: 50px;
height: 400px;
width: 400px;
background-color: #cccccc;
}
.image-item.show {
animation: show 1s linear;
}
@keyframes show {
0% {filter: blur(3px)}
30% {filter: blur(1px)}
100% {filter: blur(0px)}
}
</style>
</head>
<body>
<img src="" alt="" class="image-item" data-src="./imgs/0.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/1.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/2.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/3.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/4.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/5.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/6.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/7.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/8.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/9.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/10.jpg">
<img src="" alt="" class="image-item" data-src="./imgs/11.jpg">
<script>
let debounce = function (fn, wait, immedate = false) {
let timer = null
return function (...arg) {
if (immedate) {
fn.apply(this, arg)
immedate = false
}
timer ? clearTimeout(timer) : ''
timer = setTimeout(() => {
fn.apply(this)
}, wait);
}
}
let viewHeight = document.documentElement.clientHeight || document.body.clientHeight
let imgs = document.querySelectorAll("img[data-src]")
imgs = [...imgs]
imgs.forEach(item => {
lazy(item)
})
function lazy(item) {
let itemTop = item.getBoundingClientRect().top
if (itemTop < viewHeight && itemTop > 0) {
let src = item.getAttribute("data-src")
item.setAttribute("src", src)
item.classList.add("show")
}
if (itemTop > viewHeight && item < 0) {
item.removeAttribute("src")
}
}
window.addEventListener("scroll", debounce(function () {
imgs.forEach(item => lazy(item))
}, 100, false))
</script>
</body>
</html>
å¤å¶ä»£ç
ä¸é¢æ们è¿æ·»å äºä¸ç¹å¨ç»ï¼è®©å¾çåºç°çä¸é£ä¹çªå ã
å°è¿éå°±å ¨é¨è®²å®å¦ï¼åæç« ä¸å®¹æå¸æåä½å°å¥å¥ãå°å§å§ã大佬们ç¹ä¸ªèµå§ã