-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
实现懒加载效果
- Loading branch information
Showing
34 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
## demo | ||
[view](https://wkstudy.github.io/smallplugs/lazyload/index.html) | ||
|
||
## time | ||
19.03.23 | ||
|
||
## 参考 | ||
|
||
* 关于懒加载的实践部分主要参考[原生js实现图片懒加载(lazyLoad) - 天之蓝源的文章 - 知乎](https://zhuanlan.zhihu.com/p/55311726) | ||
* 关于节流部分的使用主要参考的是[JS简单实现防抖和节流](https://blog.csdn.net/sml115/article/details/81280101) | ||
|
||
## 说明 | ||
|
||
* 本次懒加载实现主要使用getBoundingClientRect() 这个api | ||
* throttle 函数两个都可以用 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<!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>lazyload</title> | ||
<style> | ||
ul { | ||
padding: 0 10% 0 10%; | ||
list-style-type: none; | ||
font-size: 0; | ||
} | ||
li { | ||
vertical-align: middle; | ||
display: inline-block; | ||
width: 25%; | ||
height: 700px; | ||
box-sizing: border-box; | ||
} | ||
li img { | ||
width: 100%; | ||
height: 100%; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<ul> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/1.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/2.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/3.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/4.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/5.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/6.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/7.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/8.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/9.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/10.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/11.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/12.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/13.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/14.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/15.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/16.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/17.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/18.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/19.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/20.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/21.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/22.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/23.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/24.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/25.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/26.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/27.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/28.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/29.jpg"></li> | ||
<li><img src="./pic/default.jpg" alt="picture-lazy" data-src="./pic/30.jpg"></li> | ||
</ul> | ||
</body> | ||
<script> | ||
var imgs = document.getElementsByTagName('img'), | ||
clientHeight = document.documentElement.clientHeight; | ||
|
||
lazyload(imgs, clientHeight, 'file:///F:/GitHub/smallplugs/lazyload/pic/default.jpg'); // 初始化加载,防止一开始全部是默认图片 | ||
|
||
window.addEventListener('scroll', throttle(lazyload, 1000).bind(null, imgs, clientHeight, 'file:///F:/GitHub/smallplugs/lazyload/pic/default.jpg'), false); | ||
|
||
/** | ||
* @parms imgs {Array-like} element节点结合 | ||
* @parms clientHeight {Number} 浏览器可视区高度 | ||
* @parms defaultSrc {String} 图片默认加载地址 | ||
*/ | ||
function lazyload (imgs, clientHeight, defaultSrc) { | ||
imgArr = Array.from(imgs); // 将类数组对象转换为数组,方便使用数组方法forEach,当然也可以直接使用for循环,不用转换为数组 | ||
imgArr.forEach(img => { | ||
var src, | ||
rect = img.getBoundingClientRect(); | ||
|
||
// 图像在可视区域往上100px,往下100px以内加载图片 | ||
if (rect.top < clientHeight + 100 && rect.top > -(rect.height + 100) ) { | ||
src = img.getAttribute('data-src'); | ||
// 如果加载过了,就不加载 | ||
if(img.src == defaultSrc) { | ||
img.src = src; | ||
} | ||
} | ||
}); | ||
} | ||
/* | ||
function throttle (fn, delay) { | ||
var timeId = null; | ||
return function () { | ||
var context = this, | ||
args = arguments; | ||
if (!timeId) { | ||
timeId = setTimeout(function () { | ||
fn.apply(context, args); | ||
timeId = null; | ||
}, delay); | ||
} | ||
} | ||
} | ||
*/ | ||
|
||
function throttle (fn, delay) { | ||
var time = new Date(); | ||
|
||
return function () { | ||
var context = this, | ||
args = arguments; | ||
|
||
var now = new Date(); | ||
if (now.getTime() - time.getTime() >= delay) { | ||
fn.apply(context, args); | ||
time = now; | ||
} | ||
} | ||
} | ||
</script> | ||
</html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters